public void CreateTrip_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "createTripCommit", organizer: "aUser"); var userTripRepo = new UserTripRepository(); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var dbTrip = services.GetTrip(trip.Id); Assert.IsNotNull(dbTrip); Assert.IsNotNull(dbTrip.TripPot); var pot = dbTrip.TripPot; Assert.AreEqual(trip.Price, pot.TargetAmount); Assert.AreEqual(1, dbTrip.Participants.Count()); var ptp = dbTrip.Participants.First(); Assert.AreEqual(dbTrip.Id, ptp.TripId); Assert.AreEqual(1, pot.Participants.Count()); var potUser = pot.Participants.First(); Assert.AreEqual(pot.Id, potUser.PotId); var userTrips = userTripRepo.GetUserTripsByTrip(trip.TripName); Assert.IsFalse(userTripRepo.HasErrors); Assert.AreEqual(1, userTrips.Count()); var userTrip = userTrips.First(); Assert.AreEqual(trip.TripName, userTrip.TripName); }
public void UpdatePrice_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "UpdatePriceValid", 1000, maxNbPeople: 30); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); services.UpdatePrice(reloadedTrip, 2000.26); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(2000.26, reloadedTrip.Price); Assert.IsNotNull(reloadedTrip.TripPot); Assert.AreEqual(2000.26, reloadedTrip.TripPot.TargetAmount); var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1); Assert.IsNotNull(potUser); Assert.AreEqual(2000.26 / reloadedTrip.NumberMaxOfPeople, potUser.TargetAmount); var userTrip = new UserTripRepository().GetUserTrip(1, trip.TripName); Assert.IsNotNull(userTrip); Assert.AreEqual(2000.26, userTrip.TripAmount); }
public void UpdatePrice_WhenError_ShouldRollback() { var trip = ModelTestHelper.CreateTrip(-1, "UpdatePriceError", 1000, maxNbPeople: 30); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1, reloadedTrip.Participants.Count()); var mockTripRepo = new Mock <ITripRepository>(); mockTripRepo.Setup(s => s.UpdateTrip(reloadedTrip)).Callback(() => new TripRepository().UpdateTrip(reloadedTrip)); mockTripRepo.SetupGet(s => s.HasErrors).Returns(true); mockTripRepo.SetupGet(s => s.Errors).Returns(new List <string>()); var secondServices = new TripServices(mockTripRepo.Object, new PotRepository(), new TripParticipantRepository(), new UserTripRepository(), new PotUserRepository()); secondServices.UpdatePrice(trip, 2000.26); Assert.IsTrue(secondServices.HasErrors); reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); Assert.AreEqual(1000, reloadedTrip.Price); Assert.IsNotNull(reloadedTrip.TripPot); Assert.AreEqual(1000, reloadedTrip.TripPot.TargetAmount); var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1); Assert.IsNotNull(potUser); Assert.AreEqual(1000 / (double)reloadedTrip.NumberMaxOfPeople, potUser.TargetAmount); var userTrip = new UserTripRepository().GetUserTrip(1, trip.TripName); Assert.IsNotNull(userTrip); Assert.AreEqual(1000, userTrip.TripAmount); }
public void DeleteTrip_WhenValid_ShouldCommit() { var trip = ModelTestHelper.CreateTrip(-1, "deleteTripCommit"); var services = new TripServices(); services.CreateTrip(trip, 1); Assert.IsFalse(services.HasErrors); var reloadedTrip = services.GetTrip(trip.Id); Assert.IsNotNull(reloadedTrip); var pot = reloadedTrip.TripPot; Assert.IsNotNull(pot); var participants = reloadedTrip.Participants; Assert.AreEqual(1, participants.Count()); var potUsers = pot.Participants; Assert.AreEqual(1, potUsers.Count()); var tripRepo = new TripRepository(); var potRepo = new PotRepository(); var userTripRepo = new UserTripRepository(); var potUserRepo = new PotUserRepository(); var tripParticipantRepo = new TripParticipantRepository(); services.DeleteTrip(reloadedTrip); Assert.IsFalse(services.HasErrors); Assert.IsNull(tripRepo.GetTrip(reloadedTrip.Id)); Assert.IsNull(potRepo.GetPot(pot.Id)); Assert.AreEqual(0, userTripRepo.GetUserTripsByTrip(reloadedTrip.TripName).Count()); Assert.AreEqual(0, potUserRepo.GetPotUsers(pot.Id).Count()); Assert.AreEqual(0, tripParticipantRepo.GetTripParticipants(reloadedTrip.Id).Count()); }
public void Delete_WhenErrorDuringDelete_ShouldRollBack() { var userRepo = new UserRepository(); var friendshipRepo = new FriendshipRepository(); var tripRepo = new UserTripRepository(); var user = ModelTestHelper.CreateUser(1, "DeleteUser"); var pwd = user.Password; userRepo.SaveUser(user); Assert.IsFalse(userRepo.HasErrors); var secondUser = ModelTestHelper.CreateUser(2, "AFriend", "AFriendMail"); userRepo.SaveUser(secondUser); Assert.IsFalse(userRepo.HasErrors); var friendship = ModelTestHelper.CreateFriendship(user.Id, secondUser.Pseudo); var otherFriendship = ModelTestHelper.CreateFriendship(secondUser.Id, user.Pseudo); friendshipRepo.SaveFriendship(friendship); friendshipRepo.SaveFriendship(otherFriendship); Assert.IsFalse(friendshipRepo.HasErrors); user.AddFriend(friendship); secondUser.AddFriend(otherFriendship); var trip = ModelTestHelper.CreateUserTrip(user.Id, "ATrip"); tripRepo.SaveUserTrip(trip); Assert.IsFalse(tripRepo.HasErrors); user.AddTrip(trip); var mock = new Mock <IUserTripRepository>(); mock.Setup(s => s.DeleteUserTrip(trip)).Callback(() => tripRepo.DeleteUserTrip(trip)); mock.SetupGet(s => s.HasErrors).Returns(true); var service = new UserServices(new UserRepository(), mock.Object, new FriendshipRepository()); service.DeleteUser(user); service = new UserServices(); var dbUser = service.LoginByPseudo(user.Pseudo, pwd); Assert.IsNotNull(dbUser); Assert.AreEqual(1, dbUser.Friends.Count()); Assert.AreEqual(1, dbUser.Trips.Count()); Assert.AreEqual(1, service.GetUserTrips(user.Id).Count()); Assert.AreEqual(1, service.GetUserFriendships(user.Id).Count()); Assert.AreEqual(1, service.GetUserFriendships(secondUser.Id).Count(f => f.FriendName == user.Pseudo)); }
public void Delete_WhenValid_ShouldCommit() { var userRepo = new UserRepository(); var friendshipRepo = new FriendshipRepository(); var tripRepo = new UserTripRepository(); var user = ModelTestHelper.CreateUser(1, "DeleteUser"); userRepo.SaveUser(user); Assert.IsFalse(userRepo.HasErrors); var secondUser = ModelTestHelper.CreateUser(2, "AFriend", "AFriendMail"); userRepo.SaveUser(secondUser); Assert.IsFalse(userRepo.HasErrors); var friendship = ModelTestHelper.CreateFriendship(user.Id, secondUser.Pseudo); var otherFriendship = ModelTestHelper.CreateFriendship(secondUser.Id, user.Pseudo); friendshipRepo.SaveFriendship(friendship); friendshipRepo.SaveFriendship(otherFriendship); Assert.IsFalse(friendshipRepo.HasErrors); user.AddFriend(friendship); secondUser.AddFriend(otherFriendship); var trip = ModelTestHelper.CreateUserTrip(user.Id, "ATrip"); tripRepo.SaveUserTrip(trip); Assert.IsFalse(tripRepo.HasErrors); user.AddTrip(trip); var service = new UserServices(); service.DeleteUser(user); Assert.IsNull(service.GetUserInfo(user.Pseudo)); Assert.AreEqual(0, service.GetUserTrips(user.Id).Count()); Assert.AreEqual(0, service.GetUserFriendships(user.Id).Count()); Assert.AreEqual(0, service.GetUserFriendships(secondUser.Id).Count(f => f.FriendName == user.Pseudo)); }