Exemplo n.º 1
0
        public async Task GivenExistingBooking_WhenUpdatedWithExtraCabinOfWrongSubCruise_ShouldFail()
        {
            var repository = GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var source = GetBookingForTest(
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetMultiplePaxForTest(4)));

            var result = await repository.CreateAsync(cruise, source);

            source = GetBookingForTest(
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetMultiplePaxForTest(4)),
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeBId, GetMultiplePaxForTest(4)));
            source.Reference = result.Reference;

            try
            {
                await repository.UpdateAsync(cruise, source);

                Assert.Fail("Update booking did not throw.");
            }
            catch (BookingException)
            {
            }
        }
Exemplo n.º 2
0
        public async Task GivenExistingBooking_WhenItIsLocked_ShouldFailToUpdate()
        {
            var repository = GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var booking = await GetNewlyCreatedBookingForTestAsync(cruise, repository);

            Assert.IsFalse(booking.IsLocked);

            booking.IsLocked = true;
            await repository.UpdateIsLockedAsync(booking);

            var updateSource = GetBookingForTest(GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest(firstName: "Förnamn1", lastName: "Efternamn1")));

            updateSource.Reference = booking.Reference;
            try
            {
                await repository.UpdateAsync(cruise, updateSource);

                Assert.Fail("Update booking did not throw.");
            }
            catch (BookingException)
            {
            }
        }
        public async Task GivenExistingBooking_WhenDeleted_SubTablesShouldBeCleared()
        {
            var source = BookingRepositoryTest.GetSimpleBookingForTest();

            source.Products.Add(BookingRepositoryTest.GetProductForTest());

            var repository = BookingRepositoryTest.GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var result = await repository.CreateAsync(cruise, source);

            var booking = await repository.FindByReferenceAsync(result.Reference);

            var paymentRepository = AecPaymentRepositoryTest.GetPaymentRepositoryForTest();
            await paymentRepository.CreateAsync(booking, 123.45m);

            await repository.DeleteAsync(booking);

            // The booking now has cabins, pax, products and a payment
            // If we add more data in subtables remember to add and check for it here
            // All relevant tables should be empty now

            using (var db = DbUtil.Open())
            {
                Assert.AreEqual(0, db.ExecuteScalar <int>("select count(*) from [Booking]"));
                Assert.AreEqual(0, db.ExecuteScalar <int>("select count(*) from [BookingCabin]"));
                Assert.AreEqual(0, db.ExecuteScalar <int>("select count(*) from [BookingPax]"));
                Assert.AreEqual(0, db.ExecuteScalar <int>("select count(*) from [BookingPayment]"));
                Assert.AreEqual(0, db.ExecuteScalar <int>("select count(*) from [BookingProduct]"));
            }
        }
        public async Task GivenExistingBooking_WhenDeleted_ShouldLeaveRecordInDeletedBookings()
        {
            var repository = BookingRepositoryTest.GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var booking = await BookingRepositoryTest.GetNewlyCreatedBookingForTestAsync(cruise, repository);

            var           paymentRepository = AecPaymentRepositoryTest.GetPaymentRepositoryForTest();
            const decimal amountPaid        = 10m;
            await paymentRepository.CreateAsync(booking, amountPaid);

            await repository.DeleteAsync(booking);

            var deletedBookingRepository = GetDeletedBookingRepositoryForTest();
            var deleted = (await deletedBookingRepository.FindByReferenceAsync(booking.Reference)).FirstOrDefault();

            Assert.IsNotNull(deleted);
            Assert.AreEqual(booking.CruiseId, deleted.CruiseId);
            Assert.AreEqual(booking.Reference, deleted.Reference);
            Assert.AreEqual(booking.FirstName, deleted.FirstName);
            Assert.AreEqual(booking.LastName, deleted.LastName);
            Assert.AreEqual(booking.Email, deleted.Email);
            Assert.AreEqual(booking.PhoneNo, deleted.PhoneNo);
            Assert.AreEqual(booking.TotalPrice, deleted.TotalPrice);
            Assert.AreEqual(amountPaid, deleted.AmountPaid);
        }
Exemplo n.º 5
0
        public async Task GivenExistingBooking_WhenUpdatedWithTooManyCabins_ShouldPreserveExistingBooking()
        {
            Assert.AreEqual(10, Config.NumberOfCabins);

            var cruise = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var repository = GetBookingRepositoryForTest();

            // First create a booking of 9/10 cabins
            var nineOutOfTenCabins = new BookingSource.Cabin[9];

            for (int i = 0; i < nineOutOfTenCabins.Length; i++)
            {
                nineOutOfTenCabins[i] = GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest());
            }
            var bookingThatFillsNineOutOfTenCabins = GetBookingForTest(nineOutOfTenCabins);
            await repository.CreateAsync(cruise, bookingThatFillsNineOutOfTenCabins);

            // Then create a booking for the 10th cabin
            var source = GetBookingForTest(GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest(firstName: "Test1", lastName: "Test2")));
            var result = await repository.CreateAsync(cruise, source);

            var booking = await repository.FindByReferenceAsync(result.Reference);

            var cabins = await repository.GetCabinsForBookingAsync(booking);

            Assert.AreEqual(1, cabins.Length);

            // Now try to update it with two bookings
            source = GetBookingForTest(
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest()),
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest())
                );
            source.Reference = result.Reference;

            try
            {
                result = await repository.UpdateAsync(cruise, source);

                Assert.Fail("Update booking did not throw.");
            }
            catch (AvailabilityException)
            {
            }

            // Check to make sure it is still there
            booking = await repository.FindByReferenceAsync(result.Reference);

            cabins = await repository.GetCabinsForBookingAsync(booking);

            Assert.AreEqual(1, cabins.Length);

            var pax = cabins[0].Pax[0];

            Assert.AreEqual(pax.FirstName, "Test1");
            Assert.AreEqual(pax.LastName, "Test2");
        }
Exemplo n.º 6
0
        static async Task <BookingResult> CreateBookingFromSource(BookingSource source, BookingRepository repository = null)
        {
            if (null == repository)
            {
                repository = GetBookingRepositoryForTest();
            }

            var cruise = await CruiseRepositoryTest.GetCruiseForTestAsync();

            return(await repository.CreateAsync(cruise, source));
        }
        public async Task GivenExistingBooking_WhenDeleted_ShouldCeaseToExist()
        {
            var repository = BookingRepositoryTest.GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var booking = await BookingRepositoryTest.GetNewlyCreatedBookingForTestAsync(cruise, repository);

            Assert.AreNotEqual(Guid.Empty, booking.Id);
            await repository.DeleteAsync(booking);

            var deletedBooking = await repository.FindByReferenceAsync(booking.Reference);

            Assert.IsNull(deletedBooking);

            deletedBooking = await repository.FindByIdAsync(booking.Id);

            Assert.IsNull(deletedBooking);
        }
Exemplo n.º 8
0
        public async Task GivenExistingBooking_WhenUpdatedWithValidData_ShouldUpdateBooking()
        {
            var repository = GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            // Create a booking with one cabin
            var source = GetBookingForTest(GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest(firstName: "Förnamn1", lastName: "Efternamn1")));
            var result = await repository.CreateAsync(cruise, source);

            var booking = await repository.FindByReferenceAsync(result.Reference);

            var cabins = await repository.GetCabinsForBookingAsync(booking);

            var pax = cabins[0].Pax[0];

            Assert.AreEqual(pax.FirstName, "Förnamn1");
            Assert.AreEqual(pax.LastName, "Efternamn1");

            // Now update it to have two cabins
            source = GetBookingForTest(
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest(firstName: "Förnamn2", lastName: "Efternamn2")),
                GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest(firstName: "Förnamn3", lastName: "Efternamn3"))
                );
            source.Reference = result.Reference;

            result = await repository.UpdateAsync(cruise, source);

            Assert.AreEqual(booking.Reference, result.Reference);

            booking = await repository.FindByReferenceAsync(result.Reference);

            cabins = await repository.GetCabinsForBookingAsync(booking);

            pax = cabins[0].Pax[0];
            Assert.AreEqual(pax.FirstName, "Förnamn2");
            Assert.AreEqual(pax.LastName, "Efternamn2");

            pax = cabins[1].Pax[0];
            Assert.AreEqual(pax.FirstName, "Förnamn3");
            Assert.AreEqual(pax.LastName, "Efternamn3");
        }
Exemplo n.º 9
0
        public async Task GivenMultipleConcurrentBookings_ShouldNotExceedAvailableCabins()
        {
            var repository = GetBookingRepositoryForTest();
            var cruise     = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var source = GetSimpleBookingForTest();
            var tasks  = new List <Task>();

            const int overload = 10;

            for (int i = 0; i < Config.NumberOfCabins + overload; i++)
            {
                tasks.Add(repository.CreateAsync(cruise, source));
            }

            var combinedTask = Task.WhenAll(tasks);

            try
            {
                await combinedTask;
            }
            catch
            {
                // we expect a number of tasks to fail because the cabins ran out
                Assert.IsNotNull(combinedTask.Exception, "Expected some tasks to fail.");
                Assert.AreEqual(overload, combinedTask.Exception.InnerExceptions.Count, "Number of failed tasks does not match expected.");

                foreach (Exception ex in combinedTask.Exception.InnerExceptions)
                {
                    Assert.AreEqual(typeof(AvailabilityException), ex.GetType(), $"A task failed for an unexpected reason, got {ex.GetType()} instead of {nameof(AvailabilityException)}.");
                }
            }

            using (var db = DbUtil.Open())
            {
                var numberOfCabins = await db.ExecuteScalarAsync <int>("select count(*) from [BookingCabin]");

                Assert.AreEqual(Config.NumberOfCabins, numberOfCabins, "Wrong number of cabins were booked, found {0}, expected {1}.", numberOfCabins, Config.NumberOfCabins);
            }
        }
Exemplo n.º 10
0
        public async Task GivenExistingBooking_WhenUpdatedWithMoreCabins_ShouldAllowUpToMaximumCapacity()
        {
            Assert.AreEqual(10, Config.NumberOfCabins);

            var cruise = await CruiseRepositoryTest.GetCruiseForTestAsync();

            var repository = GetBookingRepositoryForTest();

            // First create a booking of 8/10 cabins
            var eightOutOfTenCabins = new BookingSource.Cabin[8];

            for (int i = 0; i < eightOutOfTenCabins.Length; i++)
            {
                eightOutOfTenCabins[i] = GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest());
            }

            var booking = GetBookingForTest(eightOutOfTenCabins);
            var result  = await repository.CreateAsync(cruise, booking);

            // Now update it to 9/10 cabins
            var nineOutOfTenCabins = new BookingSource.Cabin[9];

            for (int i = 0; i < nineOutOfTenCabins.Length; i++)
            {
                nineOutOfTenCabins[i] = GetCabinForTest(SjoslagetDbExtensions.CabinTypeId, GetPaxForTest());
            }

            booking           = GetBookingForTest(nineOutOfTenCabins);
            booking.Reference = result.Reference;
            result            = await repository.UpdateAsync(cruise, booking);

            var savedBooking = await repository.FindByReferenceAsync(result.Reference);

            var cabins = await repository.GetCabinsForBookingAsync(savedBooking);

            Assert.AreEqual(9, cabins.Length);
        }
Exemplo n.º 11
0
        internal static async Task <Booking> GetNewlyCreatedBookingForTestAsync()
        {
            var cruise = await CruiseRepositoryTest.GetCruiseForTestAsync();

            return(await GetNewlyCreatedBookingForTestAsync(cruise, GetBookingRepositoryForTest()));
        }