Пример #1
0
        public void GetMenu()
        {
            var pizzaList           = new List <Pizza>();
            var pizzaPriceList      = new List <PizzaPrice>();
            var ingredientPriceList = new List <IngredientPrice>();

            Pizza pizza1 = new Pizza {
                Name = "Pizza1"
            };
            Pizza pizza2 = new Pizza {
                Name = "Pizza2"
            };

            Ingredient ingredient1 = new Ingredient {
                Name = "Ingredient 1"
            };
            Ingredient ingredient2 = new Ingredient {
                Name = "Ingredient 2"
            };

            pizza1.Ingredients.Add(new PizzaIngredient {
                Pizza = pizza1, Ingredient = ingredient1, IngredientId = ingredient1.Id
            });
            pizza1.Ingredients.Add(new PizzaIngredient {
                Pizza = pizza1, Ingredient = ingredient2, IngredientId = ingredient2.Id
            });

            pizzaList.Add(pizza1);
            pizzaList.Add(pizza2);

            IngredientPrice ingredientPrice = new IngredientPrice {
                Date = new DateTime(2018, 01, 01), Ingredient = ingredient1, Price = 10
            };
            IngredientPrice ingredientPrice2 = new IngredientPrice {
                Date = new DateTime(2019, 01, 01), Ingredient = ingredient1, Price = 15
            };

            PizzaPrice pizzaPrice = new PizzaPrice {
                Date = new DateTime(2018, 01, 01), Pizza = pizza1, Price = 100
            };
            PizzaPrice pizzaPrice2 = new PizzaPrice {
                Date = new DateTime(2018, 01, 01), Pizza = pizza2, Price = 110
            };

            pizzaPriceList.Add(pizzaPrice);
            pizzaPriceList.Add(pizzaPrice2);

            ingredientPriceList.Add(ingredientPrice);
            ingredientPriceList.Add(ingredientPrice2);

            Mapper.Reset();
            Mapper.Initialize(cfg => cfg.AddProfile <AutoMapperProfile>());

            var mock = new Mock <IDataAccess>();

            mock.Setup(r => r.Pizzas.GetAll()).Returns(pizzaList);

            mock.Setup(m => m.PizzaPrices.Get(It.IsAny <Expression <Func <PizzaPrice, bool> > >())).Returns <Expression <Func <PizzaPrice, bool> > >((func) =>
            {
                return(pizzaPriceList.Where(func.Compile()).ToList());
            });

            mock.Setup(m => m.IngredientPrices.Get(It.IsAny <Expression <Func <IngredientPrice, bool> > >())).Returns <Expression <Func <IngredientPrice, bool> > >((func) =>
            {
                return(ingredientPriceList.Where(func.Compile()).ToList());
            });

            DataController controller = new DataController(mock.Object, null);

            var result = controller.GetMenu();

            Assert.IsInstanceOfType(result, typeof(OkObjectResult));

            Assert.IsNotNull(((OkObjectResult)result).Value);

            Assert.AreEqual(((ICollection <PizzaViewModel>)((OkObjectResult)result).Value).Count, 2);
        }
Пример #2
0
        public void UnitOfWorkTests()
        {
            DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Test;Trusted_Connection=True;");

            ApplicationDbContext context = new ApplicationDbContext(optionsBuilder.Options);

            context.Database.EnsureCreated();

            UnitOfWork unit = new UnitOfWork(context);

            Pizza pizza1 = new Pizza {
                Name = "Pizza1"
            };
            Pizza pizza2 = new Pizza {
                Name = "Pizza2"
            };

            Ingredient ingredient1 = new Ingredient {
                Name = "Ingredient 1"
            };
            Ingredient ingredient2 = new Ingredient {
                Name = "Ingredient 2"
            };

            IngredientPrice ingredientPrice = new IngredientPrice {
                Date = new DateTime(2018, 01, 01), Ingredient = ingredient1, Price = 10
            };
            IngredientPrice ingredientPrice2 = new IngredientPrice {
                Date = new DateTime(2019, 01, 01), Ingredient = ingredient1, Price = 15
            };

            PizzaPrice pizzaPrice = new PizzaPrice {
                Date = new DateTime(2018, 01, 01), Pizza = pizza1, Price = 100
            };
            PizzaPrice pizzaPrice2 = new PizzaPrice {
                Date = new DateTime(2018, 01, 01), Pizza = pizza2, Price = 110
            };

            unit.Ingredients.Create(ingredient1);
            unit.Ingredients.Create(ingredient2);

            unit.SaveChanges();

            pizza1.Ingredients.Add(new PizzaIngredient {
                Pizza = pizza1, Ingredient = ingredient1, IngredientId = ingredient1.Id
            });
            pizza1.Ingredients.Add(new PizzaIngredient {
                Pizza = pizza1, Ingredient = ingredient2, IngredientId = ingredient2.Id
            });

            pizza2.Ingredients.Add(new PizzaIngredient {
                Pizza = pizza2, Ingredient = ingredient1, IngredientId = ingredient1.Id
            });

            unit.Pizzas.Create(pizza1);
            unit.Pizzas.Create(pizza2);

            unit.IngredientPrices.Create(ingredientPrice);
            unit.IngredientPrices.Create(ingredientPrice2);

            unit.PizzaPrices.Create(pizzaPrice);
            unit.PizzaPrices.Create(pizzaPrice2);

            unit.SaveChanges();

            var pizzaList = (List <Pizza>)unit.Pizzas.GetAll();

            Assert.AreEqual(pizzaList.Count, 2);
            Assert.AreEqual(pizzaList[0].Ingredients.Count, 2);
        }