public async Task CreateAsync_WithValidData_ShouldCreateSuccessfully() { var category = new Category { Name = "Test", }; await context.Categories.AddAsync(category); await context.SaveChangesAsync(); var subCategory = new SubCategoryServiceModel() { Name = "Test Subcategory", CategoryId = category.Id, }; var result = await service.CreateAsync(subCategory); var actualCategoryCount = context.Categories.Count(); var expectedCategoryCount = 1; Assert.True(result); Assert.AreEqual(expectedCategoryCount, actualCategoryCount); }
public async Task <bool> CreateAsync(SubCategoryServiceModel subCategoryServiceModel) { var subCategory = new SubCategory() { Name = subCategoryServiceModel.Name, CategoryId = subCategoryServiceModel.CategoryId }; if (string.IsNullOrEmpty(subCategory.Name) || string.IsNullOrWhiteSpace(subCategory.Name)) { throw new ArgumentNullException(NullOrEmptyNameErrorMessage); } if (!await categoryService.ContainsByIdAsync(subCategoryServiceModel.CategoryId)) { throw new ArgumentNullException(InvalidCategoryIdErrorMessage); } var category = await context.Categories.FirstOrDefaultAsync(c => c.Id == subCategory.CategoryId); await context.SubCategories.AddAsync(subCategory); category.SubCategories.Add(subCategory); var result = await context.SaveChangesAsync(); return(result > 0); }
public async Task GetByNameAsync_WithValidName_ShouldReturnCorrectly() { var subCategoryName = "test"; var subCategory = new SubCategory() { Name = subCategoryName, CategoryId = "test", }; await context.SubCategories.AddAsync(subCategory); await context.SaveChangesAsync(); var actualCategory = await service.GetByNameAsync(subCategory.Name); var expectedCategory = new SubCategoryServiceModel() { Name = subCategory.Name, CategoryId = subCategory.CategoryId, Id = subCategory.Id, }; AssertEx.PropertyValuesAreEquals(actualCategory, expectedCategory); }
public async Task GetByIdAsync_WithValidId_ShouldReturnCorrectly() { var subCategoryName = "test"; var subCategory = new SubCategory() { Name = subCategoryName, CategoryId = "test", }; await context.SubCategories.AddAsync(subCategory); await context.SaveChangesAsync(); var subCategoryFromDb = await context.SubCategories.FirstOrDefaultAsync(s => s.Name == subCategoryName); var actualCategory = await service.GetByIdAsync(subCategoryFromDb.Id.ToString()); var expectedCategory = new SubCategoryServiceModel() { Name = subCategory.Name, CategoryId = subCategory.CategoryId, Id = subCategory.Id, }; AssertEx.PropertyValuesAreEquals(actualCategory, expectedCategory); }
public async Task CreateAsync_WithInvalidName_ShouldThrowArgumentNullException() { var subCategoryServiceModel = new SubCategoryServiceModel() { Name = null, }; Assert.ThrowsAsync <ArgumentNullException>(async() => await service.CreateAsync(subCategoryServiceModel)); }
public async Task <SubCategoryServiceModel> GetByIdAsync(string id) { if (!await ContainsByIdAsync(id)) { throw new ArgumentException(InvalidIdErrorMessage); } var subCategory = await context.SubCategories.SingleOrDefaultAsync(s => s.Id == id); var subCategoryServiceModel = new SubCategoryServiceModel() { Id = subCategory.Id, Name = subCategory.Name, CategoryId = subCategory.CategoryId }; return(subCategoryServiceModel); }
public async Task <SubCategoryServiceModel> GetByNameAsync(string name) { var subCategory = await context.SubCategories.SingleOrDefaultAsync(s => s.Name == name); if (subCategory == null) { throw new ArgumentException(InvalidNameErrorMessage); } var subCategoryServiceModel = new SubCategoryServiceModel() { Id = subCategory.Id, Name = subCategory.Name, CategoryId = subCategory.CategoryId }; return(subCategoryServiceModel); }
public async Task <bool> CreateAllAsync(IList <string> names, string categoryId) { if (names == null) { throw new ArgumentNullException(NullCategoryNamesListErrorMessage); } for (int i = 0; i < names.Count; i++) { var subCategoryServiceModel = new SubCategoryServiceModel() { Name = names[i], CategoryId = categoryId }; await CreateAsync(subCategoryServiceModel); } var result = await context.SaveChangesAsync(); return(result > 0); }
public async Task GetAllByCategoryId_ShouldReturnCorrectly() { var categoryName = "test"; var category = new Category { Name = categoryName, }; await context.Categories.AddAsync(category); await context.SaveChangesAsync(); var subCategoryOne = new SubCategory { Name = "test", CategoryId = category.Id, }; var subCategoryTwo = new SubCategory { Name = "test2", CategoryId = category.Id, }; var subCategoryThree = new SubCategory { Name = "test3", CategoryId = "anotherId", }; await context.SubCategories.AddAsync(subCategoryOne); await context.SubCategories.AddAsync(subCategoryTwo); await context.SubCategories.AddAsync(subCategoryThree); await context.SaveChangesAsync(); var subCategories = service.GetAllByCategoryId(category.Id).ToList(); var expectedSubCategoryOne = new SubCategoryServiceModel() { Name = subCategoryOne.Name, CategoryId = subCategoryOne.CategoryId, Id = subCategoryOne.Id, }; var expectedSubCategoryTwo = new SubCategoryServiceModel { Name = subCategoryTwo.Name, CategoryId = subCategoryTwo.CategoryId, Id = subCategoryTwo.Id, }; var expectedCategoriesCount = 2; var actualCategoriesCount = subCategories.Count; Assert.AreEqual(expectedCategoriesCount, actualCategoriesCount); AssertEx.PropertyValuesAreEquals(subCategories[0], expectedSubCategoryOne); AssertEx.PropertyValuesAreEquals(subCategories[1], expectedSubCategoryTwo); }