public void CreatingNewInventorySucceedsWithValidLocationAndIngredient()
        {
            // Arrange
            RepoTesting repo = new RepoTesting();

            repo.ResetDatabase("Inventory_Test_3");
            dbm.Location dbLocation = new dbm.Location {
                Id = 2, Name = "a"
            };
            dbm.Ingredient dbIngredient = new dbm.Ingredient {
                Id = 2, Name = "pepperoni", Price = 2.0m
            };
            repo.locationRepo.Create(dbLocation);
            repo.ingredientRepo.Create(dbIngredient);
            repo.SaveChanges();

            // Ensure the required entities exist
            Assert.NotNull(repo.locationRepo.GetById(dbLocation.Id));
            Assert.NotNull(repo.ingredientRepo.GetById(dbIngredient.Id));

            dbm.InventoryJunction dbInventory = new dbm.InventoryJunction
            {
                LocationId = dbLocation.Id, IngredientId = dbIngredient.Id, Count = 1
            };

            // Act
            // Create the new inventory
            repo.inventoryRepo.Create(dbInventory);

            // Assert
            // Searching for this inventory should now succeed
            Assert.NotNull(repo.inventoryRepo.GetById(dbLocation.Id, dbIngredient.Id));
        }
        public void CreatingNewInventoryFailsWithInvalidIngredient()
        {
            // Arrange
            RepoTesting repo = new RepoTesting();

            repo.ResetDatabase("Inventory_Test_2");
            int locationId          = repo.dbLocation.Id;
            int invalidIngredientId = -1;

            // Ensure the entities exist
            Assert.NotNull(repo.locationRepo.GetById(locationId));
            // Searching for this inventory should fail since we
            //  are passing in an invalid ingredient id
            Assert.Throws <e.InvalidIdException>(() => repo.inventoryRepo.GetById(locationId, invalidIngredientId));

            db.Models.InventoryJunction dbInventory = new db.Models.InventoryJunction
            {
                LocationId = locationId, IngredientId = invalidIngredientId, Count = 1
            };


            // Act
            // Assert
            Assert.Throws <e.InvalidIdException>(() => repo.inventoryRepo.Create(dbInventory));
        }
Esempio n. 3
0
        public static db.Location Map(lib.Location libLocation, out List <db.InventoryJunction> inventoryList)
        {
            db.Location dbLocation = new db.Location
            {
                Id   = libLocation.Id,
                Name = libLocation.Name
            };

            inventoryList = new List <db.InventoryJunction>();
            foreach (var inventory in libLocation.Inventory)
            {
                db.InventoryJunction dbInventory = new db.InventoryJunction
                {
                    IngredientId = inventory.Key,
                    LocationId   = libLocation.Id,
                    Count        = inventory.Value
                };

                inventoryList.Add(dbInventory);
            }

            return(dbLocation);
        }
        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.
        }