예제 #1
0
        public void Setup()
        {
            accessToken  = "access_token";
            stravaId     = 123;
            stravaClient = Substitute.For <IStravaClient>();;
            context      = Substitute.For <ICoordinatesRepository>();

            //Mock stravaClient
            IEnumerable <VisualActivity> activities        = TestData.VisualActivitiesList();
            IEnumerable <VisualActivity> newUserActivities = TestData.NewVisualActivitiesList();
            IEnumerable <Coordinates>    coordinates       = TestData.CoordinatesList();
            IEnumerable <Coordinates>    newCoordinates    = TestData.NewCoordinatesList();

            stravaClient.getAllUserActivities(accessToken, 123).Returns(activities);
            stravaClient.getAllUserActivities(accessToken, 2222).Returns(newUserActivities);
            stravaClient.getAllUserCoordinatesById(accessToken, 123).Returns(coordinates);
            stravaClient.getAllUserCoordinatesById(accessToken, 2222).Returns(coordinates);
            stravaClient.getUserActivitiesByIdAfter(accessToken, Arg.Any <DateTime>()).Returns(newUserActivities);
            stravaClient.getUserCoordinatesByIdAfter(accessToken, Arg.Any <DateTime>()).Returns(newCoordinates);

            //Mock repository
            var stravaUser = new User {
                StartCoordinates = coordinates.ToList(), UserId = 123, LastDownload = DateTime.Now
            };

            context = Substitute.For <ICoordinatesRepository>();
            context.GetUserById(123).Returns(stravaUser);
            context.GetUserById(2222).Returns(new User());
        }
        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());
        }
예제 #3
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);
        }
예제 #4
0
        private IEnumerable <MonthSummaryActivity> getUserMonthlyActivities(string accessToken, int id, DateTime today)
        {
            var user = context.GetUserById(id);

            if (user == null || user.MonthSummaries == null || user.MonthSummaries.Count == 0)
            {
                var allUserActivities     = stravaClient.getAllUserActivities(accessToken, id);
                var monthSummaryActivites = convertToMonthSummaryActivities(allUserActivities);

                user = new User()
                {
                    UserId         = id,
                    LastDownload   = DateTime.Now.Date,
                    MonthSummaries = monthSummaryActivites.ToList(),
                };
                context.Add(user);
                context.SaveChanges();
            }
            else
            {
                var userActivities        = stravaClient.getUserActivitiesByIdAfter(accessToken, user, user.LastDownload);
                var monthSummaryActivites = convertToMonthSummaryActivities(userActivities);

                if (monthSummaryActivites != null)
                {
                    user.LastDownload = DateTime.Now.Date;
                    foreach (var activity in monthSummaryActivites)
                    {
                        if (!context.Contains(activity))
                        {
                            context.Add(activity);
                            user.MonthSummaries.Add(activity);
                        }
                    }
                    context.SaveChanges();
                }
            }

            var activitiesThisMonth = from activity in user.MonthSummaries
                                      where activity.StartDate.Value.Month == today.Month &&
                                      activity.StartDate.Value.Year == today.Year
                                      select activity;

            if (activitiesThisMonth == null || activitiesThisMonth.ToList().Count == 0)
            {
                return(null);
            }

            return(activitiesThisMonth);
        }
예제 #5
0
        public void Setup()
        {
            accessToken  = "access_token";
            stravaId     = 123;
            stravaClient = Substitute.For <IStravaClient>();;
            context      = Substitute.For <ISummaryRepository>();

            //Mock stravaClient
            IEnumerable <VisualActivity> activities        = TestData.VisualActivitiesList();
            IEnumerable <VisualActivity> newUserActivities = TestData.NewVisualActivitiesList();

            stravaClient.getAllUserActivities(accessToken, 123).Returns(activities);
            stravaClient.getAllUserActivities(accessToken, 2222).Returns(newUserActivities);
            stravaClient.getUserActivitiesByIdAfter(accessToken, Arg.Any <User>(), Arg.Any <DateTime>()).Returns(newUserActivities);

            //Mock repository
            var stravaUser = new User {
                UserId = 123, MonthSummaries = TestData.MonthSummaryActivityList()
            };

            context = Substitute.For <ISummaryRepository>();
            context.GetUserById(123).Returns(stravaUser);
            context.GetUserById(2222).Returns(new User());
        }