public async Task ShouldCreateAndFetchNewVehiclesWithNoWheelsOrEnginesAsync() { using (var context = new MockContext(emptyDbOptions)) { // Ensure DB was cleaned up context.ResetValueGenerators(); await context.Database.EnsureDeletedAsync(); var service = new VehicleService(context); var vehicleVm = new VehicleViewModel { Name = VEHICLE_NAME + 1 }; // Ensure DB emtpy var noVehicles = service.GetVehicles(); Assert.Empty(noVehicles); // Add new vehicle var isCreatedOk = await service.CreateNewVehicle(vehicleVm); Assert.True(isCreatedOk); // Ensre vehicle added OK var oneVehicles = service.GetVehicles(); Assert.Single(oneVehicles); } }
public async Task ShouldCreateVehicleWithExistingDb() { var dbOptions = DbHelper.GetNewDbOptions <MockContext>(); await CreateTestDatabase(dbOptions); using (var context = new MockContext(dbOptions)) { var service = new VehicleService(context); var vehicleVm = new VehicleViewModel { Name = "Test Vehicle", EngineId = 1, WheelId = 1 }; var isCreateSuccess = await service.CreateNewVehicle(vehicleVm); var vehicles = context.Vehicles; var vehicle1 = vehicles.First(); var vehicle5 = vehicles.Last(); Assert.True(isCreateSuccess); Assert.Equal(5, vehicles.Count()); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // At this point vehicle1.Wheels and vehicle1.Engine are null. // Inspecting the context.VehicleWheels and context.VehicleEngines verifies that these // actually exist and then inspecting vehicle1.Wheels and vehicle1.Engine are no longer null // // TL;DR: properties are null, until inspected in context, after which they are no longer null // // HACK: evaluating vehicleWheels and vehicleEngines manually results in these properties not being null as well // Is this expected? Unit test passes/succeeds based on context inspection, etc. Changes not propogating correctly... // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //var wheels = context.VehicleWheels.ToArray(); //var engines = context.VehicleEngines.ToArray(); Assert.Equal("Subaru Impreza", vehicle1.Name); Assert.Equal(2, vehicle1.Engine.EngineId); Assert.Equal(2, vehicle1.Wheels.First().WheelId); Assert.Equal("Test Vehicle", vehicle5.Name); Assert.Equal(1, vehicle5.Engine.EngineId); Assert.True(vehicle5.Wheels.All(x => x.WheelId == 1)); } }
public async Task ShouldCreateVehicleWithEmptyDb() { using (var context = new MockContext(DbHelper.GetNewDbOptions <MockContext>())) { var service = new VehicleService(context); var vehicleVm = new VehicleViewModel { Name = "Test Vehicle" }; var isCreateSuccess = await service.CreateNewVehicle(vehicleVm); var vehicle = context.Vehicles.First(); Assert.True(isCreateSuccess); Assert.Single(context.Vehicles); Assert.Equal(vehicleVm.Name, vehicle.Name); } }
public async Task ShouldCreateAndFetchNewVehiclesWithWheelsAndEnginesAsync(VehicleViewModel vehicleVm) { using (var context = new MockContext(DbHelper.GetNewDbOptions <MockContext>())) { // Ensure DB was cleaned up context.ResetValueGenerators(); await context.Database.EnsureDeletedAsync(); var vehicleService = new VehicleService(context); var wheelService = new WheelService(context); var engineService = new EngineService(context); var engineVm = new EngineViewModel { Capacity = 4000, Configuration = "V8", FuelType = "Petrol" }; var wheelVm = new WheelViewModel { Size = 17, Width = 7 }; // Ensure DB emtpy var noVehicles = vehicleService.GetVehicles(); var noEngines = engineService.GetEngines(); var noWheels = wheelService.GetWheels(); Assert.Empty(noVehicles); Assert.Empty(noEngines); Assert.Empty(noWheels); // Add entities var isCreatedOk = await engineService.CreateNewEngine(engineVm); isCreatedOk = isCreatedOk && await wheelService.CreateNewWheel(wheelVm); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // TODO: This should fail in the second execution due to foreign key constraint. We are trying to add a vehicle // with an engine and wheel combination that does not yet exist. This fails during normal execution. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! isCreatedOk = isCreatedOk && await vehicleService.CreateNewVehicle(vehicleVm); // Ensure all created ok Assert.True(isCreatedOk); // Ensure vehicle added OK var oneEngine = engineService.GetEngines(); var oneWheel = wheelService.GetWheels(); var oneVehicles = vehicleService.GetVehicles(); Assert.Single(oneEngine); Assert.Single(oneWheel); Assert.Single(oneVehicles); // Verify many-to-many tables var oneVehicleEngine = context.VehicleEngines; var fourVehicleWheels = context.VehicleWheels; Assert.Single(oneVehicleEngine); Assert.Equal(4, fourVehicleWheels.Count()); } }