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); }
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); }
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); }
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(); }
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(); }
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); }
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); }
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(); }
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); }
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); }
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); }
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); }
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); }
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(); }
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(); }
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); }
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); }
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(); }