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());
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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());
        }
예제 #9
0
        private int getActivityDifferenceSinceLastDownload(StravaUser stravaUser, int totalActivities)
        {
            int activitesDownloaded = stravaUser.VisualActivities.Count();

            return(totalActivities - activitesDownloaded);
        }
예제 #10
0
        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);
        }