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); }
public void Should_Create_Records_With_ManyToOne_Relationships(IDbProvider db) { Trace.WriteLine(TraceObjectGraphInfo(db)); // Setup var publisher = PublisherFixture.GetFirstPublisher(); var firstBook = BookFixture.GetFirstBook(publisher); var secondBook = BookFixture.GetSecondBook(publisher); db.Create(publisher); db.Create(firstBook); db.Create(secondBook); // Execute var query = db.Query <BookModel>() .Join <PublisherModel>().On((b, p) => b.Publisher.Id == p.Id) .Where((b, p) => p.Id == publisher.Id); var actualBooks = query.ToList(); // Assert actualBooks.Should().HaveCount(2); actualBooks[0].Publisher.Id.Should().Be(publisher.Id); //actualBooks[0].Publisher.Name.Should().Be(publisher.Name); actualBooks[1].Publisher.Id.Should().Be(publisher.Id); //actualBooks[1].Publisher.Name.Should().Be(publisher.Name); }
public void Should_Create_Records_With_OneToMany_Relationships(IDbProvider db) { Trace.WriteLine(TraceObjectGraphInfo(db)); // Setup var publisher = PublisherFixture.GetSecondPublisher(); db.Create(publisher); var thirdBook = BookFixture.GetThirdBook(publisher); var fourthBook = BookFixture.GetFourthBook(publisher); db.Create(thirdBook); db.Create(fourthBook); // Execute var query = db.Query <PublisherModel>() .Join <BookModel>().On((p, b) => b.Publisher.Id == p.Id) .Where((p, b) => b.Name == fourthBook.Name); var publishers = query.ToList(); // Assert publishers.Should().HaveCount(1); publishers[0].ShouldBeEquivalentTo(publisher); }
public void CreateReturns401WhenUserDoesntOwnList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("create_returns_401") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); User user = new UserFixture().User(); User unauthorizedUser = new User { Email = "unauthorized test email", Avatar = "unauthorized test avatar", }; user.ReadingLists.Add(readingList); context.Users.AddRange(new List <User>() { user, unauthorizedUser }); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(unauthorizedUser); ReadingListController controller = new ReadingListController(context, session); Book newBook = new BookFixture().Book(); var result = controller.Put(readingList.ReadingListId, newBook); Assert.IsType <NotFoundResult>(result); } }
public void AddsBooksToExistingReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("adds_book_to_existing_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); User user = new UserFixture().User(); user.ReadingLists.Add(readingList); context.Users.Add(user); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); Book newBook = new BookFixture().Book(); JsonResult result = controller.Put(readingList.ReadingListId, newBook) as JsonResult; List <Book> expectedResult = context.ReadingLists .Where(r => r.ReadingListId == readingList.ReadingListId) .FirstOrDefault() .Books; Assert.Equal(readingList, result.Value); Assert.Equal(4, context.Books.Count()); Assert.Contains(newBook, expectedResult); Assert.Equal(4, readingList.Books[3].Ranking); } }
public async Task ShouldCreateBookWithReferenceToItsAuthor(IDbProvider db) { var book = BookFixture.GetFirstBook(); var id = new Guid("F6949DAD-810D-4724-A391-5C01DD9D66E8"); book.Id = id; book.ISBN = 1919; // Execute await db.Create(book); var actualBook = await db.Query <BookModel>().Where(b => b.Id == id).FirstOrDefaultAsync(); Assert.That(actualBook, Is.Not.Null); Assert.That(actualBook.Id, Is.EqualTo(id)); Assert.That(actualBook.Authors.First().Id, Is.EqualTo(AuthorFixture.GetFirstAuthor().Id)); }
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"))); }
public void Should_Create_Records_With_ManyToMany_Relationships(IDbProvider db) { Trace.WriteLine(TraceObjectGraphInfo(db)); // Setup var publisher = PublisherFixture.GetFirstPublisher(); db.Create(publisher); var firstBook = BookFixture.GetFirstBook(publisher); var secondBook = BookFixture.GetSecondBook(publisher); db.Create(firstBook); db.Create(secondBook); var expectedAuthor = AuthorFixture.GetFirstAuthor(); expectedAuthor.AddBooks(firstBook, secondBook); // Execute db.Create(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"))); }