public async Task TestNoSqlBookUpdaterOk() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], nameof(TestNoSqlBookUpdaterAsync)); 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.CreateDummyBookOneAuthor(); sqlContext.Add(book); var hasUpdates = updater.FindBookChangesToProjectToNoSql(sqlContext); await updater.CallBaseSaveChangesWithNoSqlWriteInTransactionAsync(sqlContext, () => sqlContext.SaveChangesAsync()); //VERIFY hasUpdates.ShouldBeTrue(); sqlContext.Books.Count().ShouldEqual(1); (await noSqlContext.Books.CountAsync(p => p.BookId == book.BookId)).ShouldEqual(1); } }
public async Task TestSaveChangesAsyncUpdatesNoSqlFail() { //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))) { await sqlContext.Database.EnsureCreatedAsync(); //ATTEMPT var book = DddEfTestData.CreateDummyBookOneAuthor(); sqlContext.Add(book); var ex = await Assert.ThrowsAsync <CosmosException>(async() => await sqlContext.SaveChangesAsync()); //VERIFY sqlContext.Books.Count().ShouldEqual(0); ex.Message.ShouldStartWith("Response status code does not indicate success: 404 Substatus:"); } }
public async Task TestNoSqlBookUpdaterOk() { //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.CreateDummyBookOneAuthor(); sqlContext.Add(book); var numBooksChanged = updater.FindNumBooksChanged(sqlContext); await updater.CallBaseSaveChangesWithNoSqlWriteInTransactionAsync(sqlContext, numBooksChanged, () => sqlContext.SaveChangesAsync()); //VERIFY numBooksChanged.ShouldEqual(1); sqlContext.Books.Count().ShouldEqual(1); noSqlContext.Books.Find(book.BookId).ShouldNotBeNull(); }
public async Task TestNoSqlBookUpdaterFail_NoBookAddedToSqlDatabase() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], "UNKNOWNDATASBASENAME"); using (var sqlContext = new SqlDbContext(_sqlOptions)) using (var noSqlContext = new NoSqlDbContext(builder.Options)) { await sqlContext.Database.EnsureCreatedAsync(); var updater = new NoSqlBookUpdater(noSqlContext); //ATTEMPT var book = DddEfTestData.CreateDummyBookOneAuthor(); sqlContext.Add(book); var hasUpdates = updater.FindBookChangesToProjectToNoSql(sqlContext); var ex = await Assert.ThrowsAsync <HttpException>(async() => await updater.CallBaseSaveChangesWithNoSqlWriteInTransactionAsync(sqlContext, () => sqlContext.SaveChangesAsync())); //VERIFY ex.Message.ShouldEqual("NotFound"); hasUpdates.ShouldBeTrue(); sqlContext.Books.Count().ShouldEqual(0); } }
public async Task TestNoSqlBookUpdaterFail_NoBookAddedToSqlDatabase() { //SETUP var config = AppSettings.GetConfiguration(); var builder = new DbContextOptionsBuilder <NoSqlDbContext>() .UseCosmos( config["endpoint"], config["authKey"], "UNKNOWNDATASBASENAME"); using (var sqlContext = new SqlDbContext(_sqlOptions)) using (var noSqlContext = new NoSqlDbContext(builder.Options)) { await sqlContext.Database.EnsureCreatedAsync(); var updater = new NoSqlBookUpdater(noSqlContext); //ATTEMPT var book = DddEfTestData.CreateDummyBookOneAuthor(); sqlContext.Add(book); var numBooksChanged = updater.FindNumBooksChanged(sqlContext); var ex = await Assert.ThrowsAsync <CosmosException>(async() => await updater.CallBaseSaveChangesWithNoSqlWriteInTransactionAsync(sqlContext, numBooksChanged, () => sqlContext.SaveChangesAsync())); //VERIFY ex.Message.ShouldStartWith("Response status code does not indicate success: 404 Substatus:"); numBooksChanged.ShouldEqual(1); sqlContext.Books.Count().ShouldEqual(0); } }
public void TestAverageReviewSqlServerOk() { //SETUP var options = this.CreateUniqueClassOptionsWithLogging <SqlDbContext>( log => _output.WriteLine(log.Message)); using (var context = new SqlDbContext(options)) { context.Database.EnsureCreated(); context.WipeAllDataFromDatabase(); var book = DddEfTestData.CreateDummyBookOneAuthor(); book.AddReview(5, "test", "test"); context.Add(book); context.SaveChanges(); //ATTEMPT var aveReviews = context.Books .Select(p => p.Reviews.Select(y => (double?)y.NumStars).Average()) .Single(); //VERIFY aveReviews.ShouldEqual(5); } }
public void TestCreateOrderCorrectBookInfoOk() { //SETUP var book = DddEfTestData.CreateDummyBookOneAuthor(); //ATTEMPT var lineItems = new List <OrderBooksDto> { new OrderBooksDto(book.BookId, book, 3) }; var status = Order.CreateOrderFactory("user", DateTime.Today.AddDays(3), lineItems); //VERIFY status.HasErrors.ShouldBeFalse(); status.Result.LineItems.Count().ShouldEqual(1); status.Result.LineItems.First().NumBooks.ShouldEqual((short)3); status.Result.LineItems.First().BookPrice.ShouldEqual(book.ActualPrice); }
public void TestChangeDeliveryDateOk() { //SETUP var book = DddEfTestData.CreateDummyBookOneAuthor(); var lineItems = new List <OrderBooksDto> { new OrderBooksDto(book.BookId, book, 3) }; var order = Order.CreateOrder("user", DateTime.Today.AddDays(1), lineItems).Result; //ATTEMPT var newDeliverDate = DateTime.Today.AddDays(2); var status = order.ChangeDeliveryDate("user", newDeliverDate); //VERIFY status.IsValid.ShouldBeTrue(); order.ExpectedDeliveryDate.ShouldEqual(newDeliverDate); }
public void TestCreateOrderWithCorrectLineNumsOk() { //SETUP var book1 = DddEfTestData.CreateDummyBookOneAuthor(); var book2 = DddEfTestData.CreateDummyBookOneAuthor(); //ATTEMPT var bookOrders = new List <OrderBooksDto>() { new OrderBooksDto(book1.BookId, book1, 1), new OrderBooksDto(book2.BookId, book2, 2) }; var status = Order.CreateOrderFactory("user", DateTime.Today.AddDays(3), bookOrders); //VERIFY status.HasErrors.ShouldBeFalse(); status.Result.LineItems.Count().ShouldEqual(2); status.Result.LineItems.First().LineNum.ShouldEqual((byte)1); status.Result.LineItems.Last().LineNum.ShouldEqual((byte)2); }
public void TestAddBookOk() { //SETUP var options = SqliteInMemory.CreateOptions <SqlDbContext>(); using (var context = new SqlDbContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT context.Add(DddEfTestData.CreateDummyBookOneAuthor()); var changes = BookChangeInfo.FindBookChanges(context.ChangeTracker.Entries().ToList(), context); //VERIFY changes.Single().BookId.ShouldNotEqual(Guid.Empty); changes.Single().State.ShouldEqual(EntityState.Added); } }
public void TestChangeDeliveryDateOk() { //SETUP var book = DddEfTestData.CreateDummyBookOneAuthor(); var lineItems = new List <OrderBooksDto> { new OrderBooksDto(book.BookId, book, 3) }; var order = Order.CreateOrderFactory("user", DateTime.Today.AddDays(1), lineItems).Result; //ATTEMPT var newDeliverDate = DateTime.Today.AddDays(2); if (newDeliverDate.DayOfWeek == DayOfWeek.Sunday) { newDeliverDate = newDeliverDate.AddDays(1); } var status = order.ChangeDeliveryDate("user", newDeliverDate); //VERIFY status.HasErrors.ShouldBeFalse(); order.ExpectedDeliveryDate.ShouldEqual(newDeliverDate); }