public async void GetsAllProducts()
        {
            //Arrange
            var options = InMemoryDb("GetsAllProducts");

            //Act
            using (var context = new MSDbContext(options))
            {
                var product = new Product
                {
                    Id    = 6,
                    Price = 150.55M,
                };
                context.Add(product);
                product = new Product
                {
                    Id    = 7,
                    Price = 150.55M,
                };
                context.Add(product);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                var productRepo = new ProductRepository(context);
                var products    = await productRepo.GetAllAsync();

                Assert.Equal(6, products.Count());
            }
        }
        public async Task GetsAllLocations()
        {
            //Arrange
            var options = InMemoryDb("GetsAllLocations");

            //Act
            using (var context = new MSDbContext(options))
            {
                var store = new Store
                {
                    Name = "Florida"
                };
                context.Add(store);
                store = new Store
                {
                    Name = "Texas"
                };
                context.Add(store);
                store = new Store
                {
                    Name = "Washington"
                };
                context.Add(store);
                await context.SaveChangesAsync();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                var stores = context.Stores.Select(x => x);
                //var stores = await storeRepo.();

                Assert.Equal(4, stores.Count());
            }
        }
        public async void GetsAllOrdersForCustomer()
        {
            //Arrange
            var options = InMemoryDb("GetsCustomersOrders");

            //Act
            using (var context = new MSDbContext(options))
            {
                var customer = new Customer
                {
                    Id        = 4,
                    FirstName = "Conan",
                    LastName  = "perse",
                    Email     = "*****@*****.**"
                };
                context.Add(customer);
                context.SaveChanges();
                CreateTwoproducts(context);

                var store = new Store
                {
                    Id   = 7,
                    Name = "SomeLocation"
                };
                context.Add(store);
                context.SaveChanges();

                var order = new Order
                {
                    CustomerId = 2,
                    OrderDate  = DateTime.Now,
                    StoreId    = 3,
                };
                context.Add(order);
                order = new Order
                {
                    CustomerId = 6,
                    OrderDate  = DateTime.Now,
                    StoreId    = 5,
                };
                context.Add(order);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                IHttpContextAccessor contextAccessor = new HttpContextAccessor();
                var shoppingCartRepo = new ShoppingCart(context);
                var orderRepo        = new OrderService(context, shoppingCartRepo, contextAccessor);
                var orders           = await orderRepo.FindAsync(o => o.CustomerId == 1);

                Assert.Empty(orders);
            }
        }
        public async Task DeletesOrderFromDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeletesOrderFromDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new MSDbContext(options))
            {
                Order order = new Order
                {
                    Id      = 43,
                    StoreId = 4
                };
                db.Add(order);
                await db.SaveChangesAsync();

                db.Remove(order);
                await db.SaveChangesAsync();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(0, context.Orders.Count());
            }
        }
        public async Task DeletesProductFromDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeletesProductFromDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new MSDbContext(options))
            {
                Product product = new Product
                {
                    Id   = 26,
                    Name = "drum",
                };
                db.Add(product);
                await db.SaveChangesAsync();

                db.Remove(product);
                await db.SaveChangesAsync();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(0, context.Products.Count());
            }
        }
        public void AddsProductToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsProductToDbTest")
                          .Options;

            //Act
            using (var db = new MSDbContext(options))
            {
                Product sitar = new Product
                {
                    Id    = 17,
                    Name  = "sitar",
                    Price = 100M
                };

                db.Add(sitar);
                db.SaveChanges();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(1, context.Products.Count());

                var product1 = context.Products.Where(p => p.Id == 17).FirstOrDefault();
                Assert.Equal(17, product1.Id);
                Assert.Equal("sitar", product1.Name);
            }
        }
Пример #7
0
        public async Task DeletesCustomerFromDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeletesCustomerToDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new MSDbContext(options))
            {
                CustomerAddress customerAddress = new CustomerAddress {
                    Id = 1, Street = "8286 Clay Ave.", City = "Spokane", State = "WA", Zip = "11111"
                };
                Customer customer = new Customer {
                    Id = 6, FirstName = "Maribeth", LastName = "Fontenot", Email = "*****@*****.**", PhoneNo = "1234112233", Password = "******", UserTypeId = 2, CustomerAddress = customerAddress
                };
                db.Add(customer);
                await db.SaveChangesAsync();

                db.Remove(customer);
                await db.SaveChangesAsync();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(0, context.Customers.Count());
            }
        }
        public void AddsStoreToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsStoreToDbTest")
                          .Options;

            //Act
            using (var db = new MSDbContext(options))
            {
                Store location = new Store
                {
                    Name = "Spokane"
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(1, context.Stores.Count());

                var store2 = context.Stores.Where(s => s.Id == 1).FirstOrDefault();
                Assert.Equal(1, store2.Id);
                Assert.Equal("Spokane", store2.Name);
            }
        }
        public void ShoppingCartHasItems()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "CustomerByName")
                          .Options;
            ShoppingCartItem cartItem = new ShoppingCartItem
            {
                ShoppingCartId = "1",
                customerEmail  = "*****@*****.**",
                Quantity       = 2,
                StoreId        = "5"
            };


            //Act
            using (var context = new MSDbContext(options))
            {
                context.Add(cartItem);
                context.SaveChanges();

                var result = context.ShoppingCartItems
                             .Where(c => c.customerEmail.Contains(cartItem.customerEmail))
                             .AsNoTracking().FirstOrDefault();

                //Assert
                Assert.Equal(cartItem.customerEmail, result.customerEmail);
            }
        }
Пример #10
0
        public async Task AddsCustomerToDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsCustomerToDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new MSDbContext(options))
            {
                CustomerAddress customerAddress = new CustomerAddress {
                    Id = 1, Street = "8286 Clay Ave.", City = "Spokane", State = "WA", Zip = "11111"
                };
                Customer customer = new Customer {
                    Id = 6, FirstName = "Maribeth", LastName = "Fontenot", Email = "*****@*****.**", PhoneNo = "1234112233", Password = "******", UserTypeId = 2, CustomerAddress = customerAddress
                };
                db.Add(customer);
                db.SaveChanges();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(1, context.Customers.Count());
                var customer1 = await context.Customers.Where(x => x.FirstName == "Maribeth")
                                .AsNoTracking().FirstOrDefaultAsync();

                var customer1Address = await context.Customers.
                                       Include(c => c.CustomerAddress).AsNoTracking().FirstOrDefaultAsync();

                Assert.Equal("Maribeth", customer1.FirstName);
                Assert.Equal("11111", customer1Address.CustomerAddress.Zip);
            }
        }
        public void CustomerSearchByUserByLastName()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "CustomerByName")
                          .Options;
            Customer cust = new Customer
            {
                FirstName = "Test User",
                LastName  = "Test Last"
            };

            string lastName = "Test";

            //Act
            using (var context = new MSDbContext(options))
            {
                context.Add(cust);
                context.SaveChanges();

                var result = context.Customers
                             .Where(c => c.LastName.Contains(lastName))
                             .AsNoTracking().FirstOrDefault();

                //Assert
                Assert.Equal(cust.LastName, result.LastName);
            }
        }
        private void CreateTwoproducts(MSDbContext context)
        {
            var product = new Product
            {
                Id    = 1,
                Price = 150.55M,
            };

            context.Add(product);
            product = new Product
            {
                Id    = 2,
                Price = 150.55M,
            };
            context.Add(product);
            context.SaveChanges();
        }
        public async Task <IActionResult> Create([Bind("Id,Name,CountryId,Status,Remarks")] District mSDistrict)
        {
            if (ModelState.IsValid)
            {
                _context.Add(mSDistrict);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(mSDistrict));
        }
        private void CreateOneCustomer(MSDbContext context)
        {
            var customer = new Customer
            {
                Id        = 1,
                FirstName = "Conan",
                LastName  = "perse",
                Email     = "*****@*****.**"
            };

            context.Add(customer);
            context.SaveChanges();
        }
Пример #15
0
        public async Task <IActionResult> Create([Bind("Id,Name,ProductCode,Price")] Product product)
        {
            if (ModelState.IsValid)
            {
                ViewBag.UserName = User.FindFirstValue(ClaimTypes.Name);
                ViewBag.StoreId  = User.FindFirstValue(ClaimTypes.NameIdentifier);
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
        public void GetsOrderLineItem()
        {
            var options = InMemoryDb("GetsOrderLineItems");

            //Act
            using (var context = new MSDbContext(options))
            {
                var orderLineItem = new OrderLineItem
                {
                    Id       = 45,
                    OrderId  = 5,
                    Price    = 25M,
                    Quantity = 4
                };
                context.Add(orderLineItem);
                context.SaveChanges();

                // Assert
                var result = context.OrderLineItems.Where(o => o.Id == orderLineItem.Id).AsNoTracking().FirstOrDefault();

                Assert.Equal(orderLineItem.Quantity, result.Quantity);
            }
        }
        public async void GetsAllOrdersForLocation()
        {
            //Arrange
            var options = InMemoryDb("GetsLocationOrders");

            //Act
            using (var context = new MSDbContext(options))
            {
                var customer = new Customer
                {
                    Id        = 10,
                    FirstName = "Conan",
                    LastName  = "perse",
                    Email     = "*****@*****.**"
                };
                context.Add(customer);
                context.SaveChanges();

                var product = new Product
                {
                    Id    = 11,
                    Price = 150.55M,
                };
                context.Add(product);
                product = new Product
                {
                    Id    = 12,
                    Price = 150.55M,
                };
                context.Add(product);
                context.SaveChanges();

                var store = new Store
                {
                    Id   = 4,
                    Name = "Plorida"
                };
                context.Add(store);
                context.SaveChanges();

                var order = new Order
                {
                    Id        = 13,
                    OrderDate = DateTime.Now,
                    StoreId   = 1,
                };
                context.Add(order);
                order = new Order
                {
                    Id        = 3,
                    OrderDate = DateTime.Now,
                    StoreId   = 1,
                };
                context.Add(order);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                IHttpContextAccessor contextAccessor = new HttpContextAccessor();
                var shoppingCartRepo = new ShoppingCart(context);
                var orderRepo        = new OrderService(context, shoppingCartRepo, contextAccessor);
                var orders           = await orderRepo.FindAsync(o => o.StoreId == 1 && o.Id == 2);

                Assert.Equal(0, orders.Select(i => i.Id).FirstOrDefault());
            }
        }