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()); }
private IEnumerable <Coordinates> getUserCoordinates(string accessToken, int id) { var user = context.GetUserById(id); if (user == null || user.StartCoordinates == null || user.StartCoordinates.Count == 0) { IEnumerable <Coordinates> coordinates = stravaClient.getAllUserCoordinatesById(accessToken, id); //Activities without a valid location cannot be mapped coordinates = from c in coordinates where !(c.Longitude == null || c.Longitude == null) select c; if (user == null) { user = new User() { StartCoordinates = coordinates.ToList(), UserId = id, LastDownload = DateTime.Now.Date }; context.Add(user); context.SaveChanges(); } else { //Update UserId if it is not valid if (user.UserId <= 0 && user.UserId != id) { user.UserId = id; } addCoordinates(user, coordinates); } } else { var latestCoordinates = stravaClient.getUserCoordinatesByIdAfter(accessToken, user.LastDownload); addCoordinates(user, latestCoordinates); } return(user.StartCoordinates); }