Пример #1
0
        public void Should_Query_ManyToOne_Records_With_Needing_To_Build_Join(IDbProvider db)
        {
            Trace.WriteLine(TraceObjectGraphInfo(db));

            // Setup
            var publisher = PublisherFixture.GetThirdPublisher();

            db.Create(publisher);

            var thirdBook  = BookFixture.GetThirdBook(publisher);
            var fourthBook = BookFixture.GetFourthBook(publisher);

            // Execute
            db.Create(thirdBook);
            db.Create(fourthBook);

            // Assert
            var query = db.Query <BookModel>()
                        .Where(b => b.Publisher.Id == publisher.Id);
            var actualBooks = query.ToList();

            actualBooks.Should().HaveCount(2);
            actualBooks[0].Publisher.Id.Should().Be(publisher.Id);
            actualBooks[1].Publisher.Id.Should().Be(publisher.Id);
        }
Пример #2
0
        public async Task Should_Remove_Records_From_ManyToMany_Relationship(IDbProvider db)
        {
            Trace.WriteLine(TraceObjectGraphInfo(db));

            // Setup
            var publisher = PublisherFixture.GetThirdPublisher();
            await db.Create(publisher);

            var firstBook  = BookFixture.GetFirstBook(publisher);
            var secondBook = BookFixture.GetSecondBook(publisher);
            await db.Create(firstBook);

            await db.Create(secondBook);

            var expectedAuthor = AuthorFixture.GetThirdAuthor();

            expectedAuthor.AddBooks(firstBook, secondBook);
            await db.Create(expectedAuthor);

            // Execute
            expectedAuthor.RemoveBooks(firstBook);
            await db.Update(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")));
        }
Пример #3
0
        public void Should_Remove_Records_From_ManyToMany_Relationship_Using_Transactions(IDbProvider db)
        {
            Trace.WriteLine(TraceObjectGraphInfo(db));

            // Setup
            var publisher      = PublisherFixture.GetThirdPublisher();
            var firstBook      = BookFixture.GetFirstBook(publisher);
            var secondBook     = BookFixture.GetSecondBook(publisher);
            var expectedAuthor = AuthorFixture.GetThirdAuthor();

            expectedAuthor.RemoveBooks(firstBook);
            expectedAuthor.AddBooks(firstBook, secondBook);

            db.RunInTransaction(trans =>
            {
                trans.Create(publisher);
                trans.Create(firstBook);
                trans.Create(secondBook);
                trans.Create(expectedAuthor);
                trans.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")));
        }