public async void VehiclesController_GetVehicleByUuid(Guid uuid) { try { var options = new DbContextOptionsBuilder <NMTFleetManagerContext>() .UseSqlite(connection) .Options; using (context = new NMTFleetManagerContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); } using (context = new NMTFleetManagerContext(options)) { SeedCollection seedCollection = SeedData.Seed(); context.Vehicles.AddRange(seedCollection.Vehicles); context.SaveChanges(); } using (context = new NMTFleetManagerContext(options)) { var expected = await context.Vehicles.SingleOrDefaultAsync(v => v.Uuid == uuid); vehiclesController = new VehiclesController(context); var actionResult = await vehiclesController.GetVehicleByUuid(uuid); if (expected == null) { Assert.IsType <NotFoundResult>(actionResult.Result); } else { var okResult = actionResult.Result as OkObjectResult; var actual = okResult?.Value as Vehicle; Assert.Equal(expected, actual); } } } finally { connection.Close(); } }
public async void VehiclesController_GetVehicles() { // Arrange (DB schema) try { // TODO: UseModel to override OnModelCreating var options = new DbContextOptionsBuilder <NMTFleetManagerContext>() .UseSqlite(connection) .Options; using (context = new NMTFleetManagerContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); } // Seed data into the context using (context = new NMTFleetManagerContext(options)) { SeedCollection seedCollection = SeedData.Seed(); context.Vehicles.AddRange(seedCollection.Vehicles); context.SaveChanges(); } // Assert using (context = new NMTFleetManagerContext(options)) { int expected = 5; vehiclesController = new VehiclesController(context); var actionResult = await vehiclesController.GetVehicles(); var okResult = actionResult.Result as OkObjectResult; var vehicles = okResult?.Value as IEnumerable <Vehicle>; var actual = vehicles.Count(); Assert.Equal(expected, actual); } } finally { connection.Close(); } }
public async void VehiclesController_PutVehicle(Guid uuid, Vehicle vehicle) { try { var options = new DbContextOptionsBuilder <NMTFleetManagerContext>() .UseSqlite(connection) .Options; using (context = new NMTFleetManagerContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); } using (context = new NMTFleetManagerContext(options)) { SeedCollection seedCollection = SeedData.Seed(); context.Vehicles.AddRange(seedCollection.Vehicles); context.SaveChanges(); } using (context = new NMTFleetManagerContext(options)) { var controller = new VehiclesController(context); var actionResult = await controller.PutVehicle(uuid, vehicle); bool validVehicle = await context.Vehicles.AnyAsync(v => v.Uuid == uuid && v.Uuid == vehicle.Uuid); if (validVehicle) { Assert.IsType <NoContentResult>(actionResult); var expected = vehicle; var actual = await context.Vehicles.SingleOrDefaultAsync(v => v.Uuid == uuid); output.WriteLine(expected.ToString()); output.WriteLine(actual.ToString()); Assert.Equal(expected, actual); } else { Assert.IsNotType <NoContentResult>(actionResult); var invalidVehicle = vehicle; var actual = await context.Vehicles.SingleOrDefaultAsync(v => v.Uuid == uuid); if (actual == null) { actual = await context.Vehicles.SingleOrDefaultAsync(v => v.Uuid == vehicle.Uuid); if (actual == null) { // since actual does not refer to any existing vehicle in the context, pass test Assert.Null(actual); return; } } // ensure that the corresponding vehicle in the context has not been updated Assert.NotEqual(invalidVehicle.Odometer, actual.Odometer); Assert.NotEqual(invalidVehicle.Registration, actual.Registration); } } } finally { connection.Close(); } }