示例#1
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");
        }
示例#2
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);
            }
        }
示例#3
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);
        }
示例#4
0
        internal static async Task <Booking> GetNewlyCreatedBookingForTestAsync()
        {
            var cruise = await CruiseRepositoryTest.GetCruiseForTestAsync();

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