public async Task Should_Add_Records_To_ManyToMany_Relationship_Using_Transactions(IDbProvider db) { Trace.WriteLine(TraceObjectGraphInfo(db)); // Setup var publisher = PublisherFixture.GetSecondPublisher(); var firstBook = BookFixture.GetFirstBook(publisher); var secondBook = BookFixture.GetSecondBook(publisher); var expectedAuthor = AuthorFixture.GetSecondAuthor(); expectedAuthor.AddBooks(firstBook, secondBook); // execute await db.RunInTransaction(async trans => { await trans.CreateOrUpdate(publisher); await trans.CreateOrUpdate(firstBook); await trans.CreateOrUpdate(secondBook); await trans.CreateOrUpdate(expectedAuthor); await trans.CreateOrUpdate(expectedAuthor); }); // Assert var actualAuthor = await db.Query <AuthorModel>() .Where(a => a.Email == expectedAuthor.Email) .SingleAsync(); actualAuthor.Should().NotBeNull(); var moreBooks = await db.Query <BookModel>() .ManyToManyJoin <AuthorModel>() .Where((b, a) => a.Id == actualAuthor.Id) .Select(); actualAuthor.AddBooks(moreBooks.ToArray()); // Excluding publisher info because only its ID is included in the hydration. actualAuthor.Should().BeEquivalentTo(expectedAuthor, options => options.Excluding(a => a.SelectedMemberPath.Contains("Publisher"))); }
public void Should_Add_Records_To_ManyToMany_Relationship(IDbProvider db) { Trace.WriteLine(TraceObjectGraphInfo(db)); // Setup var publisher = PublisherFixture.GetSecondPublisher(); db.Create(publisher); var firstBook = BookFixture.GetFirstBook(publisher); var secondBook = BookFixture.GetSecondBook(publisher); db.Create(firstBook); db.Create(secondBook); var expectedAuthor = AuthorFixture.GetSecondAuthor(); db.Create(expectedAuthor); // Execute expectedAuthor.AddBooks(firstBook, secondBook); db.Update(expectedAuthor); // Assert var actualAuthor = db.Query <AuthorModel>() .Where(a => a.Email == expectedAuthor.Email) .Select().Single(); actualAuthor.Should().NotBeNull(); actualAuthor.AddBooks(db.Query <BookModel>() .ManyToManyJoin <AuthorModel>() .Where((b, a) => a.Id == actualAuthor.Id) .Select().ToArray()); // Excluding publisher info because only its ID is included in the hydration. actualAuthor.ShouldBeEquivalentTo(expectedAuthor, options => options.Excluding(a => a.PropertyPath.Contains("Publisher"))); }