public void TestAddReviewToCreatedBookAndCheckReviewAddedHandlerOk() { //SETUP var showLog = false; var options = SqliteInMemory.CreateOptionsWithLogging <SqlEventsDbContext>(x => { if (showLog) { _output.WriteLine(x.DecodeMessage()); } }); var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(); context.Database.EnsureCreated(); var book = WithEventsEfTestData.CreateDummyBookOneAuthor(); context.Add(book); context.SaveChanges(); //ATTEMPT showLog = true; book.AddReview(4, "OK", "me"); context.SaveChanges(); //VERIFY book.ReviewsCount.ShouldEqual(1); book.ReviewsAverageVotes.ShouldEqual(4); }
public void TestAddReviewConcurrencyFixed() { //SETUP var options = SqliteInMemory.CreateOptions <SqlEventsDbContext>(); var config = new GenericEventRunnerConfig(); config.RegisterSaveChangesExceptionHandler <SqlEventsDbContext>(BookWithEventsConcurrencyHandler.HandleCacheValuesConcurrency); var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(config: config); context.Database.EnsureCreated(); var book = WithEventsEfTestData.CreateDummyBookOneAuthor(); context.Add(book); context.SaveChanges(); book.AddReview(4, "OK", "me"); //This simulates adding a review with NumStars of 2 before the AddReview context.Database.ExecuteSqlRaw( "UPDATE Books SET ReviewsCount = @p0, ReviewsAverageVotes = @p1 WHERE BookId = @p2", 1, 2, book.BookId); //ATTEMPT context.SaveChanges(); //VERIFY var foundBook = context.Find <BookWithEvents>(book.BookId); foundBook.ReviewsCount.ShouldEqual(2); foundBook.ReviewsAverageVotes.ShouldEqual(6.0 / 2.0); }
public void TestCreateBookWithOneAuthorAndChangeTheAuthorsName() { //SETUP var options = SqliteInMemory.CreateOptions <SqlEventsDbContext>(); { var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(); context.Database.EnsureCreated(); var book = WithEventsEfTestData.CreateDummyBookOneAuthor(); context.Add(book); context.SaveChanges(); //ATTEMPT book.AuthorsOrdered.ShouldEqual("Test Author"); book.AuthorsLink.First().Author.ChangeName("New name"); context.SaveChanges(); //VERIFY context.Books.First().AuthorsOrdered.ShouldEqual("New name"); } }
public void TestAddReviewCauseConcurrencyThrown() { //SETUP var options = SqliteInMemory.CreateOptions <SqlEventsDbContext>(); var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(); context.Database.EnsureCreated(); var book = WithEventsEfTestData.CreateDummyBookOneAuthor(); context.Add(book); context.SaveChanges(); //ATTEMPT book.AddReview(4, "OK", "me"); //This simulates adding a review with NumStars of 2 before the AddReview context.Database.ExecuteSqlRaw( "UPDATE Books SET ReviewsCount = @p0, ReviewsAverageVotes = @p1 WHERE BookId = @p2", 1, 2, book.BookId); //VERIFY Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges()); }
public void TestAddReviewToExistingBookAndCheckReviewAddedHandlerOk() { //SETUP var options = SqliteInMemory.CreateOptions <SqlEventsDbContext>(); { var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(); context.Database.EnsureCreated(); var book = WithEventsEfTestData.CreateDummyBookOneAuthor(); context.Add(book); context.SaveChanges(); } { var context = options.CreateDbWithDiForHandlers <SqlEventsDbContext, ReviewAddedHandler>(); var book = context.Books.Single(); //ATTEMPT book.AddReview(4, "OK", "me", context); context.SaveChanges(); //VERIFY book.ReviewsCount.ShouldEqual(1); book.ReviewsAverageVotes.ShouldEqual(4); } }