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