public void Create_WithValidEntity_ShouldCreateDatabaseRecord() { // Arrange var context = new ECommerceDbContext(); var sut = new ProductRepository(context); var categoryRepo = new CategoryRepository(context); Category category = new Category { Name = "Shoes", Description = "Shoes Department" }; categoryRepo.Create(category); Product product = new Product { Name = "Rubber Shoes", Description = "This is a pair of rubber shoes.", Price = 2000, CategoryID = category.ID, ImageUrl = "rubbershoes.jpg" }; // Act sut.Create(product); // Assert var result = sut.Retrieve(product.ID); Assert.NotNull(result); // Cleanup sut.Delete(result.ID); categoryRepo.Delete(category.ID); }
public void Retrieve_WithSkipAndCount_ReturnsTheCorrectPage() { //Arrange var context = new ECommerceDbContext(); var sut = new SupplierRepository(context); for (int i = 0; i < 20; i += 1) { sut.Create(new Supplier { Name = string.Format("Supplier No. {0}", i), Description = string.Format("Description No. {0}", i), IsActive = true }); } //Act var actual = sut.Retrieve(4, 3); //Assert Assert.True(actual.Count() == 3); //Cleanup var list = sut.Retrieve(0, Int32.MaxValue); foreach (var item in list) { sut.Delete(item.ID); } }
public void Update_WithValidEntity_ShouldUpdateDatabaseRecord() { //Arrange var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); var category = new Category { Name = "Shoes", Description = "Shoes Department" }; sut.Create(category); category.Name = "Boots"; category.Description = "Shoes Department Updated"; var actual = sut.Retrieve(category.ID); //Act sut.Update(category.ID, category); var expected = sut.Retrieve(category.ID); //Assert Assert.Equal(actual.ID, expected.ID); Assert.Equal(actual.Name, expected.Name); Assert.Equal(actual.Description, expected.Description); //Cleanup sut.Delete(category.ID); }
public void Retrieve_WithVAlidEntityID_ReturnsAValidEntity() { var options = ConnectionOptionHelper.Sqlite(); //Arrange var category = new Category { Name = "Shoes", Description = "Shoes Department" }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); context.Categories.Add(category); context.SaveChanges(); } using (var context = new ECommerceDbContext(options)) { var sut = new CategoryRepository(context); // Act var actual = sut.Retrieve(category.ID); //Assert Assert.NotNull(actual); Assert.Equal(category.Name, actual.Name); Assert.Equal(category.Description, actual.Description); } }
public void Update_WithValidEntity_ShouldUpdateDatabaseRecord() { //Arrange var context = new ECommerceDbContext(); var sut = new SupplierRepository(context); var supplier = new Supplier { Name = "Melrose Mejidana", Description = "Shoe Supplier", IsActive = true }; Assert.True(supplier.ID != 0); supplier.Name = "Myel Mejidana"; supplier.Description = "Shoe Supplier will be hiatus"; supplier.IsActive = false; //Act sut.Update(supplier.ID, supplier); var actual = sut.Retrieve(supplier.ID); //Assert Assert.Equal(supplier.Name, actual.Name); Assert.Equal(supplier.Description, actual.Description); Assert.Equal(supplier.IsActive, actual.IsActive); //Cleanup sut.Delete(supplier.ID); }
public void Create_WithValidEntity_ShouldCreateDatabaseRecord() { //Arrange var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); var category = new Category { Name = "Shoes", Description = "Shoes Department" }; //Act sut.Create(category); //Assert Assert.True(category.ID != 0); var entity = sut.Retrieve(category.ID); Assert.NotNull(entity); //Cleanup sut.Delete(category.ID); }
public void Create_WithValidEntity_ShouldCreateDatabaseRecord() { var options = ConnectionOptionHelper.Sqlite(); var category = new Category { Name = "Bag", Description = "Bag Department" }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); var sut = new CategoryRepository(context); //Act sut.Create(category); } using (var context = new ECommerceDbContext(options)) { //Assert Assert.True(category.ID != 0); var entity = context.Categories.Find(category.ID); Assert.NotNull(entity); } }
public CategoriesController(ICategoryRepository repository, IProductRepository productrepository , ECommerceDbContext context) { this.repository = repository; this.productrepository = productrepository; this.context = context; }
public void Update_WithValidEntity_ShouldUpdateDatabaseRecord() { var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); var category = new Category { Name = "shoes", Description = "Shoes Department" }; sut.Create(category); var record = sut.Retrieve(category.ID); var expectedName = "Bag"; var expectedDescription = "Bag Department"; //Act record.Name = expectedName; record.Description = expectedDescription; var actual = sut.Update(record.ID, category); //Assert Assert.Equal(expectedName, actual.Name); Assert.Equal(expectedDescription, actual.Description); //Cleanup sut.Delete(actual.ID); }
public void Update_WithValidEntity_ShouldUpdateRecords() { //Arrange var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); var category = new Category { Name = "Shoes", Description = "Shoes Department" }; sut.Create(category); var actual = sut.Retrieve(category.ID); var expectedName = "Shoepatos"; var expectedDescription = "Sapatos natin"; actual.Name = expectedName; actual.Description = expectedDescription; //Act sut.Update(actual.ID, actual); //Assert var expected = sut.Retrieve(actual.ID); Assert.Equal(expectedName, expected.Name); Assert.Equal(expectedDescription, expected.Description); //Cleanup sut.Delete(actual.ID); }
public void Create_WithNotExistingCategoryID_ShouldThrowException() { var options = ConnectionOptionHelper.Sqlite(); //Arrange Product product; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); product = new Product { Name = "Sling Bag", Description = "This is a Sling Bag", Price = 199, //CategoryID = 100, ImageUrl = "slingbag.jpg" }; var sut = new ProductRepository(context); //Act &Assert Assert.Throws <DbUpdateException>(() => sut.Create(product)); } }
public List <Product> GetProducts() { using (var context = new ECommerceDbContext()) { return(context.Products.Include(x => x.Category).ToList()); } }
public Product GetProduct(int ID) { using (var context = new ECommerceDbContext()) { return(context.Products.Find(ID)); } }
public void Retrieve_WithValidEntityID_ShouldReturnAValidProduct() { // Arrange var context = new ECommerceDbContext(); var sut = new ProductRepository(context); var categoryRepo = new CategoryRepository(context); Category category = new Category { Name = "Shoes", Description = "Shoes Department" }; categoryRepo.Create(category); Product product = new Product { Name = "Rubber Shoes", Description = "This is a pair of rubber shoes.", Price = 2000, CategoryID = category.ID, ImageUrl = "rubbershoes.jpg" }; sut.Create(product); // Act var actual = sut.Retrieve(product.ID); // Assert Assert.NotNull(actual); // Cleanup sut.Delete(actual.ID); categoryRepo.Delete(category.ID); }
public List<Category> GetFeaturedCategories() { using (var context = new ECommerceDbContext()) { return context.Categories.Where(x => x.isFeatured && x.ImageURL != null).ToList(); } }
public void Retrieve_WithSkipAndCount_ReturnsTheCorrectPage() { //Arrange var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); for (var i = 1; i <= 20; i += 1) { sut.Create(new Category { Name = string.Format("Category {0}", i), Description = string.Format("Description {0}", i) }); } //Act var list = sut.Retrieve(5, 5); //Assert Assert.True(list.Count() == 5); //Cleanup list = sut.Retrieve(0, 41); foreach (var item in list) { sut.Delete(item.ID); } }
public void SaveCategory(Category category) { using (var context = new ECommerceDbContext()) { context.Categories.Add(category); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } } }
public void Create_WithValidEntity_ShouldCreateDatabaseRecord() { //Arrange var context = new ECommerceDbContext(); var sut = new ProductRepository(context); var product = new Product { Name = "Shoes", Description = "Shoes Department", Price = 100, CategoryID = 1, ImageUrl = "image.jpg", IsActive = true }; //Act sut.Create(product); //Assert Assert.True(product.ID != 0); var entity = sut.Retrieve(product.ID); Assert.NotNull(entity); //Cleanup sut.Delete(product.ID); }
public void Delete_WithValidEntityID_ShouldRemoveRecordFromDatabase() { var options = ConnectionOptionHelper.Sqlite(); //Arrange var category = new Category { Name = "Bag", Description = "Bag Department" }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); var sut = new CategoryRepository(context); context.Categories.Add(category); context.SaveChanges(); //Act sut.Delete(category.ID); context.SaveChanges(); var actual = context.Categories.Find(category.ID); //Assert Assert.Null(actual); } }
public void Delete_WithValidEntityID_ShouldRemoveRecordFromDatabase() { var options = ConnectionOptionHelper.Sqlite(); //Arrange var supplier = new Supplier { Name = "Shirt Supplier", Description = "This is a Shirt Supplier", IsActive = true }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); var sut = new SupplierRepository(context); context.Suppliers.Add(supplier); context.SaveChanges(); //Act sut.Delete(supplier.ID); context.SaveChanges(); var actual = context.Suppliers.Find(supplier.ID); //Assert Assert.Null(actual); } }
public void Update_WithValidEntity_ShouldUpdateDatabaseRecord() { var options = ConnectionOptionHelper.Sqlite(); var oldCategory = new Category { Name = "shoes", Description = "Shoes Department" }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); //Arrange context.Categories.Add(oldCategory); context.SaveChanges(); } var newName = "Bag"; var newDescription = "Bag Department"; using (var context = new ECommerceDbContext(options)) { var sut = new CategoryRepository(context); //Act var record = context.Categories.Find(oldCategory.ID); record.Name = newName; record.Description = newDescription; sut.Update(record.ID, record); var newCategory = context.Categories.Find(record.ID); //Assert Assert.Equal(newCategory.Name, newName); Assert.Equal(newCategory.Description, newDescription); } }
public void Create_WithValidEntity_ShouldCreateDatabaseRecord() { var options = ConnectionOptionHelper.Sqlite(); // Arrange var supplier = new Supplier { Name = "Shoes Supplier", Description = "This is a Shoes Supplier", IsActive = true }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); var sut = new SupplierRepository(context); // Act sut.Create(supplier); } using (var context = new ECommerceDbContext(options)) { // Assert var actual = context.Suppliers.Find(supplier.ID); Assert.NotNull(actual); Assert.Equal(supplier.Name, actual.Name); Assert.Equal(supplier.Description, actual.Description); } }
public void Retrieve_WithSkipAndCount_ShouldReturnTheCorrectRecords() { // Arrange var context = new ECommerceDbContext(); var sut = new SupplierRepository(context); for (var i = 1; i <= 10; i += 1) { sut.Create(new Supplier { Name = string.Format("Supplier {0}", i), Description = string.Format("Supplier Description {0}.", i), IsActive = true }); } // Act var list = sut.Retrieve(0, 5); // Assert Assert.True(list.Count() == 5); // Cleanup list = context.Suppliers.ToList(); foreach (var item in list) { sut.Delete(item.ID); } }
public void Retrieve_WithVAlidEntityID_ReturnsAValidEntity() { var options = ConnectionOptionHelper.Sqlite(); // Arrange var supplier = new Supplier { Name = "Bag Supplier", Description = "This is a Bag Supplier" }; using (var context = new ECommerceDbContext(options)) { context.Database.OpenConnection(); context.Database.EnsureCreated(); context.Suppliers.Add(supplier); context.SaveChanges(); } using (var context = new ECommerceDbContext(options)) { var sut = new SupplierRepository(context); // Act var actual = sut.Retrieve(supplier.ID); //Assert Assert.NotNull(actual); Assert.Equal(supplier.Name, actual.Name); Assert.Equal(supplier.Description, actual.Description); } }
public void Retrieve_WithSkipAndCount_ShouldReturnValidList() { //Arrange var context = new ECommerceDbContext(); var sut = new SupplierRepository(context); for (var i = 1; i <= 20; i += 1) { sut.Create(new Supplier { Name = string.Format("Supplier {0}", i), Description = string.Format("Description {0}", i), IsActive = true }); } //Act var list = sut.Retrieve(5, 5); //Assert Assert.True(list.Count() == 5); //Cleanup list = sut.Retrieve(0, int.MaxValue).ToList(); foreach (var entity in list) { sut.Delete(entity.ID); } }
public Category GetCategory(int ID) { using (var context = new ECommerceDbContext()) { return context.Categories.Find(ID); } }
public void Update_WithValidProperty_ShouldUpdateEntity() { //Arrange var context = new ECommerceDbContext(); var sut = new SupplierRepository(context); var supplier = new Supplier { Name = "Blast Asia", Description = "Software Supplier", IsActive = true }; sut.Create(supplier); var expected = sut.Retrieve(supplier.ID); //Act expected.Name = "Blaster"; expected.Description = "Software Engineer Supplier"; sut.Update(supplier.ID, expected); var actual = sut.Retrieve(supplier.ID); //Assert Assert.Equal(actual, expected); //Cleanup sut.Delete(supplier.ID); }
public List<Category> GetCategories() { using (var context = new ECommerceDbContext()) { return context.Categories.ToList(); } }
public void Retrieve_WithSkipAndCount_ReturnsTheCorrectPage() { //Arrange var context = new ECommerceDbContext(); var sut = new CategoryRepository(context); for (var i = 1; i <= 20; i += 1) { sut.Create(new Category { Name = string.Format("Category {0}", i), Description = string.Format("Description {0}", i) }); } //Act var list = sut.Retrieve(5, 5); //Assert Assert.True(list.Count() == 5); //Cleanup list = sut.Retrieve(0, Int32.MaxValue); list.All(c => { sut.Delete(c.ID); return(true); }); //count max, then -- }
public void Retrieve_WithValidID_ReturnsValidEntity() { //Arrange var context = new ECommerceDbContext(); var cat = new CategoryRepository(context); var category = new Category { Name = "Shoe", Description = "Shoe Department" }; cat.Create(category); var fillerCategory = cat.Retrieve(category.ID); var sut = new ProductRepository(context); var product = new Product { Name = "Nike", Description = "Airmax", Price = 3000, Category = fillerCategory, ImageUrl = "picture" }; sut.Create(product); //Act var actual = sut.Retrieve(product.ID); //Assert Assert.NotNull(actual); //Cleanup sut.Delete(actual.ID); cat.Delete(category.ID); }