예제 #1
0
        public async Task EFUnitOfWorkTests_ShouldCreateNewFlightAsynchronousily()
        {
            // Arrange
            Flight newFlight = new Flight
            {
                FlightNumber         = "FakeFlight_" + 10,
                AirportDepartureId   = 1,
                AirportDestinationId = 2,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };
            // Act
            var context = new EFUnitOfWork(_contextBuilder.Options);

            context.CreateSet <Flight>().Add(newFlight);
            await context.CommitAsync();


            Flight currentCreatedRecord = context.CreateSet <Flight>().Find(newFlight.Id);


            // Assert
            Check.That(currentCreatedRecord.FlightNumber).IsEqualTo(newFlight.FlightNumber);
            Check.That(currentCreatedRecord.AirportDepartureId).IsEqualTo(newFlight.AirportDepartureId);
            Check.That(currentCreatedRecord.AirportDestinationId).IsEqualTo(newFlight.AirportDestinationId);
            Check.That(currentCreatedRecord.AircraftId).IsEqualTo(newFlight.AircraftId);
            Check.That(currentCreatedRecord.FlightDate).IsEqualTo(newFlight.FlightDate);
            Check.That(currentCreatedRecord.FlightTime).IsEqualTo(newFlight.FlightTime);
        }
예제 #2
0
        public async Task EFUnitOfWorkTests_ShouldCreateNewAircraftAsynchronousily()
        {
            // Arrange
            var      random      = new Random();
            Aircraft newAircraft = new Aircraft
            {
                Code              = "FakeAircraft_11",
                MilesPerHour      = random.Next(100, 300),
                TakeOffEffort     = random.Next(30, 50),
                ConsumptionKgPerH = random.Next(300, 800)
            };

            // Act
            var context = new EFUnitOfWork(_contextBuilder.Options);

            context.CreateSet <Aircraft>().Add(newAircraft);
            await context.CommitAsync();

            Aircraft currentCreatedRecord = context.CreateSet <Aircraft>().Find(newAircraft.Id);

            // Assert
            Check.That(currentCreatedRecord.Code).IsEqualTo(newAircraft.Code);
            Check.That(currentCreatedRecord.MilesPerHour).IsEqualTo(newAircraft.MilesPerHour);
            Check.That(currentCreatedRecord.TakeOffEffort).IsEqualTo(newAircraft.TakeOffEffort);
            Check.That(currentCreatedRecord.ConsumptionKgPerH).IsEqualTo(newAircraft.ConsumptionKgPerH);
        }
예제 #3
0
        public void EFUnitOfWorkTests_GetFlight_ShouldReturnAFlightWhenFlightIdExistsInDatabase()
        {
            // Arrange
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Expected
            var expectedFlight = _flightsRecords.First();

            // Act
            var flight = context.CreateSet <Flight>().Find(expectedFlight.Id);

            // Assert
            Check.That(flight).IsNotNull();

            Check.That(flight.CreationDate).IsEqualTo(expectedFlight.CreationDate);
            Check.That(flight.Distance).IsEqualTo(expectedFlight.Distance);
            Check.That(flight.EstimatedArrivalTime).IsEqualTo(expectedFlight.EstimatedArrivalTime);
            Check.That(flight.EstimatedFlightDuration).IsEqualTo(expectedFlight.EstimatedFlightDuration);
            Check.That(flight.EstimatedFuelNeeded).IsEqualTo(expectedFlight.EstimatedFuelNeeded);
            Check.That(flight.FlightNumber).IsEqualTo(expectedFlight.FlightNumber);
            Check.That(flight.FlightDate).IsEqualTo(expectedFlight.FlightDate);
            Check.That(flight.FlightTime).IsEqualTo(expectedFlight.FlightTime);

            Check.That(flight.Departure).HasFieldsWithSameValues(expectedFlight.Departure);
            Check.That(flight.Destination).HasFieldsWithSameValues(expectedFlight.Destination);
            Check.That(flight.Aircraft).HasFieldsWithSameValues(expectedFlight.Aircraft);
        }
예제 #4
0
        public async Task EFUnitOfWorkTests_Delete_Asynchronousily_ShouldDeleteAnExistingFlight()
        {
            // Arrange
            var context        = new EFUnitOfWork(_contextBuilder.Options);
            var flightToDelete = context.CreateSet <Flight>().Last();

            // Expected

            // Act
            EntityEntry <Flight> deletedFlight = context.CreateSet <Flight>().Remove(flightToDelete);
            await context.SaveChangesAsync();

            var exists = context.CreateSet <Flight>().Where(e => e.Id.Equals(flightToDelete.Id)).Any();

            // Assert
            Check.That(deletedFlight.State).IsEqualTo(EntityState.Detached);
            Check.That(exists).IsFalse();
        }
예제 #5
0
        public void EFUnitOfWorkTests_GetAirport_ShouldReturnNullWhenAirportIdDoesNotExistInDatabase()
        {
            // Arrange
            var context   = new EFUnitOfWork(_contextBuilder.Options);
            var airportId = 19977777;

            // Act
            var airport = context.CreateSet <Airport>().Find(airportId);

            // Assert
            Check.That(airport).IsNull();
        }
예제 #6
0
        public void EFUnitOfWorkTests_CreateSet_ShouldInstanciateAndReturnAirportDbSet()
        {
            // Arrange
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Act
            var airportsDbset = context.CreateSet <Airport>();

            // Assert
            Check.That(airportsDbset).IsNotNull();
            Check.That(airportsDbset.Count()).IsEqualTo(_airportsRecords.Count);
        }
예제 #7
0
        public async Task EFUnitOfWorkTests_ShouldReturnAllFlightsRecordsAsynchronousily()
        {
            // Act
            List <Flight> records = null;
            var           context = new EFUnitOfWork(_contextBuilder.Options);

            records = await context.CreateSet <Flight>().ToListAsync();

            // Assert
            Check.That(records).IsNotNull();
            Check.That(records.Count).IsEqualTo(_flightsRecords.Count);
        }
예제 #8
0
        public async Task EFUnitOfWorkTests_GetAircraft_Asynchronously_ShouldReturnNullWhenAircraftIdDoesNotExistInDatabase()
        {
            // Arrange
            var context    = new EFUnitOfWork(_contextBuilder.Options);
            var aircraftId = 19977777;

            // Act
            var aircraft = await context.CreateSet <Aircraft>().FindAsync(aircraftId);

            // Assert
            Check.That(aircraft).IsNull();
        }
예제 #9
0
        public async Task EFUnitOfWorkTests_Update_Asynchronousily_ShouldUpdateAnExistingFlight()
        {
            // Arrange
            var context        = new EFUnitOfWork(_contextBuilder.Options);
            var flightToUpdate = context.CreateSet <Flight>().First();

            flightToUpdate.FlightNumber         = "NewFlightNumber";
            flightToUpdate.AirportDepartureId   = 4;
            flightToUpdate.AirportDestinationId = 5;
            flightToUpdate.AircraftId           = 2;

            // Expected
            var expectedFlight = context.CreateSet <Flight>().First();

            // Act
            var currentEntity = context.CreateSet <Flight>().Update(flightToUpdate).Entity;
            await context.SaveChangesAsync();

            // Assert
            Check.That(currentEntity).IsSameReferenceAs(expectedFlight);
            Check.That(currentEntity).HasSameValueAs(expectedFlight);
        }
예제 #10
0
        public void EFUnitOfWorkTests_ShouldReturnAllAircraftsRecords()
        {
            // Act
            List <Aircraft> records = null;
            var             context = new EFUnitOfWork(_contextBuilder.Options);

            records = context.CreateSet <Aircraft>().ToList();


            // Assert
            Check.That(records).IsNotNull();
            Check.That(records.Count()).IsEqualTo(_aircraftsRecords.Count);
        }
예제 #11
0
        public async Task EFUnitOfWorkTests_ShouldCreateNewAirportAsynchronousily()
        {
            // Arrange
            var     random     = new Random();
            Airport newAirport = new Airport
            {
                Name      = "FakeAirport_10",
                Latitude  = random.Next(1000, 99999),
                Longitude = random.Next(1000, 99999)
            };
            // Act
            var context = new EFUnitOfWork(_contextBuilder.Options);

            context.CreateSet <Airport>().Add(newAirport);
            await context.CommitAsync();

            Airport currentCreatedRecord = context.CreateSet <Airport>().Find(newAirport.Id);

            // Assert
            Check.That(currentCreatedRecord.Name).IsEqualTo(newAirport.Name);
            Check.That(currentCreatedRecord.Latitude).IsEqualTo(newAirport.Latitude);
            Check.That(currentCreatedRecord.Longitude).IsEqualTo(newAirport.Longitude);
        }
예제 #12
0
        public async Task EFUnitOfWorkTests_GetAircraft_Asynchronously_ShouldReturnAnAircraftWhenAircraftIdExistsInDatabase()
        {
            // Arrange
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Expected
            var expectedAircraft = _aircraftsRecords.First();

            // Act
            var currentAircraft = await context.CreateSet <Aircraft>().FindAsync(expectedAircraft.Id);

            // Assert
            Check.That(currentAircraft).IsNotNull();
            Check.That(currentAircraft).HasFieldsWithSameValues(expectedAircraft);
        }
예제 #13
0
        public void EFUnitOfWorkTests_GetAirport_ShouldReturnAnAirportWhenAirportIdExistsInDatabase()
        {
            // Arrange
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Expected
            var expectedAirport = _airportsRecords.First();

            // Act
            var currentAirport = context.CreateSet <Airport>().Find(expectedAirport.Id);

            // Assert
            Check.That(currentAirport).IsNotNull();
            Check.That(currentAirport).HasFieldsWithSameValues(expectedAirport);
        }
예제 #14
0
        public void EFUnitOfWorkTests_ShouldNotFindAnyAircraftsRecords()
        {
            //Arrange && Expected
            var context      = new EFUnitOfWork(_contextBuilder.Options);
            var aircraftCode = "AnyAircraft";

            Expression <Func <Aircraft, bool> > filter = (Aircraft a) =>
                                                         (a.Code == aircraftCode);

            // Act
            var foundAircrafts = context.CreateSet <Aircraft>().Where(filter).ToList();

            // Assert
            Check.That(foundAircrafts).IsNotNull();
            Check.That(foundAircrafts).IsEmpty();
        }
예제 #15
0
        public void EFUnitOfWorkTests_ShouldNotFindAnyFlightsRecords()
        {
            //Arrange && Expected
            var context      = new EFUnitOfWork(_contextBuilder.Options);
            var flightNumber = "AnyFlight";

            Expression <Func <Flight, bool> > filter = (Flight f) =>
                                                       (f.FlightNumber == flightNumber);

            // Act
            var foundFlights = context.CreateSet <Flight>().Where(filter).ToList();

            // Assert
            Check.That(foundFlights).IsNotNull();
            Check.That(foundFlights).IsEmpty();
        }
예제 #16
0
        public void EFUnitOfWorkTests_ShouldFindOnlyOneFlightRecordByFlightNumber()
        {
            //Arrange && Expected
            var context        = new EFUnitOfWork(_contextBuilder.Options);
            var expectedFlight = _flightsRecords.First();

            Expression <Func <Flight, bool> > filter = (Flight f) =>
                                                       (f.FlightNumber == expectedFlight.FlightNumber);

            // Act
            var foundFlights = context.CreateSet <Flight>().Where(filter).ToList();

            // Assert
            Check.That(foundFlights).IsNotNull();
            Check.That(foundFlights.Count()).Equals(1);
            Check.That(foundFlights.First().Id).Equals(expectedFlight.Id);
            Check.That(foundFlights.First().FlightNumber).Equals(expectedFlight.FlightNumber);
        }
예제 #17
0
        public void EFUnitOfWorkTests_ShouldFindSomeAircraftsRecords()
        {
            //Arrange && Expected
            var context          = new EFUnitOfWork(_contextBuilder.Options);
            var expectedAircraft = _aircraftsRecords.First();

            Expression <Func <Aircraft, bool> > filter = (Aircraft a) =>
                                                         (a.Code == expectedAircraft.Code);

            // Act
            var foundAircrafts = context.CreateSet <Aircraft>().Where(filter).ToList();

            // Assert
            Check.That(foundAircrafts).IsNotNull();
            Check.That(foundAircrafts.Count()).Equals(1);
            Check.That(foundAircrafts.First().Id).Equals(expectedAircraft.Id);
            Check.That(foundAircrafts.First().Code).Equals(expectedAircraft.Code);
        }
예제 #18
0
        private void Seed()
        {
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Ensure to clear data if exists
            if (context.CreateSet <Flight>().Count() > 0)
            {
                context.CreateSet <Flight>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }

            if (context.CreateSet <Aircraft>().Count() > 0)
            {
                context.CreateSet <Aircraft>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }

            if (context.CreateSet <Airport>().Count() > 0)
            {
                context.CreateSet <Airport>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }
            // Create Fake Records

            Random random = new Random();

            _airportsRecords  = new List <Airport>();
            _aircraftsRecords = new List <Aircraft>();
            _flightsRecords   = new List <Flight>();

            for (int i = 1; i < 10; i++)
            {
                _airportsRecords.Add(new Airport
                {
                    Name      = "FakeAirport_" + i,
                    Latitude  = random.Next(1000, 99999),
                    Longitude = random.Next(1000, 99999)
                });
            }

            for (int i = 1; i < 10; i++)
            {
                _aircraftsRecords.Add(new Aircraft
                {
                    Code              = "FakeAircraft_" + i,
                    MilesPerHour      = random.Next(100, 300),
                    TakeOffEffort     = random.Next(30, 50),
                    ConsumptionKgPerH = random.Next(300, 800)
                });
            }

            for (int i = 1; i < 10; i++)
            {
                _flightsRecords.Add(new Flight
                {
                    FlightNumber         = "FakeFlight_" + i,
                    AirportDepartureId   = i,
                    AirportDestinationId = i + 1,
                    AircraftId           = i,
                    FlightDate           = DateTime.Now,
                    FlightTime           = DateTime.Now
                });
            }

            context.AddRange(_airportsRecords);
            context.AddRange(_aircraftsRecords);
            context.AddRange(_flightsRecords);
            context.SaveChanges();
        }