Ejemplo n.º 1
0
        public void PizzaJunctionDeleteRemovesFromOrderTable()
        {
            // Arrange
            RepoTesting repo = new RepoTesting();

            repo.ResetDatabase("PizzaJuntion_Test_3");
            db.Models.OrderJunction dbOrderJunction = repo.dbOrderJunction;
            int orderId      = dbOrderJunction.OrderId;
            int pizzaId      = dbOrderJunction.PizzaId;
            int ingredientId = repo.dbIngredient.Id;

            // Ensure the entities exist
            // order junction
            Assert.NotNull(repo.orderJunctionRepo.GetById(dbOrderJunction.OrderId, dbOrderJunction.PizzaId));
            // Pizza junction
            Assert.NotNull(repo.pizzaRepo.GetById(pizzaId, ingredientId));

            // Act
            repo.pizzaRepo.Delete(repo.dbPizza);
            repo.SaveChanges();


            // Assert
            // Order junction was removed
            Assert.Throws <e.InvalidIdException>(() => repo.orderJunctionRepo.GetById(dbOrderJunction.OrderId, dbOrderJunction.PizzaId));
            // Pizza junction was removed
            Assert.Throws <e.InvalidIdException>(() => repo.pizzaRepo.GetById(pizzaId, ingredientId));
        }
Ejemplo n.º 2
0
        public void CreatingNewOrderJunctionSucceedsWithValidOrderIdandPizzaId()
        {
            // Arrange
            RepoTesting repo = new RepoTesting();

            repo.ResetDatabase("OrderJunction_Test_1");
            dbm.PizzaJunction dbPizza = new dbm.PizzaJunction {
                PizzaId = dbm.PizzaJunction.GetNewId(), Count = 1, IngredientId = repo.dbIngredient.Id
            };
            repo.pizzaRepo.Create(dbPizza);
            repo.SaveChanges();

            // Ensure the required entities exist
            Assert.NotNull(repo.pizzaRepo.GetById(dbPizza.PizzaId, dbPizza.IngredientId));

            dbm.OrderJunction dbOrderJunction = new dbm.OrderJunction
            {
                OrderId = repo.dbOrder.Id, PizzaId = dbPizza.PizzaId
            };

            // Act
            // Create the new order junction
            repo.orderJunctionRepo.Create(dbOrderJunction);

            // Assert
            // Searching for this orderJunction should now succeed
            Assert.NotNull(repo.orderJunctionRepo.GetById(repo.dbOrder.Id, dbPizza.PizzaId));
        }
Ejemplo n.º 3
0
        public void CreatingNewOrderJunctionFailsWithInvalidPizzaId()
        {
            // Arrange
            RepoTesting repo = new RepoTesting();

            repo.ResetDatabase("OrderJunction_Test_3");
            int invalidPizzaId = -1;

            // Ensure the required entities exist
            Assert.NotNull(repo.orderRepo.GetById(repo.dbOrder.Id));

            dbm.OrderJunction dbOrderJunction = new dbm.OrderJunction
            {
                OrderId = repo.dbOrder.Id, PizzaId = invalidPizzaId
            };

            // Act
            // Create the new order junction
            Assert.Throws <e.InvalidIdException>(() => repo.orderJunctionRepo.Create(dbOrderJunction));

            // Assert
            // Searching for this orderJunction should fail
            Assert.Throws <e.InvalidIdException>(() => repo.orderJunctionRepo.GetById(repo.dbOrder.Id, invalidPizzaId));
        }
Ejemplo n.º 4
0
        public static db.Order Map(lib.Order libOrder,
                                   out List <db.OrderJunction> orderJunctionList,
                                   out List <db.PizzaJunction> pizzaJunctionList)
        {
            db.Order dbOrder = new db.Order
            {
                Id         = libOrder.Id,
                LocationId = libOrder.LocationId,
                UserId     = libOrder.UserId,
                TimePlaced = libOrder.TimePlaced,
                TotalPrice = libOrder.TotalPrice
            };

            orderJunctionList = new List <db.OrderJunction>();
            pizzaJunctionList = new List <db.PizzaJunction>();
            foreach (var pizza in libOrder.Pizzas)
            {
                db.OrderJunction orderJunction = new db.OrderJunction
                {
                    OrderId = libOrder.Id,
                    PizzaId = pizza.Id
                };
                orderJunctionList.Add(orderJunction);
                foreach (var ingredient in pizza.Ingredients.Distinct())
                {
                    db.PizzaJunction pizzaJunction = new db.PizzaJunction
                    {
                        PizzaId      = pizza.Id,
                        IngredientId = ingredient.Id,
                        Count        = pizza.Ingredients.Where(i => i.Id == ingredient.Id).Count()
                    };
                    pizzaJunctionList.Add(pizzaJunction);
                }
            }
            return(dbOrder);
        }
        public void ResetDatabase(string dbName)
        {
            var options = new DbContextOptionsBuilder <db.PizzaStoreDBContext>()
                          .UseInMemoryDatabase(dbName).Options;

            database          = new db.PizzaStoreDBContext(options);
            ingredientRepo    = new IngredientRepository(database);
            locationRepo      = new LocationRepository(database);
            userRepo          = new UserRepository(database);
            inventoryRepo     = new InventoryJunctionRepository(database);
            pizzaRepo         = new PizzaJunctionRepository(database);
            orderJunctionRepo = new OrderJunctionRepository(database);
            orderRepo         = new OrderRepository(database);

            // Going to add one entry to each repo for testing purposes.
            dbIngredient = new dbm.Ingredient {
                Id = 9999, Name = "cheese", Price = 1.50m
            };
            ingredientRepo.Create(dbIngredient);
            dbLocation = new dbm.Location {
                Id = 9999, Name = "John's Pizzaria"
            };
            locationRepo.Create(dbLocation);

            // Save should populate the above entities' Ids
            SaveChanges();

            dbUser = new dbm.User {
                Id = 9999, FirstName = "John", LastName = "Pot", DefaultLocationId = dbLocation.Id
            };
            userRepo.Create(dbUser);
            dbInventory = new dbm.InventoryJunction {
                LocationId = dbLocation.Id, IngredientId = dbIngredient.Id, Count = 100
            };
            inventoryRepo.Create(dbInventory);
            // Have to manually set the pizza junction id since it is a nested many-to-many relationship
            Random rand = new Random(DateTime.Now.TimeOfDay.Milliseconds);

            dbPizza = new dbm.PizzaJunction {
                PizzaId = dbm.PizzaJunction.GetNewId(), IngredientId = dbIngredient.Id, Count = 2
            };
            pizzaRepo.Create(dbPizza);

            // Update user id for order usage
            SaveChanges();

            dbOrder = new dbm.Order {
                Id = 9999, LocationId = dbLocation.Id, UserId = dbUser.Id, TimePlaced = DateTime.Now, TotalPrice = 20.50m
            };
            orderRepo.Create(dbOrder);

            // Order junction needs order to have an Id
            SaveChanges();

            dbOrderJunction = new dbm.OrderJunction {
                OrderId = dbOrder.Id, PizzaId = dbPizza.PizzaId
            };
            orderJunctionRepo.Create(dbOrderJunction);

            SaveChanges();
            // All tables should now have one entry.
        }