Exemplo n.º 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());
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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());
        }