예제 #1
0
        public void CreateShoppingCartHasCorrectInventoryAmount()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateShoppingCartHasCorrectInventoryAmount")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                ProductView productView = new ProductView()
                {
                    ProductViewId = 1,
                    Title         = "Catch-22",
                    Author        = "Joseph Heller",
                    Description   = "Catch-22 is a satirical war novel by American author Joseph Heller.",
                    Price         = 12.50,
                    Amount        = 2,
                    ProductId     = 1,
                    IsInCart      = true,
                };

                ViewModel viewModel = new ViewModel();

                ShoppingCart shoppingCart = viewModel.CreateShoppingCart(productView, 1, "Missouri");

                Assert.Equal(1, shoppingCart.StockAmount);
                Assert.Equal("Missouri", shoppingCart.State);
            }
        }
예제 #2
0
        public void CreateProductViewsHasCorrectInventoryAmount()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateProductViewsHasCorrectInventoryAmount")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                List <Product>   products    = DbManipulation.GetProducts(context).ToList();
                List <Inventory> inventories = DbManipulation.GetInventoriesOfStore(context, 1).ToList();

                foreach (Inventory inventory in inventories)
                {
                    inventory.Amount = 1;
                }

                ViewModel viewModel = new ViewModel();

                List <ProductView> productViews = viewModel.CreateProductViews(products, inventories).ToList();

                foreach (ProductView productView in productViews)
                {
                    Assert.Equal(1, productView.Amount);
                }
            }
        }
        public static void SeedDb(BookopolisDbContext context)
        {
            SeedData seedData = new SeedData();

            foreach (var prod in seedData.Products)
            {
                context.Products.Add(prod);
            }
            context.SaveChanges();

            var prodContext = context.Products.ToList();

            foreach (var store in seedData.Stores)
            {
                List <Inventory> inventories = new List <Inventory>();
                foreach (var prod in prodContext)
                {
                    Inventory inventory = new Inventory();
                    inventory.ProductId = prod.ProductId;
                    inventory.Amount    = 2;
                    inventories.Add(inventory);
                }
                store.Inventories = inventories;
            }

            foreach (var store in seedData.Stores)
            {
                context.Stores.Add(store);
            }
            context.SaveChanges();
        }
        public static List <Order> GetOrdersFromOrderProducts(BookopolisDbContext context, IEnumerable <OrderProduct> orderProducts)
        {
            IQueryable <Order> inboth = (from p1 in context.Orders
                                         join p2 in orderProducts
                                         on p1.OrderId equals p2.OrderId
                                         select p1).Distinct();
            List <Order> res = inboth.ToList();

            return(res);
        }
예제 #5
0
        public OrderController(IMemoryCache cache, BookopolisDbContext db)
        {
            _cache = cache;
            _db    = db;

            if (!_cache.TryGetValue("shoppingCart", out shoppingCart))
            {
                _cache.Set("shoppingCart", new List <ShoppingCart>());
                _cache.TryGetValue("shoppingCart", out shoppingCart);
            }
        }
예제 #6
0
        public void GetInventoryOfShoppingCartReturnsCorrectShoppingCart()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetInventoryOfShoppingCartReturnsCorrectShoppingCart")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                List <Product>      products             = DbManipulation.GetProducts(context).ToList();
                List <ShoppingCart> shoppingCartProducts = new List <ShoppingCart>()
                {
                    new ShoppingCart()
                    {
                        Inventory = new Inventory()
                        {
                            InventoryId = 1
                        },
                        ProductId   = 1,
                        StoreId     = 1,
                        StockAmount = 1
                    },
                    new ShoppingCart()
                    {
                        Inventory = new Inventory()
                        {
                            InventoryId = 2
                        },
                        ProductId   = 1,
                        StoreId     = 1,
                        StockAmount = 0
                    },
                    new ShoppingCart()
                    {
                        Inventory = new Inventory()
                        {
                            InventoryId = 3
                        },
                        ProductId   = 1,
                        StoreId     = 2,
                        StockAmount = 2
                    }
                };

                List <ShoppingCart> result = DbManipulation.GetInventoryOfShoppingCart(shoppingCartProducts, 1, 1);

                foreach (ShoppingCart shoppingCart in result)
                {
                    Assert.Equal(1, shoppingCart.StoreId);
                    Assert.Equal(1, shoppingCart.ProductId);
                }
            }
        }
예제 #7
0
        public UnitTest1()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "HomeIndexViewIsReturned")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                _homeController = new HomeController(context);
            }
        }
        public ProductController(BookopolisDbContext db, IMemoryCache cache)
        {
            _cache = cache;
            _db    = db;

            if (!_cache.TryGetValue("shoppingCart", out shoppingCartProducts))
            {
                _cache.Set("shoppingCart", new List <ShoppingCart>());
                _cache.TryGetValue("shoppingCart", out shoppingCartProducts);
            }
        }
예제 #9
0
        public void IsSeededReturnsFalseWhenEmpty()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "IsSeededReturnsFalseWhenEmpty")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                Assert.False(DbManipulation.IsSeeded(context));
            }
        }
예제 #10
0
        public void IsSeededReturnsTrueWhenSeeded()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "IsSeededReturnsTrueWhenSeeded")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);
                Assert.True(DbManipulation.IsSeeded(context));
            }
        }
 public static void UpdateInventoryAmounts(BookopolisDbContext context, Dictionary <int, int> myDict)
 {
     for (int i = 0; i < myDict.Keys.Count; i++)
     {
         for (int j = 0; j < context.Inventories.ToList().Count; j++)
         {
             if (myDict.Keys.ToList()[i] == context.Inventories.ToList()[j].InventoryId)
             {
                 context.Inventories.ToList()[j].Amount -= myDict[context.Inventories.ToList()[j].InventoryId];
             }
         }
     }
 }
예제 #12
0
        public void IsStoreIsInRange()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "IsStoreIsInRange")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                Assert.InRange(4, 1, 5);
                Assert.False(DbManipulation.IsStore(context, 5));
                Assert.True(DbManipulation.IsStore(context, 4));
            }
        }
예제 #13
0
        public void GetInventoriesReturnsAllInventories()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetInventoriesReturnsAllInventories")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                IEnumerable <Inventory> inventories     = DbManipulation.GetInventories(context);
                List <Inventory>        inventoriesList = inventories.ToList();
                Assert.InRange(inventoriesList.Count, 1, inventoriesList.Count + 1);
            }
        }
예제 #14
0
        public void GetProductsReturnsAllProducts()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetProductsReturnsAllProducts")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                IEnumerable <Product> products     = DbManipulation.GetProducts(context);
                List <Product>        productsList = products.ToList();
                Assert.InRange(productsList.Count, 1, productsList.Count + 1);
            }
        }
예제 #15
0
        public void SeedDbAddsAllData()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "SeedDbAddsAllData")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);
                SeedData seedData = new SeedData();

                Assert.Equal(seedData.Products.Count, context.Products.Count());
                Assert.Equal(seedData.Stores.Count, context.Stores.Count());
            }
        }
예제 #16
0
        public void GetStoreReturnsCorrectStore()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetStoreReturnsCorrectStore")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                Store store = DbManipulation.GetStore(context, 2);
                Assert.IsType <Store>(store);
                Assert.Equal(2, store.StoreId);
                Assert.NotEqual(3, store.StoreId);
            }
        }
예제 #17
0
        public void GetProductReturnsCorrectProduct()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetProductReturnsCorrectProduct")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                Product product = DbManipulation.GetProduct(context, 7);
                Assert.IsType <Product>(product);
                Assert.Equal(7, product.ProductId);
                Assert.NotEqual(3, product.ProductId);
            }
        }
예제 #18
0
        public void GetInventoriesOfStoreReturnsCorrectInventories()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetInventoriesOfStoreReturnsCorrectInventories")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                IEnumerable <Inventory> inventories = DbManipulation.GetInventoriesOfStore(context, 3);
                int count = inventories.ToList().Select(inv => inv.StoreId).Count();

                Assert.Equal(context.Products.Count(), count);
            }
        }
예제 #19
0
        public void CreateProductViewHasCorrectInventoryAmount()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateProductViewHasCorrectInventoryAmount")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                Inventory inv  = DbManipulation.GetInventoryOfStoreProduct(context, 1, 3);
                Product   prod = DbManipulation.GetProduct(context, 3);

                List <ShoppingCart> shoppingCartProducts = new List <ShoppingCart>()
                {
                    new ShoppingCart()
                    {
                        ShoppingCartId = 1,
                        StoreId        = 1,
                        ProductId      = 3,
                        StockAmount    = 2
                    },
                    new ShoppingCart()
                    {
                        ShoppingCartId = 2,
                        StoreId        = 1,
                        ProductId      = 3,
                        StockAmount    = 1
                    },
                    new ShoppingCart()
                    {
                        ShoppingCartId = 3,
                        StoreId        = 1,
                        ProductId      = 3,
                        StockAmount    = 0
                    },
                };

                List <ShoppingCart> shoppingCartInvs = DbManipulation.GetInventoryOfShoppingCart(shoppingCartProducts, 1, 3);

                ViewModel viewModel = new ViewModel();

                ProductView productView = viewModel.CreateProductView(shoppingCartInvs, inv, prod);

                Assert.Equal(0, productView.Amount);
            }
        }
예제 #20
0
        public void GetInventoryOfStoreProductReturnsCorrectInventory()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetInventoryOfStoreProductReturnsCorrectInventory")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                Inventory inventory1   = DbManipulation.GetInventoryOfStoreProduct(context, 4, 2);
                Inventory expectedInv1 = context.Inventories.FirstOrDefault(inv => inv.StoreId == 4 && inv.ProductId == 2);

                Inventory inventory2   = DbManipulation.GetInventoryOfStoreProduct(context, 4, 2);
                Inventory expectedInv2 = context.Inventories.FirstOrDefault(inv => inv.StoreId == 3 && inv.ProductId == 2);

                Assert.Equal(expectedInv1, inventory1);
                Assert.NotEqual(expectedInv2, inventory2);
            }
        }
예제 #21
0
        public void SetShoppingCartStockSetsCorrectStock()
        {
            var options = new DbContextOptionsBuilder <BookopolisDbContext>()
                          .UseInMemoryDatabase(databaseName: "SetShoppingCartStockSetsCorrectStock")
                          .Options;

            using (var context = new BookopolisDbContext(options))
            {
                DbManipulation.SeedDb(context);

                List <ShoppingCart> shoppingCartProducts = new List <ShoppingCart>()
                {
                    new ShoppingCart()
                    {
                        Inventory = new Inventory()
                        {
                            InventoryId = 1
                        },
                        StockAmount = 0
                    },
                    new ShoppingCart()
                    {
                        Inventory = new Inventory()
                        {
                            InventoryId = 2
                        },
                        StockAmount = 1
                    }
                };

                List <Inventory> inventories = DbManipulation.GetInventoriesOfStore(context, 1).OrderBy(pid => pid.ProductId).ToList();
                DbManipulation.SetShoppingCartStock(shoppingCartProducts, inventories);
                Inventory correctStockAmount1 = inventories.FirstOrDefault(inv => inv.InventoryId == 1);
                Inventory correctStockAmount2 = inventories.FirstOrDefault(inv => inv.InventoryId == 2);

                Assert.Equal(0, correctStockAmount1.Amount);
                Assert.Equal(1, correctStockAmount2.Amount);
            }
        }
 public static IEnumerable <Customer> GetCustomers(BookopolisDbContext context)
 => context.Customers;
예제 #23
0
 public StoreController(BookopolisDbContext db, IMemoryCache cache)
 {
     _db    = db;
     _cache = cache;
 }
 public CustomerController(BookopolisDbContext db, IMemoryCache cache)
 {
     _db    = db;
     _cache = cache;
 }
예제 #25
0
        public void SeedStores(BookopolisDbContext context)
        {
            List <Store> stores = new List <Store>()
            {
                new Store()
                {
                    StreetAddress = "1701 W 133rd St",
                    City          = "Kansas City",
                    State         = "Missouri",
                    ZIP           = "64145"
                },
                new Store()
                {
                    StreetAddress = "6185 Retail Rd",
                    City          = "Dallas",
                    State         = "Texas",
                    ZIP           = "75231"
                },
                new Store()
                {
                    StreetAddress = "1400 Hilltop Mall Rd",
                    City          = "Richmond",
                    State         = "California",
                    ZIP           = "94806"
                },
                new Store()
                {
                    StreetAddress = "3201 E Platte Ave",
                    City          = "Colorado Springs",
                    State         = "Colorado",
                    ZIP           = "80909"
                },
            };

            List <Product> products = new List <Product>()
            {
                new Product()
                {
                    Title       = "Catch-22",
                    Author      = "Joseph Heller",
                    Description = "Catch-22 is a satirical war novel by American author Joseph Heller.",
                    Price       = 12.50
                },
                new Product()
                {
                    Title       = "The Grapes of Wrath",
                    Author      = "John Steinbeck",
                    Description = "The Grapes of Wrath is an American realist novel written by John Steinbeck.",
                    Price       = 12.50
                },
                new Product()
                {
                    Title       = "Midnight's Children",
                    Author      = "Salman Rushdie",
                    Description = "Midnight's Children is a 1981 novel by author Salman Rushdie",
                    Price       = 12.50
                },
                new Product()
                {
                    Title       = "Ulysses",
                    Author      = "James Joyce",
                    Description = "Ulysses is a modernist novel by Irish writer James Joyce.",
                    Price       = 15.50
                },
                new Product()
                {
                    Title       = "The Sound and the Fury",
                    Author      = "William Faulkner",
                    Description = "The Sound and the Fury is a novel by the American author William Faulkner.",
                    Price       = 10.50
                },
                new Product()
                {
                    Title       = "On the Road",
                    Author      = "Jack Kerouac",
                    Description = "On the Road is a 1957 novel by American writer Jack Kerouac, based on the travels of Kerouac and his friends across the United States",
                    Price       = 11.50
                },
                new Product()
                {
                    Title       = "The Sun Also Rises",
                    Author      = "Ernest Hemingway",
                    Description = "The Sun Also Rises is a 1926 novel by American writer Ernest Hemingway that portrays American and British expatriates who travel from Paris to the Festival of San Fermín in Pamplona to watch the running of the bulls and the bullfights.",
                    Price       = 16.50
                },
                new Product()
                {
                    Title       = "I, Claudius",
                    Author      = "Robert Graves",
                    Description = "I, Claudius is a historical novel by English writer Robert Graves, published in 1934.",
                    Price       = 10.50
                },
            };

            foreach (var prod in products)
            {
                context.Products.Add(prod);
            }
            context.SaveChanges();

            var prodContext = context.Products.ToList();

            foreach (var store in stores)
            {
                List <Inventory> inventories = new List <Inventory>();
                foreach (var prod in prodContext)
                {
                    Inventory inventory = new Inventory();
                    inventory.ProductId = prod.ProductId;
                    inventory.Amount    = 2;
                    inventories.Add(inventory);
                }
                store.Inventories = inventories;
            }

            foreach (var store in stores)
            {
                context.Stores.Add(store);
            }
            context.SaveChanges();
        }
예제 #26
0
 public HomeController(BookopolisDbContext db)
 {
     _db = db;
 }
 public OrderController(IMemoryCache cache, BookopolisDbContext db)
 {
     _cache = cache;
     _db    = db;
 }
 public static Customer GetCustomer(BookopolisDbContext context, int?customerId)
 => context.Customers.FirstOrDefault(c => c.CustomerId == customerId);
 public static IEnumerable <OrderProduct> GetCustomerOrderProducts(BookopolisDbContext context, int?customerId)
 => context.OrderProducts.Where(op => op.CustomerId == customerId);
 public static IEnumerable <CustomerAddress> GetCustomerAddresses(BookopolisDbContext context, int?customerId)
 => context.CustomerAddresses.Where(ca => ca.CustomerId == customerId);