public async void GetsAllLocations() { //Arrange var options = BuildInMemoryDb("GetsAllLocations"); //Act using (var context = new ShoppingDbContext(options)) { var store = new Store { Location = "Location1" }; context.Add(store); store = new Store { Location = "Location2" }; context.Add(store); store = new Store { Location = "Location3" }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var storeRepo = new StoreRepository(context); var stores = await storeRepo.All(); Assert.Equal(3, stores.Count()); } }
public async void GetCustomerByEmail() { //Arrange var options = BuildInMemoryDb("GetsCustomerByEmail"); string fName = "Bob", lName = "Dole", email = "*****@*****.**"; int id = 1; //Act using (var context = new ShoppingDbContext(options)) { var customer = new Customer { CustomerId = id, FirstName = fName, LastName = lName, Email = email }; context.Add(customer); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var customerRepo = new CustomerRepository(context); var list = await customerRepo.Find(c => c.Email == email); var customerInfo = list.FirstOrDefault(); Assert.Equal(id, customerInfo.CustomerId); Assert.Equal(fName, customerInfo.FirstName); Assert.Equal(lName, customerInfo.LastName); Assert.Equal(email, customerInfo.Email); } }
public async virtual Task <T> Add(T entity) { var ent = _context.Add(entity).Entity; await _context.SaveChangesAsync(); return(ent); }
public async void GetsAllOrdersForCustomer() { //Arrange var options = BuildInMemoryDb("GetsCustomersOrders"); //Act using (var context = new ShoppingDbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1" }; context.Add(store); context.SaveChanges(); var order = new Order { CusomerId = 1, OrderDateTime = DateTime.Now, OrderId = 1, StoreId = 1, }; context.Add(order); order = new Order { CusomerId = 1, OrderDateTime = DateTime.Now, OrderId = 2, StoreId = 1, }; context.Add(order); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var orderRepo = new OrderRepository(context); var orders = await orderRepo.Find(o => o.CusomerId == 1); Assert.Equal(2, orders.Count()); } }
private void CreateTwoproducts(ShoppingDbContext context) { var product = new Product { PoductId = 1, Price = 2.99, ProductDescription = "Prod1" }; context.Add(product); product = new Product { PoductId = 2, Price = 5.99, ProductDescription = "Prod2" }; context.Add(product); context.SaveChanges(); }
public void ThrowsOnNegativeInventory() { //Arrange var options = BuildInMemoryDb("ThrowsException"); //Act using (var context = new ShoppingDbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 } } }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var unitOfWork = new UnitOfWork(context); var prods = new List <OrderDetails>() { new OrderDetails() { ProductId = 1, Quantity = 12, PricePaid = 5.99 } }; var order = new Order { ProductsOrdered = prods, CusomerId = 1, StoreId = 1 }; //orderId = unitOfWork.OrderRepository.Add(order).Result.OrderId; Assert.ThrowsAny <Exception>(() => unitOfWork.OrderRepository.Add(order).Result); } }
private void CreateOneCustomer(ShoppingDbContext context) { var customer = new Customer { CustomerId = 1, FirstName = "Jim", LastName = "Bob", Email = "*****@*****.**" }; context.Add(customer); context.SaveChanges(); }
public async void GetsAllCustomers() { //Arrange var options = BuildInMemoryDb("GetsAllCustomers"); //Act using (var context = new ShoppingDbContext(options)) { var customer = new Customer { CustomerId = 1, FirstName = "Jim", LastName = "Bob", Email = "*****@*****.**" }; context.Add(customer); customer = new Customer { CustomerId = 2, FirstName = "Jane", LastName = "Doe", Email = "*****@*****.**" }; context.Add(customer); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var customerRepo = new CustomerRepository(context); var customers = await customerRepo.All(); Assert.Equal(2, customers.Count()); } }
public async void GetsCorrectInventoryCount() { //Arrange var options = BuildInMemoryDb("GetsInventoryCount"); //Act using (var context = new ShoppingDbContext(options)) { CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new ShoppingDbContext(options)) { var inventoryRepo = new InventoryRepository(context); var items = await inventoryRepo.Find(i => i.StoreId == 1); Assert.Equal(2, items.Count()); } }
public void CancelsOrderOnNegativeInventory() { //Arrange var options = BuildInMemoryDb("CancelsOrder"); //Act using (var context = new ShoppingDbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); try { var unitOfWork = new UnitOfWork(context); var prods = new List <OrderDetails>() { new OrderDetails() { ProductId = 1, Quantity = 12, PricePaid = 5.99 } }; var order = new Order { ProductsOrdered = prods, CusomerId = 1, StoreId = 1 }; unitOfWork.OrderRepository.Add(order); } catch { } } //Assert using (var context = new ShoppingDbContext(options)) { var orders = (from o in context.Orders select o).ToList(); Assert.Empty(orders); } }
public void DecrementsInventoryOnOrder() { //Arrange var options = BuildInMemoryDb("DecrementsInventory"); int orderId; //Act using (var context = new ShoppingDbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); var unitOfWork = new UnitOfWork(context); var prods = new List <OrderDetails>() { new OrderDetails() { ProductId = 1, Quantity = 2, PricePaid = 5.99 } }; var order = new Order { ProductsOrdered = prods, CusomerId = 1, StoreId = 1 }; orderId = unitOfWork.OrderRepository.Add(order).Result.OrderId; } //Assert using (var context = new ShoppingDbContext(options)) { var item = (from inv in context.StoreInventories where inv.StoreId == 1 && inv.ProductId == 1 select inv).Take(1).FirstOrDefault(); Assert.Equal(8, item.Quantity); } }
public void AddsOrderToDb() { //Arrange var options = BuildInMemoryDb("AddsOrderToDb"); int orderId; //Act using (var context = new ShoppingDbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); var unitOfWork = new UnitOfWork(context); var prods = new List <OrderDetails>() { new OrderDetails() { ProductId = 1, Quantity = 2, PricePaid = 5.99 }, new OrderDetails() { ProductId = 2, Quantity = 5, PricePaid = 6.99 } }; var order = new Order { ProductsOrdered = prods, CusomerId = 1, StoreId = 1 }; orderId = unitOfWork.OrderRepository.Add(order).Result.OrderId; } //Assert using (var context = new ShoppingDbContext(options)) { var orders = from ord in context.Orders where ord.OrderId == orderId select ord; Assert.Single(orders); } }