public void UpdapteFuel_With_Correct_Parameter() { //ARRANGE var options = new DbContextOptionsBuilder <FleetManagmentContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FleetManagmentContext(options); IFuelRepository fuelRepository = new FuelRepository(context); FuelTO fuel = new FuelTO { Name = "diesel" }; //ACT var addedFuel = fuelRepository.Insert(fuel); context.SaveChanges(); addedFuel.Name = "essence"; fuelRepository.Update(addedFuel); context.SaveChanges(); //ASSERT Assert.IsNotNull(addedFuel); Assert.AreNotEqual(0, addedFuel.Id); Assert.AreEqual("essence", addedFuel.Name); }
public void GetFuel_ById() { //ARRANGE var options = new DbContextOptionsBuilder <FleetManagmentContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FleetManagmentContext(options); IFuelRepository fuelRepository = new FuelRepository(context); FuelTO fuel = new FuelTO { Name = "diesel", }; FuelTO fuel2 = new FuelTO { Name = "essence", }; //ACT var addedFuel = fuelRepository.Insert(fuel); var addedFuel2 = fuelRepository.Insert(fuel2); context.SaveChanges(); //ASSERT Assert.IsNotNull(addedFuel); Assert.AreNotEqual(0, addedFuel.Id); Assert.AreEqual("diesel", addedFuel.Name); Assert.AreEqual(1, fuelRepository.GetByID(1).Id); }
public void AddFuel_ThrowsException_WhenNullIsProvided() { var options = new DbContextOptionsBuilder <FleetManagmentContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FleetManagmentContext(options); IFuelRepository fuelRepository = new FuelRepository(context); FuelTO fuel = new FuelTO { Name = "diesel", }; FuelTO fuel2 = new FuelTO { Name = "essence", }; //ACT var addedFuel = fuelRepository.Insert(fuel); context.SaveChanges(); //ASSERT Assert.ThrowsException <ArgumentNullException>(() => fuelRepository.Insert(null)); }
public FuelTO Insert(FuelTO fuel) { if (fuel is null) { throw new ArgumentNullException(nameof(fuel)); } return(context.Fuels.Add(fuel.ToEntity()).Entity.ToTransfertObject()); }
public FuelTO Update(FuelTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } var updated = context.Fuels.FirstOrDefault(x => x.Id == entity.Id); updated.Name = entity.Name; return(updated.ToTransfertObject()); }
public static Fuel ToEntity(this FuelTO fuel) { if (fuel is null) { throw new ArgumentNullException(nameof(fuel)); } return(new Fuel { Id = fuel.Id, Name = fuel.Name, ModelFuels = fuel?.CreatingKind() }); }
public static Fuel ToDomain(this FuelTO fuel) { if (fuel is null) { throw new ArgumentNullException(nameof(fuel)); } return(new Fuel { Id = fuel.Id, Name = fuel.Name, Models = fuel.Models?.Select(x => x.ToDomain()).ToList() }); }
public void DeleteFuel_With_FuelTO_Parameter() { //ARRANGE var options = new DbContextOptionsBuilder <FleetManagmentContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FleetManagmentContext(options); IFuelRepository fuelRepository = new FuelRepository(context); FuelTO fuel = new FuelTO { Name = "diesel", }; FuelTO fuel2 = new FuelTO { Name = "essence", }; var addedFuel = fuelRepository.Insert(fuel); var addedFuel2 = fuelRepository.Insert(fuel2); context.SaveChanges(); //List<FuelTO> fuels = new List<FuelTO>(); var fuels = fuelRepository.GetAll().ToList(); //ACT fuelRepository.Remove(addedFuel); context.SaveChanges(); fuels = fuelRepository.GetAll().ToList(); //ASSERT Assert.IsNotNull(addedFuel); Assert.IsNotNull(addedFuel2); Assert.AreNotEqual(0, addedFuel.Id); Assert.AreNotEqual(0, addedFuel2.Id); Assert.AreEqual("diesel", addedFuel.Name); Assert.AreEqual("essence", addedFuel2.Name); Assert.AreEqual(1, fuels.Count); }
public static ICollection <ModelFuel> CreatingKind(this FuelTO fuel) { ICollection <ModelFuel> kinds = new List <ModelFuel>(); if (fuel.Models == null) { return(kinds); } foreach (var model in fuel.Models) { var kind = new ModelFuel { FuelId = fuel.Id, ModelId = model.Id, }; kinds.Add(kind); } return(kinds); }
public bool Remove(FuelTO fuel) { return(RemoveById(fuel.Id)); }