Beispiel #1
0
        public void AddModel_With_Correct_Parameter()
        {
            //ARRANGE
            var options = new DbContextOptionsBuilder <FleetManagmentContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new FleetManagmentContext(options);

            IModelRepository modelRepository = new ModelRepository(context);
            ModelTO          model           = new ModelTO
            {
                Name = "polo",
            };

            ModelTO model2 = new ModelTO
            {
                Name = "golf",
            };

            //ACT
            var addedModel  = modelRepository.Insert(model);
            var addedModel2 = modelRepository.Insert(model2);

            context.SaveChanges();
            //ASSERT
            Assert.IsNotNull(addedModel);
            Assert.AreNotEqual(0, addedModel.Id);
            Assert.AreEqual("polo", addedModel.Name);
        }
        public ModelTO Insert(ModelTO model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(context.Models.Add(model.ToEntity()).Entity.ToTransfertObject());
        }
        public ModelTO Update(ModelTO entity)
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            var updated = context.Models.FirstOrDefault(x => x.Id == entity.Id);

            updated.Name  = entity.Name;
            updated.Brand = entity.Brand?.ToEntity();

            return(updated.ToTransfertObject());
        }
        public static Model ToDomain(this ModelTO model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(new Model
            {
                Id = model.Id,
                Name = model.Name,
                Brand = model.Brand?.ToDomain(),
                Fuels = model.Fules?.Select(x => x.ToDomain()).ToList()
            });
        }
Beispiel #5
0
        public static Model ToEntity(this ModelTO model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(new Model
            {
                Id = model.Id,
                Name = model.Name,
                Brand = model.Brand?.ToEntity(),
                ModelFuels = model?.CreatingKind(),
            });
        }
Beispiel #6
0
        public void DeleteModel_With_ModelTO_Parameter()
        {
            //ARRANGE
            var options = new DbContextOptionsBuilder <FleetManagmentContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new FleetManagmentContext(options);

            IModelRepository modelRepository = new ModelRepository(context);

            ModelTO model = new ModelTO
            {
                Name = "polo",
            };

            ModelTO model2 = new ModelTO
            {
                Name = "golf",
            };

            var addedModel  = modelRepository.Insert(model);
            var addedModel2 = modelRepository.Insert(model2);

            context.SaveChanges();

            //List<ModelTO> models = new List<ModelTO>();
            var models = modelRepository.GetAll().ToList();

            //ACT
            modelRepository.Remove(addedModel);
            context.SaveChanges();
            models = modelRepository.GetAll().ToList();

            //ASSERT
            Assert.IsNotNull(addedModel);
            Assert.IsNotNull(addedModel2);
            Assert.AreNotEqual(0, addedModel.Id);
            Assert.AreNotEqual(0, addedModel2.Id);
            Assert.AreEqual("polo", addedModel.Name);
            Assert.AreEqual("golf", addedModel2.Name);
            Assert.AreEqual(1, models.Count);
        }
Beispiel #7
0
        public static ICollection <ModelFuel> CreatingKind(this ModelTO model)
        {
            ICollection <ModelFuel> kinds = new List <ModelFuel>();

            if (model.Fules == null)
            {
                return(kinds);
            }
            foreach (var fuel in model.Fules)
            {
                var kind = new ModelFuel
                {
                    FuelId  = fuel.Id,
                    ModelId = model.Id,
                };
                kinds.Add(kind);
            }
            return(kinds);
        }
Beispiel #8
0
        public void AddModel_ThrowsException_WhenNullIsProvided()
        {
            var options = new DbContextOptionsBuilder <FleetManagmentContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using var context = new FleetManagmentContext(options);

            IModelRepository modelRepository = new ModelRepository(context);
            ModelTO          model           = new ModelTO
            {
                Name = "polo",
            };

            //ACT
            var addedModel = modelRepository.Insert(model);

            context.SaveChanges();
            //ASSERT
            Assert.ThrowsException <ArgumentNullException>(() => modelRepository.Insert(null));
        }
 public bool Remove(ModelTO model)
 {
     return(RemoveById(model.Id));
 }