public void Setup() { httpContextHelper = Substitute.For <IHttpContextHelper>(); httpContextHelper.getAccessToken().Returns("access_token"); stravaClient = Substitute.For <IStravaClient>(); IEnumerable <VisualActivity> activities = TestData.VisualActivitiesList(); IEnumerable <VisualActivity> newUserActivities = TestData.NewVisualActivitiesList(); stravaClient.getAllUserActivities("access_token", 123).Returns(activities); stravaClient.getAllUserActivities("access_token", 2222).Returns(newUserActivities); stravaClient.getUserActivitiesAfter("access_token", Arg.Any <StravaUser>(), Arg.Any <DateTime>()).Returns(newUserActivities); map = new Map(); var userActivity = new StravaUser { VisualActivities = (List <VisualActivity>)activities, UserId = 2, LastDownload = DateTime.Now }; userActivities = new List <StravaUser> { new StravaUser { VisualActivities = (List <VisualActivity>)activities, UserId = 1, LastDownload = DateTime.Now }, userActivity, new StravaUser { VisualActivities = (List <VisualActivity>)activities, UserId = 3, LastDownload = DateTime.Now }, }.AsQueryable(); userActivityRepository = Substitute.For <IStravaVisualizerRepository>(); userActivityRepository.GetUserActivities().Returns(userActivities); userActivityRepository.GetStravaUserById(123).Returns(userActivity); userActivityRepository.GetStravaUserById(2222).Returns(new StravaUser()); }
private StravaUser getUpdatedUserActivities(string accessToken, int id) { var user = _context.GetStravaUserById(id); if (user == null || user.VisualActivities == null || user.VisualActivities.Count == 0) { var activities = _stravaClient.getAllUserActivities(accessToken, id); user = new StravaUser() { VisualActivities = activities.ToList(), UserId = id, LastDownload = DateTime.Now.Date }; _context.Add(user); _context.SaveChanges(); } else { var latestActivities = _stravaClient.getUserActivitiesAfter(accessToken, user, user.LastDownload); if (latestActivities != null) { foreach (var activity in latestActivities) { if (!_context.Contains(activity)) { _context.Add(activity); user.VisualActivities.Add(activity); } } _context.SaveChanges(); } } return(user); }
public void SetStravaUser(StravaUser stravaUser) { CurrentUser.StravaUser = stravaUser; this.repo.Upsert(this.CurrentUser); CurrentUser = this.repo.Get(CurrentUserId); }
public void Test_UserActivities() { StravaUser activities = new StravaUser(); activities.VisualActivities = visualActivities; activities.LastDownload = DateTime.Now.Date; activities.UserId = 123; CollectionAssert.AreEqual((List <VisualActivity>)visualActivities, (List <VisualActivity>)activities.VisualActivities); Assert.AreEqual(DateTime.Now.Date, activities.LastDownload); Assert.AreEqual(123, activities.UserId); }
public void Test_RequesUserActivitiesAfter_ZeroRequiredPages() { StravaClient stravaClient = new StravaClient(activitiesApi, athleteApi); DateTime dateTime = DateTime.Now; var user = new StravaUser() { VisualActivities = null, UserId = 123, LastDownload = dateTime }; //List<VisualActivity> result = (List<VisualActivity>) stravaClient.getUserActivitiesAfter("access_token", 123, dateTime); List <VisualActivity> result = (List <VisualActivity>)stravaClient.getUserActivitiesAfter("access_token", user, dateTime); Assert.AreEqual(0, result.Count); }
public async Task requestActivitiesAfterAsync() { StravaClient stravaClient = new StravaClient(activitiesApi, athleteApi); DateTime dateTime = DateTime.Now; var user = new StravaUser() { VisualActivities = TestData.VisualActivitiesList(), UserId = 123, LastDownload = dateTime }; List <SummaryActivity> result = (List <SummaryActivity>) await stravaClient.requestActivitiesAfterAsync("access_token", user, dateTime); Assert.AreEqual(9, result.Count); Assert.AreEqual(100, result.ToArray()[0].MovingTime); Assert.AreEqual(ActivityType.Run, result.ToArray()[1].Type); }
public void Test_RequesUserActivitiesAfter() { StravaClient stravaClient = new StravaClient(activitiesApi, athleteApi); DateTime dateTime = DateTime.Now; var user = new StravaUser() { VisualActivities = TestData.VisualActivitiesList(), UserId = 123, LastDownload = dateTime }; //List<VisualActivity> result = (List<VisualActivity>) stravaClient.getUserActivitiesAfter("access_token", 123, dateTime); List <VisualActivity> result = (List <VisualActivity>)stravaClient.getUserActivitiesAfter("access_token", user, dateTime); Assert.AreEqual(9, result.Count); Assert.AreEqual(100, result.ToArray()[0].Summary.MovingTime); Assert.AreEqual(ActivityType.Run, result.ToArray()[1].Summary.Type); }
public IEnumerable <VisualActivity> getUserActivitiesAfter(string accessToken, StravaUser stravaUser, DateTime afterDate) { List <VisualActivity> visualActivites = new List <VisualActivity>(); if (stravaUser.VisualActivities == null) { return(visualActivites); } var summaryActivities = requestActivitiesAfterAsync(accessToken, stravaUser, afterDate).Result; if (summaryActivities == null) { return(null); } foreach (var summary in summaryActivities) { visualActivites.Add(new VisualActivity(summary)); } return(visualActivites.AsEnumerable()); }
private int getActivityDifferenceSinceLastDownload(StravaUser stravaUser, int totalActivities) { int activitesDownloaded = stravaUser.VisualActivities.Count(); return(totalActivities - activitesDownloaded); }
public async Task <IEnumerable <SummaryActivity> > requestActivitiesAfterAsync(string accessToken, StravaUser stravaUser, DateTime afterDate) { Configuration.Default.AccessToken = accessToken; try { var athleteStats = await _athletesApi.GetStatsAsync(stravaUser.UserId); int totalActivites = calcTotalActivityCount(athleteStats); int requiredActivities = getActivityDifferenceSinceLastDownload(stravaUser, totalActivites); return(await requestActivitiesAfter(requiredActivities, afterDate)); } catch (Exception e) { Debug.Print("Exception when calling ActivitiesApi.getLoggedInAthleteActivities: " + e.Message); } return(null); }