public void GetProductByProductId() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddProductToDbTest").Options; var expectedProductId = 1; //Act using (var db = new ShopHubContext(options)) { Product watch = new Product { LocationId = 1, Name = "watch", Quantity = 30, Price = "300" }; db.Add(watch); db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { var getProductFromTempDB = context.Products.Where(p => p.Id == 1).FirstOrDefault(); Assert.Equal(expectedProductId, getProductFromTempDB.Id); } }
public void GetProductByLocationIdTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddProductToDbTest").Options; Product getProductFromTempDB = new Product(); //Act using (var db = new ShopHubContext(options)) { Product cup = new Product { LocationId = 2, Name = "cup", Quantity = 10, Price = "100" }; db.Add(cup); db.SaveChanges(); getProductFromTempDB = db.Products.Where(p => p.LocationId == 2).FirstOrDefault(); } //Check product is not null after save it to database //Assert Assert.NotNull(getProductFromTempDB); }
public void AddOrderToDbTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddOrderToDbTest").Options; //Act using (var db = new ShopHubContext(options)) { Order order = new Order { UserId = 1, ProductId = 2, Quantity = 10, Timestamp = DateTime.Now }; db.Add(order); db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { var createdOrderInTempDB = context.Orders.Where(p => p.Id == 1).FirstOrDefault(); Assert.NotNull(createdOrderInTempDB); } }
public void AddsProductToDbTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddProductToDbTest").Options; //Act using (var db = new ShopHubContext(options)) { Product cup = new Product { LocationId = 5, Name = "cup", Quantity = 10, Price = "100" }; db.Add(cup); db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { Assert.Equal(1, context.Products.Count()); var createdProductInTempDB = context.Products.Where(p => p.LocationId == 5).FirstOrDefault(); Assert.Equal("100", createdProductInTempDB.Price); Assert.Equal(10, createdProductInTempDB.Quantity); } }
public void UpdateProductToDbTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddProductToDbTest").Options; //Act using (var db = new ShopHubContext(options)) { Product cap = new Product { LocationId = 1, Name = "cap", Quantity = 20, Price = "200" }; db.Add(cap); db.SaveChanges(); //Update the quantity of recently saved product var createdProductEntity = db.Products.Where(p => p.LocationId == 1).FirstOrDefault(); createdProductEntity.Quantity = 30; createdProductEntity.Name = "Straight Cap"; db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { var getProductFromTempDB = context.Products.Where(p => p.LocationId == 1).FirstOrDefault(); Assert.Equal(30, getProductFromTempDB.Quantity); Assert.Equal("Straight Cap", getProductFromTempDB.Name); } }
public void User_AddUser_UserSuccessfullyAddedToTheDatabaseWithAllSpecifiedDetails() { // Arrange var options = new DbContextOptionsBuilder <Model.Context.ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddUserToDatabaseTest") .Options; User user = new User("John", "Smith", "Password1", new UserType()); User userRecordFromDatabase; //Act using (var dbContext = new ShopHubContext(options)) { dbContext.Add(user); dbContext.SaveChanges(); userRecordFromDatabase = dbContext.Users.First(); } //Assert Assert.NotNull(userRecordFromDatabase); Assert.Equal("John", userRecordFromDatabase.FirstName); Assert.Equal("Smith", userRecordFromDatabase.LastName); Assert.Equal("Password1", userRecordFromDatabase.Password); }
public void MinusQuantityWhenOrderPlaceToDbTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddOrderToDbTest").Options; //Act using (var db = new ShopHubContext(options)) { //Add product to db Product watch = new Product { LocationId = 1, Name = "watch", Quantity = 30, Price = "300" }; db.Add(watch); db.SaveChanges(); //Place order to db Order order = new Order { UserId = 1, ProductId = 1, Quantity = 10, Timestamp = DateTime.Now }; db.Add(order); db.SaveChanges(); //Minus Stock Quantity var createdProductInTempDB = db.Products.Where(p => p.Name.Equals("watch")).FirstOrDefault(); createdProductInTempDB.Quantity = createdProductInTempDB.Quantity - order.Quantity; db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { var createdOrderInTempDB = context.Products.Where(p => p.Name.Equals("watch")).FirstOrDefault(); Assert.Equal(20,createdOrderInTempDB.Quantity); } }
public void User_AddTwoUsersAndRemoveOneUser_OnlyOneUserRemainsInTheDatabaseWithAllSpecifiedDetails() { // Arrange var options = new DbContextOptionsBuilder <ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddTwoUsersAndRemoveOneUserTest") .Options; User user1 = new User("John", "Smith", "JohnPassword", new UserType()); User user2 = new User("Julliet", "Smith", "JullietPassword", new UserType()); List <User> databaseUserRecords; //Act using (var dbContext = new ShopHubContext(options)) { //Add both users to the datbaase dbContext.Add(user1); dbContext.Add(user2); dbContext.SaveChanges(); //Remove first user (John) from the database dbContext.Remove(user1); dbContext.SaveChanges(); databaseUserRecords = dbContext.Users.ToList(); } //Assert Assert.NotNull(databaseUserRecords); Assert.Single(databaseUserRecords); //Check only Julliet remains Assert.Equal("Julliet", databaseUserRecords.First().FirstName); Assert.Equal("Smith", databaseUserRecords.First().LastName); Assert.Equal("JullietPassword", databaseUserRecords.First().Password); Assert.DoesNotContain(databaseUserRecords, x => x.FirstName.Equals("John")); }
public void RemoveProductToDbTest() { // Arrange var options = new DbContextOptionsBuilder<ShopHubContext>() .UseInMemoryDatabase(databaseName: "AddProductToDbTest").Options; var expectedOutComes = string.Empty; //Act using (var db = new ShopHubContext(options)) { Product bottle = new Product { LocationId = 1, Name = "bottle", Quantity = 90, Price = "100" }; db.Add(bottle); db.SaveChanges(); //Remove the recently saved product var productEntity = db.Products.Where(p => p.LocationId == 1).FirstOrDefault(); db.Products.Remove(productEntity); db.SaveChanges(); } //Assert using (var context = new ShopHubContext(options)) { var getProductFromTempDB = context.Products.Where(p => p.LocationId == 1).FirstOrDefault(); if (getProductFromTempDB is null) { expectedOutComes = ""; } Assert.Equal("", expectedOutComes); } }