示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        public FuelTO Insert(FuelTO fuel)
        {
            if (fuel is null)
            {
                throw new ArgumentNullException(nameof(fuel));
            }

            return(context.Fuels.Add(fuel.ToEntity()).Entity.ToTransfertObject());
        }
示例#5
0
        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());
        }
示例#6
0
        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()
            });
        }
示例#7
0
        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()
            });
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
示例#10
0
 public bool Remove(FuelTO fuel)
 {
     return(RemoveById(fuel.Id));
 }