public async Task TestWhetherFlightGotAdded() { var repo = new FlightRepo(_context); var takeOff = new DateTime(2020, 5, 5, 8, 30, 0); var flight = CreateFlight(takeOff, "*****@*****.**"); await repo.Add(flight); var result = await(_context.Flights.Where(f => f.FlightId == flight.FlightId)).FirstOrDefaultAsync(); Assert.IsNotNull(result); }
public async System.Threading.Tasks.Task <IActionResult> ScheduleSubmitAsync(DateTime currentDate, int hours, int minutes, string Route, string Callsign, float flightSpeed, string pilotEmail) { if (pilotEmail == null) { pilotEmail = HttpContext.Session.GetString("username"); } var getPilot = _db_pilots.GetUser(pilotEmail); try { Flight flight = new Flight { FlightSpeed = flightSpeed, RouteId = Route, TimeOfFlight = currentDate.AddHours(hours).AddMinutes(minutes), PilotEmailId = pilotEmail, Pilot = (Pilot)await getPilot, Route = (Route)await _db_route.GetRoute(Route) }; await _db_flights.Add(flight); return(await UpdateScheduleViewFlightsAsync(flight.TimeOfFlight, Route, pilotEmail)); } catch (DbUpdateException) { return(Content("debug 1")); } catch (InvalidOperationException) { return(await UpdateScheduleViewFlightsAsync(currentDate, Route, pilotEmail, true)); } }
public async Task TestWhetherAddFlightDeconflictThrowsSqlException() { // ARRANGE - setup repo, make a flight and 2 others-- one that conflicts and one that doesn't var repo = new FlightRepo(_context); var takeOff = new DateTime(2019, 3, 20, 9, 30, 0); // 9:30am, March 20th, 2019 - first flight var flight = CreateFlight(takeOff); var takeOffConflictBefore = new DateTime(2019, 3, 20, 9, 20, 0); // 9:20am, March 20th, 2019 - conflicting flight (before first flight) var flightConflictBefore = CreateFlight(takeOffConflictBefore); var takeOffConflictAfter = new DateTime(2019, 3, 20, 9, 40, 0); // 9:40am, March 20th, 2019 - conflicting flight (after first flight) var flightConflictAfter = CreateFlight(takeOffConflictBefore); var takeOffNoConflict = new DateTime(2018, 3, 20, 9, 30, 0); // 9:30am, March 20th, 2018 - non-conflicting flight var flightNoConflict = CreateFlight(takeOffNoConflict); // ACT - add the flights to the db try // add first flight { await repo.Add(flight); } catch (DbUpdateException) { Assert.IsTrue(false); } // manually throw Assertion Exception catch (InvalidOperationException) { Assert.IsTrue(false); } // this Add shouldn't create conflict try // add non-conflicting flight { await repo.Add(flightNoConflict); } catch (DbUpdateException) { Assert.IsTrue(false); } // manually throw Assertion Exception catch (InvalidOperationException) { Assert.IsTrue(false); } // this Add shouldn't create conflict try // add conflicting flight before, should throw a DbUpdateException upon Add attempt { await repo.Add(flightConflictBefore); Assert.IsTrue(false); // shouldn't get here } catch (DbUpdateException) { /* Should be caught here */ } catch (InvalidOperationException) { /* And here */ } try // add conflicting flight after, should throw a DbUpdateException upon Add attempt { await repo.Add(flightConflictAfter); Assert.IsTrue(false); } catch (DbUpdateException) { /* Should be caught here */ } catch (InvalidOperationException) { /* And here */ } // get the current list of flights to be sure the conflicting flight wasn't added var resultFlights = await _context.Flights.ToListAsync(); // look for the conflicting flights in the db list -- result should be empty var resultBefore = resultFlights.Where(f => DateTime.Compare(f.TimeOfFlight, flightConflictBefore.TimeOfFlight) == 0 && flightConflictBefore.PilotEmailId == f.PilotEmailId); var resultAfter = resultFlights.Where(f => DateTime.Compare(f.TimeOfFlight, flightConflictAfter.TimeOfFlight) == 0 && flightConflictBefore.PilotEmailId == f.PilotEmailId); // ASSERT - should return no result Assert.IsEmpty(resultBefore); Assert.IsEmpty(resultAfter); }