Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public void DeleteTrip_WhenError_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "deleteTripError");
            var services = new TripServices();

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.IsNotNull(reloadedTrip);
            var userTrip         = ModelTestHelper.CreateUserTrip(1, trip.TripName);
            var mockUserTripRepo = new Mock <IUserTripRepository>();

            mockUserTripRepo.Setup(s => s.GetUserTripsByTrip(trip.TripName)).Returns(new List <UserTrip> {
                userTrip
            });
            mockUserTripRepo.SetupGet(s => s.HasErrors).Returns(true);
            mockUserTripRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(),
                                                  mockUserTripRepo.Object, new PotUserRepository());

            secondServices.DeleteTrip(reloadedTrip);
            Assert.IsTrue(secondServices.HasErrors);
            reloadedTrip = services.GetTrip(trip.Id);
            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(1, reloadedTrip.Participants.Count());
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        public void Quit_WhenError_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "QuitError");
            var services = new TripServices();
            var userTrip = ModelTestHelper.CreateUserTrip(2, trip.TripName);

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(1, reloadedTrip.Participants.Count());
            services.Participate(reloadedTrip, 2, "aSecondUser");
            Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors));
            var mockUserTripRepo = new Mock <IUserTripRepository>();

            mockUserTripRepo.Setup(s => s.GetUserTrip(2, reloadedTrip.TripName)).Returns(userTrip);
            mockUserTripRepo.Setup(s => s.HasErrors).Returns(true);
            mockUserTripRepo.Setup(s => s.Errors).Returns(new List <string>());
            var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(),
                                                  mockUserTripRepo.Object, new PotUserRepository());

            secondServices.Quit(reloadedTrip, 2, "aSecondUser");
            Assert.IsTrue(secondServices.HasErrors);
            Assert.AreEqual(2, services.GetParticipants(reloadedTrip.Id).Count());
        }
Exemplo n.º 7
0
        public void UpdateAllowedNumberOfPeople_WhenException_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "UpdateNbPeopleException", 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)).Throws(new Exception("ExceptionForTest"));
            var secondServices = new TripServices(mockTripRepo.Object, new PotRepository(), new TripParticipantRepository(),
                                                  new UserTripRepository(), new PotUserRepository());

            secondServices.UpdateAllowedNumberOfPeople(trip, 50);
            Assert.IsTrue(secondServices.HasErrors);
            reloadedTrip = services.GetTrip(trip.Id);
            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(30, reloadedTrip.NumberMaxOfPeople);
            var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1);

            Assert.IsNotNull(potUser);
            Assert.AreEqual(reloadedTrip.Price / 30, potUser.TargetAmount);
        }
Exemplo n.º 8
0
        public void Equals_ShouldReturnCorrectValue(int firstId, int secondId,
                                                    bool expected)
        {
            var firstTrip  = ModelTestHelper.CreateTrip(firstId, "TripName");
            var secondTrip = ModelTestHelper.CreateTrip(secondId, "SecondTrip");

            Assert.AreEqual(expected, firstTrip.Equals(secondTrip));
        }
Exemplo n.º 9
0
        public void SaveTrip_WhenTripNameIsUsed_ShouldLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsTripNameUsed(It.IsAny <string>())).Returns(true);
            var repo = CreateRepository(mock.Object);
            var trip = ModelTestHelper.CreateTrip(1, "aTrip");

            repo.SaveTrip(trip);
            CheckErrors(repo, string.Format("Trip name {0} is already used, please use another one", trip.TripName));
        }
Exemplo n.º 10
0
        public void SaveTrip_WhenDbInsertFails_ShouldLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsTripNameUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.Save(It.IsAny <Trip>())).Returns(false);
            var repo = CreateRepository(mock.Object);
            var trip = ModelTestHelper.CreateTrip(2, "aTrip");

            repo.SaveTrip(trip);
            CheckErrors(repo, SaveFail);
        }
Exemplo n.º 11
0
        public void SaveTrip_WhenValid_ShouldNotLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsTripNameUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.Save(It.IsAny <Trip>())).Returns(true);
            var repo = CreateRepository(mock.Object);
            var trip = ModelTestHelper.CreateTrip(2, "aTrip");

            repo.SaveTrip(trip);
            Assert.IsFalse(repo.HasErrors);
        }
Exemplo n.º 12
0
        public void Clone_ShoulDuplicateValues()
        {
            var pot = ModelTestHelper.CreatePot(1, 1, "cloneOrga", name: "ClonePot");

            pot.AddParticipant(ModelTestHelper.CreatePotUser(1, 1));
            pot.AddParticipant(ModelTestHelper.CreatePotUser(2, 1));
            var trip = ModelTestHelper.CreateTrip(1, "MyTrip", maxNbPeople: 20, note: 6.4);

            trip.TripPot = pot;
            trip.AddParticipant(ModelTestHelper.CreateTripParticipant(1, "toto"));
            trip.AddParticipant(ModelTestHelper.CreateTripParticipant(1, "tutu"));
            TestClone(trip);
        }
Exemplo n.º 13
0
        public void GetTripByName_ShouldReturnTrip()
        {
            var trip = ModelTestHelper.CreateTrip(4, "TripByName");
            var mock = CreateMock();

            mock.Setup(s => s.GetTripByName(It.IsAny <string>())).Returns(trip);
            var repo   = CreateRepository(mock.Object);
            var dbTrip = repo.GetTrip("TripByName");

            Assert.IsFalse(repo.HasErrors);
            Assert.IsNotNull(dbTrip);
            Assert.AreEqual(4, dbTrip.Id);
        }
Exemplo n.º 14
0
        public void Clone_WhenParticipantsIsNull_ShouldCreateNewList()
        {
            var trip = ModelTestHelper.CreateTrip(1, "testTrip");

            trip.Participants = null;
            trip.TripPot      = null;
            Assert.IsNull(trip.Participants);
            Assert.IsNull(trip.TripPot);
            var clone = trip.Clone() as Trip;

            Assert.IsNotNull(clone);
            Assert.IsNotNull(clone.Participants);
            Assert.IsNotNull(clone.TripPot);
        }
Exemplo n.º 15
0
        public void CreateTrip_WhenExceptionIsThrown_ShouldRollback()
        {
            var trip        = ModelTestHelper.CreateTrip(-1, "createTripRollback");
            var tripRepo    = new TripRepository();
            var mockPotRepo = new Mock <IPotRepository>();

            mockPotRepo.Setup(s => s.SavePot(It.IsAny <Pot>())).Throws(new Exception("RollbackException"));
            var services = new TripServices(tripRepo, mockPotRepo.Object, new TripParticipantRepository(),
                                            new UserTripRepository(), new PotUserRepository());

            services.CreateTrip(trip, 1);
            var secondServices = new TripServices();

            Assert.IsNull(secondServices.GetTrip(trip.Id));
        }
Exemplo n.º 16
0
        public void Participate_WhenValid_ShouldCommit()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "participateCommit");
            var services = new TripServices();

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.AreEqual(1, reloadedTrip.Participants.Count());
            Assert.IsNotNull(reloadedTrip);
            services.Participate(reloadedTrip, 2, "aSecondUser");
            Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors));
            Assert.AreEqual(2, services.GetParticipants(reloadedTrip.Id).Count());
            Assert.AreEqual(2, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count());
            Assert.AreEqual(2, new PotUserRepository().GetPotUsers(reloadedTrip.TripPot.Id).Count());
        }
Exemplo n.º 17
0
        public void GetValidTrips_ShouldReturnValidList()
        {
            var list = new List <Trip>
            {
                ModelTestHelper.CreateTrip(1, "Trip1"),
                ModelTestHelper.CreateTrip(2, "Trip2"),
            };
            var mock = CreateMock();

            mock.Setup(s => s.GetValidTrips(It.IsAny <DateTime>())).Returns(list);
            var repo   = CreateRepository(mock.Object);
            var dbList = repo.GetValidTrips(new DateTime());

            Assert.IsFalse(repo.HasErrors);
            Assert.AreEqual(2, dbList.Count());
            Assert.IsTrue(dbList.Any(t => t.Id == 1));
            Assert.IsTrue(dbList.Any(t => t.Id == 2));
        }
Exemplo n.º 18
0
        public void CreateTrip_WhenErrorDuringProcess_ShouldRollback()
        {
            var trip        = ModelTestHelper.CreateTrip(-1, "createTripError");
            var tripRepo    = new TripRepository();
            var potRepo     = new PotRepository();
            var mockPotRepo = new Mock <IPotRepository>();

            mockPotRepo.Setup(s => s.SavePot(It.IsAny <Pot>())).Callback(() => potRepo.SavePot(It.IsAny <Pot>()));
            mockPotRepo.SetupGet(s => s.HasErrors).Returns(true);
            mockPotRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            var services = new TripServices(tripRepo, mockPotRepo.Object, new TripParticipantRepository(),
                                            new UserTripRepository(), new PotUserRepository());

            services.CreateTrip(trip, 1);
            var secondServices = new TripServices();

            Assert.IsNull(secondServices.GetTrip(trip.Id));
        }
Exemplo n.º 19
0
        public void UpdateAllowedNumberOfPeople_WhenValid_ShouldCommit()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "UpdateNbPeopleValid", 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.UpdateAllowedNumberOfPeople(reloadedTrip, 50);
            reloadedTrip = services.GetTrip(trip.Id);
            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(50, reloadedTrip.NumberMaxOfPeople);
            var potUser = new PotUserRepository().GetPotUser(reloadedTrip.TripPot.Id, 1);

            Assert.IsNotNull(potUser);
            Assert.AreEqual(reloadedTrip.Price / 50, potUser.TargetAmount);
        }
Exemplo n.º 20
0
        public void Quit_WhenValid_ShouldCommit()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "QuitCommit", 1000);
            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.Participate(reloadedTrip, 2, "aSecondUser");
            Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors));
            Assert.IsNotNull(reloadedTrip.TripPot);
            var pot = reloadedTrip.TripPot;

            pot.CurrentAmount = 200;
            var potRepo = new PotRepository();

            potRepo.UpdatePot(pot);
            Assert.IsFalse(potRepo.HasErrors);
            var potUserRepo = new PotUserRepository();
            var potUser     = potUserRepo.GetPotUser(pot.Id, 2);

            Assert.IsFalse(potUserRepo.HasErrors);
            Assert.IsNotNull(potUser);
            potUser.Amount = 200;
            potUserRepo.UpdatePotUser(potUser);
            Assert.IsFalse(potUserRepo.HasErrors);
            services.Quit(reloadedTrip, 2, "aSecondUser");
            Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors));
            reloadedTrip = services.GetTrip(reloadedTrip.Id);
            Assert.IsFalse(services.HasErrors, string.Join(",", services.Errors));
            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(1, reloadedTrip.Participants.Count());
            pot = reloadedTrip.TripPot;
            Assert.IsNotNull(pot);
            Assert.AreEqual(0, pot.CurrentAmount);
            Assert.AreEqual(1, pot.Participants.Count());
            Assert.AreEqual(1, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count());
        }
Exemplo n.º 21
0
        public void Participate_WhenException_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "participateException");
            var services = new TripServices();

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.AreEqual(1, reloadedTrip.Participants.Count());
            Assert.IsNotNull(reloadedTrip);
            var mockUserTripRepo = new Mock <IUserTripRepository>();

            mockUserTripRepo.Setup(s => s.SaveUserTrip(It.IsAny <UserTrip>())).Throws(new Exception("Exception"));
            var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(),
                                                  mockUserTripRepo.Object, new PotUserRepository());

            secondServices.Participate(reloadedTrip, 2, "aSecondUser");
            Assert.IsTrue(secondServices.HasErrors);
            Assert.AreEqual(1, services.GetParticipants(reloadedTrip.Id).Count());
        }
Exemplo n.º 22
0
        public void DeleteTrip_WhenException_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "deleteTripException");
            var services = new TripServices();

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.IsNotNull(reloadedTrip);
            var mockUserTripRepo = new Mock <IUserTripRepository>();

            mockUserTripRepo.Setup(s => s.GetUserTripsByTrip(trip.TripName)).Throws(new Exception("Exception"));
            var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(),
                                                  mockUserTripRepo.Object, new PotUserRepository());

            secondServices.DeleteTrip(reloadedTrip);
            Assert.IsTrue(secondServices.HasErrors);
            reloadedTrip = services.GetTrip(trip.Id);
            Assert.IsNotNull(reloadedTrip);
            Assert.AreEqual(1, reloadedTrip.Participants.Count());
        }
Exemplo n.º 23
0
        public void Participate_WhenError_ShouldRollback()
        {
            var trip     = ModelTestHelper.CreateTrip(-1, "participateError");
            var services = new TripServices();

            services.CreateTrip(trip, 1);
            Assert.IsFalse(services.HasErrors);
            var reloadedTrip = services.GetTrip(trip.Id);

            Assert.AreEqual(1, reloadedTrip.Participants.Count());
            Assert.IsNotNull(reloadedTrip);
            var mockUserTripRepo = new Mock <IUserTripRepository>();

            mockUserTripRepo.Setup(s => s.SaveUserTrip(It.IsAny <UserTrip>())).Callback(() => new UserTripRepository().SaveUserTrip(It.IsAny <UserTrip>()));
            mockUserTripRepo.SetupGet(s => s.HasErrors).Returns(true);
            mockUserTripRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            var secondServices = new TripServices(new TripRepository(), new PotRepository(), new TripParticipantRepository(),
                                                  mockUserTripRepo.Object, new PotUserRepository());

            secondServices.Participate(reloadedTrip, 2, "aSecondUser");
            Assert.IsTrue(secondServices.HasErrors);
            Assert.AreEqual(1, services.GetParticipants(reloadedTrip.Id).Count());
            Assert.AreEqual(1, new UserTripRepository().GetUserTripsByTrip(reloadedTrip.TripName).Count());
        }
Exemplo n.º 24
0
 public override Trip CreateModelWithId(int id)
 {
     return(ModelTestHelper.CreateTrip(1, "tripWithId"));
 }
Exemplo n.º 25
0
 protected override Trip CreateModel()
 {
     return(ModelTestHelper.CreateTrip(1, "MyTrip"));
 }
Exemplo n.º 26
0
 public override Trip CreateModel()
 {
     return(ModelTestHelper.CreateTrip(1, "myTrip"));
 }