public async Task TestSaveChangesDeleteNoSqlOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], GetType().Name); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { sqlContext.Database.EnsureCreated(); noSqlContext.Database.EnsureCreated(); var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); await sqlContext.SaveChangesAsync(); //ATTEMPT sqlContext.Remove(book); await sqlContext.SaveChangesAsync(); //VERIFY sqlContext.Books.Count().ShouldEqual(0); var noSqlBook = noSqlContext.Books.SingleOrDefault(p => p.BookId == book.BookId); noSqlBook.ShouldBeNull(); } }
public void TestSaveChangesUpdatesNoSqlFail() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], "UNKNOWNDATABASENAME"); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { sqlContext.Database.EnsureCreated(); //ATTEMPT var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); var ex = Assert.Throws <CosmosException> (() => sqlContext.SaveChanges()); //VERIFY sqlContext.Books.Count().ShouldEqual(0); ex.Message.ShouldStartWith("Response status code does not indicate success: 404 Substatus:"); } }
public async Task TestSaveChangesIndirectUpdatesNoSqlOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], GetType().Name); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { sqlContext.Database.EnsureCreated(); noSqlContext.Database.EnsureCreated(); var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); await sqlContext.SaveChangesAsync(); } using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { //ATTEMPT var book = sqlContext.Books.Include(x => x.Reviews).Single(); book.AddReview(5, "xxx", "yyy"); await sqlContext.SaveChangesAsync(); //VERIFY sqlContext.Books.Count().ShouldEqual(1); var noSqlBook = noSqlContext.Books.Single(p => p.BookId == book.BookId); noSqlBook.AuthorsOrdered.ShouldEqual("Author1, Author2"); noSqlBook.ReviewsCount.ShouldEqual(3); } }
public async Task TestSaveChangesAsyncAddNoSqlOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], nameof(TestSqlSaveChanges)); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { await sqlContext.Database.EnsureCreatedAsync(); await noSqlContext.Database.EnsureCreatedAsync(); //ATTEMPT var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); await sqlContext.SaveChangesAsync(); //VERIFY sqlContext.Books.Count().ShouldEqual(1); var noSqlBook = noSqlContext.Books.SingleOrDefault(p => p.BookId == book.BookId); noSqlBook.ShouldNotBeNull(); noSqlBook.AuthorsOrdered.ShouldEqual("Author1, Author2"); noSqlBook.ReviewsCount.ShouldEqual(2); } }
public void TestSaveChangesSoftDeleteNoSqlOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], nameof(TestSqlSaveChanges)); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { sqlContext.Database.EnsureCreated(); noSqlContext.Database.EnsureCreated(); var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); sqlContext.SaveChanges(); //ATTEMPT book.SoftDeleted = true; sqlContext.SaveChanges(); //VERIFY sqlContext.Books.Count().ShouldEqual(0); sqlContext.Books.IgnoreQueryFilters().Count().ShouldEqual(1); var noSqlBook = noSqlContext.Books.SingleOrDefault(p => p.BookId == book.BookId); noSqlBook.ShouldBeNull(); } }
public TestScalarFunctions(ITestOutputHelper output) { _output = output; var options = this.CreateUniqueClassOptions <SqlDbContext>(); using (var context = new SqlDbContext(options)) { context.Database.EnsureCreated(); var filepath = TestData.GetFilePath(@"..\..\EfCoreSqlAndCosmos\wwwroot\AddUserDefinedFunctions.sql"); ApplyScriptsToDatabase.ExecuteScriptFileInTransaction(context, filepath); context.WipeAllDataFromDatabase(); var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); context.Add(book); context.SaveChanges(); } }
public async Task TestNoSqlBookUpdaterWithReviewsOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], GetType().Name); using var sqlContext = new SqlDbContext(_sqlOptions); using var noSqlContext = new NoSqlDbContext(builder.Options); await sqlContext.Database.EnsureCreatedAsync(); await noSqlContext.Database.EnsureCreatedAsync(); var updater = new NoSqlBookUpdater(noSqlContext); //ATTEMPT var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); var numBooksChanged = updater.FindNumBooksChanged(sqlContext); await updater.CallBaseSaveChangesWithNoSqlWriteInTransactionAsync(sqlContext, numBooksChanged, () => sqlContext.SaveChangesAsync()); //VERIFY numBooksChanged.ShouldEqual(1); sqlContext.Books.Count().ShouldEqual(1); var noSqlResult = noSqlContext.Books.Find(book.BookId); noSqlResult.ReviewsCount.ShouldEqual(2); noSqlResult.ReviewsAverageVotes.ShouldEqual(3); }
public void TestSaveChangesDirectUpdatesNoSqlOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], GetType().Name); using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { sqlContext.Database.EnsureCreated(); noSqlContext.Database.EnsureCreated(); var book = DddEfTestData.CreateDummyBookTwoAuthorsTwoReviews(); sqlContext.Add(book); sqlContext.SaveChanges(); } using (var noSqlContext = new NoSqlDbContext(builder.Options)) using (var sqlContext = new SqlDbContext(_sqlOptions, new NoSqlBookUpdater(noSqlContext))) { //ATTEMPT var book = sqlContext.Books.Single(); book.PublishedOn = DddEfTestData.DummyBookStartDate.AddDays(1); sqlContext.SaveChanges(); //VERIFY sqlContext.Books.Count().ShouldEqual(1); var noSqlBook = noSqlContext.Books.Single(p => p.BookId == book.BookId); noSqlBook.PublishedOn.ShouldEqual(DddEfTestData.DummyBookStartDate.AddDays(1)); noSqlBook.AuthorsOrdered.ShouldEqual("Author1, Author2"); noSqlBook.ReviewsCount.ShouldEqual(2); } }