public void Test_All_Orders_Can_Be_Retrieved_From_Database() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); // Act result = orderService.GetOrders(); //Cleanup context.Database.EnsureDeleted(); } //Assert Assert.NotNull(result.Result); var orders = Assert.IsType <List <Order> >(result.Result); Assert.Equal(_testOrdersList.Count, orders.Count); }
public void Admin_ShouldReturnCorrectValue() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var productController = new ProductController(productService, languageService); // Act result = productController.Admin(); //Cleanup context.Database.EnsureDeleted(); } //Assert Assert.NotNull(result); var viewResult = Assert.IsType <ViewResult>(result) as ViewResult; var model = Assert.IsAssignableFrom <IOrderedEnumerable <ProductViewModel> >(viewResult.ViewData.Model); Assert.Equal(_testProductViewModels.OrderByDescending(p => p.Id), model, new ProductViewModelEqualityComparator()); }
public void Create_ShouldReturnCorrectValue() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var productController = new ProductController(productService, languageService); // Act result = productController.Create(); //Cleanup context.Database.EnsureDeleted(); } //Assert Assert.IsType <ViewResult>(result); }
public void Test_CanSaveNewProductToDb() { var cart = new Cart(); var options = new DbContextOptionsBuilder <P3Referential>() .UseInMemoryDatabase(databaseName: "Test_Database") //.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a;Trusted_Connection=True;MultipleActiveResultSets=true") .Options; ProductService productService = null; ProductViewModel newProduct = new ProductViewModel { Name = "Test Product", Description = "This is a test product", Price = "5.99", Stock = "10", Details = "" }; var product = new Product(); using (var ctx = new P3Referential(options)) { productService = new ProductService(cart, new ProductRepository(ctx), new OrderRepository(ctx), _localizer); productService.SaveProduct(newProduct); product = ctx.Product.FirstOrDefault(p => p.Name == "Test Product"); } Assert.NotNull(product); }
public void AddToCart_ProductExists_ShouldAddTheProductToCart() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; var productToAddToCart = 1; var cart = new Cart(); using (var context = new P3Referential(options)) { var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var cartController = new CartController(cart, productService); // Act result = cartController.AddToCart(productToAddToCart); } Assert.Single(cart.Lines); var line = (cart.Lines as List <CartLine>).First(); using (var context = new P3Referential(options)) { Assert.Equal(context.Product.ToList().Find(x => x.Id == productToAddToCart), line.Product, new ProductEqualityComparator()); //Cleanup context.Database.EnsureDeleted(); } }
public void Test_Product_Stock_Can_Be_Updated() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var productId = 2; var qtyToRemove = 20; using (var context = new P3Referential(options)) { var cart = new Cart(); cart.AddItem(_testProductsList.Find(x => x.Id == productId), qtyToRemove); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); // Act productService.UpdateProductQuantities(); } //Assert using (var context = new P3Referential(options)) { var products = context.Product.ToList(); var product = products.Find(x => x.Id == productId); var originalProduct = _testProductsList.Find(x => x.Id == productId); Assert.Equal(originalProduct.Quantity - qtyToRemove, product.Quantity); //Cleanup context.Database.EnsureDeleted(); } }
public void Test_Existing_Product_Can_Be_Deleted() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var productId = 2; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); // Act productService.DeleteProduct(productId); } //Assert using (var context = new P3Referential(options)) { var products = context.Product.ToList(); var doesProductExistAnyMore = products.Exists(x => x.Id == productId); Assert.False(doesProductExistAnyMore); //Cleanup context.Database.EnsureDeleted(); } }
public void Completed_ClearsTheCart() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var productToAdd = 1; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); var orderController = new OrderController(cart, orderService, null); var cartController = new CartController(cart, productService); cartController.AddToCart(productToAdd); //Check that cart has a single item Assert.Single(cart.Lines); //Act orderController.Completed(); //Verify that the cart gets cleared Assert.Empty(cart.Lines); //Cleanup context.Database.EnsureDeleted(); } }
public void AddToCart_ProductDoesNotExist_ShouldNotAddTheProductToCart() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; var productToAddToCart = 99; var cart = new Cart(); using (var context = new P3Referential(options)) { var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var cartController = new CartController(cart, productService); // Act result = cartController.AddToCart(productToAddToCart); } Assert.Empty(cart.Lines); using (var context = new P3Referential(options)) { Assert.False(context.Product.ToList().Exists(x => x.Id == productToAddToCart)); //Cleanup context.Database.EnsureDeleted(); } }
public IntegrationDbTests() { var options = new DbContextOptionsBuilder <P3Referential>(). UseSqlServer("Server=.\\SQLEXPRESS;Database=P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a;Trusted_Connection=True;MultipleActiveResultSets=true") .Options; _context = new P3Referential(options); }
public void Post_Index_ValidOrder_ShouldBeSaved() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; var productToAdd = 1; var orderToAdd = new OrderViewModel() { Name = "new order", Address = "new address", City = "new City", Zip = "new zip", Country = "new country", }; int totalOrderBefore = 0; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); var orderController = new OrderController(cart, orderService, null); var cartController = new CartController(cart, productService); cartController.AddToCart(productToAdd); totalOrderBefore = context.Order.ToList().Count; // Act result = orderController.Index(orderToAdd); } //Verify that order was added using (var context = new P3Referential(options)) { var orders = context.Order.ToList(); int totalOrdersAfter = orders.Count; //Verify that order total is increased by one Assert.Equal(totalOrderBefore + 1, totalOrdersAfter); //Get the most recent order var order = orders.Last(); var didDataMatch = orderToAdd.Name == order.Name && orderToAdd.Address == order.Address && orderToAdd.City == orderToAdd.City && orderToAdd.Zip == orderToAdd.Zip && orderToAdd.Country == orderToAdd.Country; Assert.True(didDataMatch); //Cleanup context.Database.EnsureDeleted(); } }
public void Post_Create_ValidProduct_ShouldBeSaved() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; int totalProductsBefore = 0; var productToAdd = new ProductViewModel() { Description = "product 4 desc", Details = "product 4 details", Name = "product 4 name", Price = "50.3", Stock = "500" }; using (var context = new P3Referential(options)) { totalProductsBefore = context.Product.Count(); var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var productController = new ProductController(productService, languageService); // Act result = productController.Create(productToAdd); } using (var context = new P3Referential(options)) { var productsSaved = context.Product.ToList(); int totalProductsAfter = productsSaved.Count; //Verify that product count is increased by one Assert.Equal(totalProductsBefore + 1, totalProductsAfter); var doesProductExist = productsSaved.Exists(p => p.Description == productToAdd.Description && p.Details == productToAdd.Details && p.Name == productToAdd.Name && p.Price.ToString() == productToAdd.Price && p.Quantity.ToString() == productToAdd.Stock); Assert.True(doesProductExist); //Cleanup context.Database.EnsureDeleted(); } //Other Assertions var redirectToAction = Assert.IsType <RedirectToActionResult>(result) as RedirectToActionResult; Assert.Equal("Admin", redirectToAction.ActionName); }
private void SeedTestDb(DbContextOptions <P3Referential> options) { using (var context = new P3Referential(options)) { foreach (var p in _testProductsList) { context.Product.Add(p); } context.SaveChanges(); } }
public void Post_Create_InvalidProduct_MissingPrice_ShouldNotBeSaved() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; int totalProductsBefore = 0; var productToAdd = new ProductViewModel() { Description = "product 4 desc", Details = "product 4 details", Name = "product 4 name", Stock = "500" }; using (var context = new P3Referential(options)) { totalProductsBefore = context.Product.Count(); var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, _mockLocalizer.Object); var languageService = new LanguageService(); var productController = new ProductController(productService, languageService); // Act result = productController.Create(productToAdd); } using (var context = new P3Referential(options)) { var productsSaved = context.Product.ToList(); int totalProductsAfter = productsSaved.Count; //Verify that product count did not increase by one Assert.Equal(totalProductsBefore, totalProductsAfter); var doesProductExist = productsSaved.Exists(p => p.Description == productToAdd.Description && p.Details == productToAdd.Details && p.Name == productToAdd.Name && p.Quantity.ToString() == productToAdd.Stock); Assert.False(doesProductExist); //Cleanup context.Database.EnsureDeleted(); } //Other Assertions var viewResult = Assert.IsType <ViewResult>(result) as ViewResult; Assert.False(viewResult.ViewData.ModelState.IsValid); }
protected DbContextTestBase() { var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); DbContextOptionsRealDb = new DbContextOptionsBuilder <P3Referential>().UseSqlServer(config.GetConnectionString("P3Referential")).Options; DbContextRealDb = new P3Referential(DbContextOptionsRealDb); var serviceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider(); DbContextOptionsInMemory = new DbContextOptionsBuilder <P3Referential>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .UseInternalServiceProvider(serviceProvider) .Options; DbContextInMemoryDb = new P3Referential(DbContextOptionsInMemory); }
public void Post_Index_InValidOrder_ShouldNotBeSaved() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; var orderToAdd = new OrderViewModel() { Name = "new order", Address = "new address", City = "new City", Zip = "new zip", Country = "new country", }; int totalOrderBefore = 0; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); var orderController = new OrderController(cart, orderService, _mockLocalizer.Object); var cartController = new CartController(cart, productService); totalOrderBefore = context.Order.ToList().Count; // Act result = orderController.Index(orderToAdd); } //Verify that order was added using (var context = new P3Referential(options)) { var orders = context.Order.ToList(); int totalOrdersAfter = orders.Count; //Verify that order total is not increased by one Assert.Equal(totalOrderBefore, totalOrdersAfter); //Cleanup context.Database.EnsureDeleted(); } }
private bool disposedValue = false; // To detect redundant calls public P3Referential CreateContextForInMemory() { var option = new DbContextOptionsBuilder <P3Referential>() .UseInMemoryDatabase(databaseName: "P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a") .Options; var context = new P3Referential(option); if (context != null) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } return(context); }
public void Test_New_Product_Can_Be_Saved_To_Database() { //Arrange var options = TestDbContextOptionsBuilder(); var productToAdd = new ProductViewModel() { Description = "test desc *", Details = "test details *", Name = "test product *", Price = "50.11", Stock = "501" }; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); // Act productService.SaveProduct(productToAdd); } //Assert using (var context = new P3Referential(options)) { var savedProducts = context.Product.ToList(); Assert.Single(savedProducts); Assert.IsAssignableFrom <List <Product> >(savedProducts); var savedProduct = savedProducts.Find(x => x.Id == 1); var doesDataMatch = productToAdd.Description == savedProduct.Description && productToAdd.Details == savedProduct.Details && productToAdd.Name == savedProduct.Name && productToAdd.Price == savedProduct.Price.ToString() && productToAdd.Stock == savedProduct.Quantity.ToString(); Assert.True(doesDataMatch); //Cleanup context.Database.EnsureDeleted(); } }
private void SeedTestDb(DbContextOptions <P3Referential> options) { using (var context = new P3Referential(options)) { //seed products foreach (var p in _testProductsList) { context.Product.Add(p); } context.SaveChanges(); //seed orders foreach (var o in _testOrdersList) { context.Order.Add(o); } context.SaveChanges(); } }
public P3Referential CreateContextSQLServer() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkSqlServer() .BuildServiceProvider(); var builder = new DbContextOptionsBuilder <P3Referential>(); builder.UseSqlServer($"Server=(localdb)\\mssqllocaldb;Database=P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a;Trusted_Connection=True;MultipleActiveResultSets=true") .UseInternalServiceProvider(serviceProvider); var context = new P3Referential(builder.Options); if (context != null) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } return(context); }
public void RemoveFromCart_ProductNotExist_ShouldNotRemoveProduct() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; var productToAddToCart = 1; var cart = new Cart(); using (var context = new P3Referential(options)) { var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var cartController = new CartController(cart, productService); // Act //First add item to cart result = cartController.AddToCart(productToAddToCart); //Verify that item is added Assert.Single(cart.Lines); } //Now remove item from cart using (var context = new P3Referential(options)) { var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var cartController = new CartController(cart, productService); // Act //First add item to cart result = cartController.RemoveFromCart(99); //99 does not exist in the products //Verify that item is not removed Assert.Single(cart.Lines); } }
public void Test_CanGetProductById() { var cart = new Cart(); var options = new DbContextOptionsBuilder <P3Referential>() //.UseInMemoryDatabase(databaseName: "Test_Database") .UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a;Trusted_Connection=True;MultipleActiveResultSets=true") .Options; ProductService productService = null; Product product = null; using (var ctx = new P3Referential(options)) { productService = new ProductService(cart, new ProductRepository(ctx), new OrderRepository(ctx), _localizer); product = productService.GetProductById(1); } Assert.NotNull(product); Assert.Equal(1, product.Id); }
public void Test_CanGetAllProductsAsViewModel() { var cart = new Cart(); var options = new DbContextOptionsBuilder <P3Referential>() //.UseInMemoryDatabase(databaseName: "Test_Database") .UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=P3Referential-2f561d3b-493f-46fd-83c9-6e2643e7bd0a;Trusted_Connection=True;MultipleActiveResultSets=true") .Options; ProductService productService = null; var products = new List <ProductViewModel>(); using (var ctx = new P3Referential(options)) { productService = new ProductService(cart, new ProductRepository(ctx), new OrderRepository(ctx), _localizer); products = productService.GetAllProductsViewModel(); } Assert.NotEmpty(products); Assert.IsType <List <ProductViewModel> >(products); }
public void Test_Order_Can_Be_Retrieved_From_Database_By_OrderId() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var orderId = 2; var result = (dynamic)null; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); // Act result = orderService.GetOrder(orderId); //Cleanup context.Database.EnsureDeleted(); } //Assert Assert.NotNull(result); var taskWithProduct = Assert.IsType <Task <Order> >(result); var order = Assert.IsType <Order>(taskWithProduct.Result) as Order; var expectedOrder = _testOrdersList.Find(x => x.Id == orderId); var doesDataMatch = expectedOrder.Name == order.Name && expectedOrder.Address == order.Address && expectedOrder.City == order.City && expectedOrder.Date == order.Date && expectedOrder.Country == order.Country; Assert.True(doesDataMatch); }
public void DeleteProduct_ProductShouldBeDeleted() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var result = (dynamic)null; int totalProductsBefore = 0; var productIdToDelete = 1; using (var context = new P3Referential(options)) { totalProductsBefore = context.Product.Count(); var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); var languageService = new LanguageService(); var productController = new ProductController(productService, languageService); // Act result = productController.DeleteProduct(productIdToDelete); } using (var context = new P3Referential(options)) { var productsRemaining = context.Product.ToList(); int totalProductsAfter = productsRemaining.Count; //Verify that product count is increased by one Assert.Equal(totalProductsBefore - 1, totalProductsAfter); var doesProductExist = productsRemaining.Exists(p => p.Id == productIdToDelete); Assert.False(doesProductExist); //Cleanup context.Database.EnsureDeleted(); } }
public void Test_Product_Can_Be_Retrieved_From_Database_By_ProductId() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); var productId = 2; var result = (dynamic)null; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); // Act result = productService.GetProduct(productId); //Cleanup context.Database.EnsureDeleted(); } //Assert Assert.NotNull(result); var taskWithProduct = Assert.IsType <Task <Product> >(result); var product = Assert.IsType <Product>(taskWithProduct.Result); var expectedProduct = _testProductsList.Find(x => x.Id == productId); var doesDataMatch = expectedProduct.Name == product.Name && expectedProduct.Price == product.Price && expectedProduct.Quantity == product.Quantity && expectedProduct.Details == product.Details && expectedProduct.Description == product.Description; Assert.True(doesDataMatch); }
public void TestSaveProductPopulatedProduct() { // Arrange var options = TestDbContextOptionsBuilder(); ProductViewModel testProductViewModel = new ProductViewModel { Name = "VTech CS6114 DECT 6.0", Description = "Cordless Phone", Details = "I am details", Stock = "40", Price = "32.50" }; using (var context = new P3Referential(options)) { var cart = new Cart(); var productRepository = new ProductRepository(context); var productService = new ProductService(cart, productRepository, null, null); // Act productService.SaveProduct(testProductViewModel); } using (var context = new P3Referential(options)) { var result = context.Product.ToList(); // Assert Assert.Single(result); Assert.IsAssignableFrom <List <Product> >(result); Assert.Equal("VTech CS6114 DECT 6.0", result.First().Name); context.Database.EnsureDeleted(); } }
public OrderRepository(P3Referential context) { _context = context; }
public void Test_Given_Cart_Contains_Product_ToCheckout_New_Order_Can_Be_Saved_To_Database_ShouldUpdateStockInProductsTable() { //Arrange var options = TestDbContextOptionsBuilder(); SeedTestDb(options); //This product should exist in the Products table (It is added as part of initial data seed) var productPurchased = new Product() { Id = 2, Description = "test desc 2", Details = "test details 2", Name = "test product 2", Price = 20.10, Quantity = 200 }; //Prepare the order to add var orderToAdd = new OrderViewModel() { Name = "new order", Address = "new address", City = "new City", Zip = "new zip", Country = "new country", Lines = new List <CartLine>() { new CartLine() { Product = productPurchased, Quantity = 10 } } }; using (var context = new P3Referential(options)) { var cart = new Cart(); //Add the item to the cart cart.AddItem(productPurchased, 10); var productRepository = new ProductRepository(context); var orderRepository = new OrderRepository(context); var productService = new ProductService(cart, productRepository, null, null); var orderService = new OrderService(cart, orderRepository, productService); // Act orderService.SaveOrder(orderToAdd); } //Assert using (var context = new P3Referential(options)) { var savedOrders = context.Order.Include(x => x.OrderLine).ToList(); Assert.Equal(_testOrdersList.Count + 1, savedOrders.Count); Assert.IsAssignableFrom <List <Order> >(savedOrders); //get the most recent order which is just newly added (2 were pre-existing) var savedOrder = savedOrders.Find(x => x.Id == 3); //get the orderLine of order passed in for adding var orderToAddFirstLine = orderToAdd.Lines.First(x => x.Product.Id == 2); //get the orderLine of actual saved var savedOrderFirstLine = savedOrder.OrderLine.First(x => x.ProductId == 2); var doesDataMatch = orderToAdd.Address == savedOrder.Address && orderToAdd.City == savedOrder.City && orderToAdd.Country == savedOrder.Country && orderToAdd.Lines.Count == savedOrder.OrderLine.Count && orderToAddFirstLine.Product.Id == savedOrderFirstLine.ProductId && orderToAddFirstLine.Quantity == savedOrderFirstLine.Quantity; Assert.True(doesDataMatch); //Check if product stock is reduced after the order Assert.Equal(200 - 10, context.Product.ToList().Find(x => x.Id == 2).Quantity); //Cleanup context.Database.EnsureDeleted(); } }
public ProductRepository(P3Referential context) { _context = context; }