public async Task EfDeletableEntityRepoAllWithDeletedShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "EfDeletableEntityRepoAllWithDeletedShouldWork").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Category>(dbContext); var service = new CategoriesService(repository); await service.AddAsync("test", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description"); await service.AddAsync("test2", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description2"); await service.AddAsync("test3", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description3"); var categoryToDelete = service.GetCategoryByName("test3"); repository.Delete(categoryToDelete); await repository.SaveChangesAsync(); var result = repository.AllWithDeleted().ToList(); Assert.Equal(3, result.Count); }
public async Task AddAsyncShouldThrowNullException(string name) { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Category>(dbContext); var service = new CategoriesService(repository); await Assert.ThrowsAsync <ArgumentNullException>(() => service.AddAsync(name, new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description")); }
public async Task AddAsyncShouldWork(string name) { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddAsync").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Category>(dbContext); var service = new CategoriesService(repository); await service.AddAsync(name, new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description"); Assert.True(repository.All().Any(x => x.Name == name)); }
public async Task AddAsyncThrowsIfTheInputModelIsNull() { // Arrange var mapperMock = new Mock <IMapper>(); var repositoryMock = new Mock <IDeletableEntityRepository <Category> >(); var categoriesService = new CategoriesService(repositoryMock.Object, mapperMock.Object); // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { // Act await categoriesService.AddAsync(null, new ImageInputModel()); }); }
private async Task <Category> GetOrCreateCategoryAsync(string name) { if (name.IsNullOrEmpty()) { return(null); } var category = await _categoriesService.GetByNameAsync(name); if (category == null) { category = new Category { Name = name }; await _categoriesService.AddAsync(category); } return(category); }
public async Task AddAsyncMapsTheInputModelAndAddsItToTheRepository( string name, string description, bool nullImage, string imageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var categoriesList = new List <Category>(); var repositoryMock = new Mock <IDeletableEntityRepository <Category> >(); repositoryMock.Setup(x => x.AddAsync(It.IsAny <Category>())) .Callback((Category category) => { categoriesList.Add(category); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var categoriesService = new CategoriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var categoryInput = new CategoryInputModel() { Name = name, Description = description, }; var imageInput = new ImageInputModel() { Source = imageSource, }; if (nullImage) { imageInput = null; } // Act await categoriesService.AddAsync(categoryInput, imageInput); // Assert var actualCategory = categoriesList[0]; Assert.True(saved); Assert.Equal(name, actualCategory.Name); Assert.Equal(description, actualCategory.Description); var actualImage = actualCategory.Image; if (nullImage) { Assert.Null(actualCategory.Image); } else { Assert.Equal(imageSource, actualImage.Source); } }