public async void TestSaveNotExistingCategoryWithNotExistingId() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); FaqCategory ExistingCat = new FaqCategory() { Id = 1, CategoryName = "Test", }; await context.FaqCategory.AddAsync(ExistingCat); await context.SaveChangesAsync(); IFaqRepository repository = new EFFaqRepository(context); // Change some values FaqCategory catToUpdate = new FaqCategory() { Id = 2, CategoryName = "New", }; await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => repository.SaveCategory(catToUpdate)); }
public async void TestSaveNewCategory() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); context.FaqCategory.AddRange( new FaqCategory() { CategoryName = "Test" } ); await context.SaveChangesAsync(); IFaqRepository repository = new EFFaqRepository(context); FaqCategory categoryToCreate = new FaqCategory() { CategoryName = "Test" }; await repository.SaveCategory(categoryToCreate); // Check if the item was created FaqCategory foundCat = await context.FaqCategory.FirstOrDefaultAsync(x => x.CategoryName == "Test"); Assert.NotNull(foundCat); Assert.Equal("Test", foundCat.CategoryName); }
public async void TestSaveUpdatedCategory() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); FaqCategory catToUpdate = new FaqCategory() { Id = 1, CategoryName = "Test", }; await context.FaqCategory.AddAsync(catToUpdate); await context.SaveChangesAsync(); IFaqRepository repository = new EFFaqRepository(context); // Change some values catToUpdate.CategoryName = "New"; await repository.SaveCategory(catToUpdate); // Check if the item was updated FaqCategory updatedCat = await context.FaqCategory.FindAsync((long)1); Assert.NotNull(updatedCat); Assert.Equal("New", updatedCat.CategoryName); }
public async void TestSaveNullCategory() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); context.FaqCategory.AddRange( new FaqCategory() { CategoryName = "Test" } ); await context.SaveChangesAsync(); IFaqRepository repository = new EFFaqRepository(context); await Assert.ThrowsAsync <ArgumentNullException>(() => repository.SaveCategory(null)); }