Search in sources :

Example 1 with Book

use of dev.morphia.test.models.Book in project morphia by mongodb.

the class TestReferences method testAggregationLookups.

@Test
public void testAggregationLookups() {
    final Author author = new Author("Jane Austen");
    getDs().save(author);
    List<Book> books = List.of(new Book("Sense and Sensibility", author), new Book("Pride and Prejudice", author), new Book("Mansfield Park", author), new Book("Emma", author), new Book("Northanger Abbey", author));
    getDs().save(books);
    author.setList(books);
    author.setSet(new HashSet<>(books));
    // Map<String, Book> map = addBookMap(author);
    getDs().save(author);
    Aggregation<Author> aggregation = getDs().aggregate(Author.class).lookup(lookup(Book.class).as("set").foreignField("_id").localField("set")).lookup(lookup(Book.class).as("list").foreignField("_id").localField("list"));
    final Author loaded = aggregation.execute(Author.class).tryNext();
    assertListEquals(author.list, loaded.list);
    assertListEquals(author.set, loaded.set);
    // validateMap(map, loaded);
    Book foundBook = getDs().aggregate(Book.class).lookup(lookup(Author.class).as("author").foreignField("_id").localField("author")).unwind(unwind("author")).execute(Book.class).next();
    assertTrue(foundBook.author.isResolved());
    assertEquals(author, foundBook.author.get());
}
Also used : Book(dev.morphia.test.models.Book) Author(dev.morphia.test.models.Author) Test(org.testng.annotations.Test)

Example 2 with Book

use of dev.morphia.test.models.Book in project morphia by mongodb.

the class TestUpdateOperations method testSetUnset.

@Test
public void testSetUnset() {
    Datastore ds = getDs();
    final ObjectId key = ds.save(new Circle(1)).getId();
    Query<Circle> circle = ds.find(Circle.class).filter(eq("radius", 1D));
    assertUpdated(circle.update(set("radius", 2D)).execute(), 1);
    Query<Circle> idQuery = ds.find(Circle.class).filter(eq("_id", key));
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(2D));
    circle = ds.find(Circle.class).filter(eq("radius", 2D));
    assertUpdated(circle.update(unset("radius")).execute(new UpdateOptions().multi(false)), 1);
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(0D));
    Book article = new Book();
    ds.save(article);
    Query<Book> query = ds.find(Book.class);
    query.update(set("title", "Some Title")).execute();
    query.update(unset("title")).execute();
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) Book(dev.morphia.test.models.Book) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 3 with Book

use of dev.morphia.test.models.Book in project morphia by mongodb.

the class TestMapping method testReferenceWithoutIdValue.

@Test
public void testReferenceWithoutIdValue() {
    assertThrows(ReferenceException.class, () -> {
        getMapper().map(Book.class, Author.class);
        final Book book = new Book();
        book.setAuthor(new Author());
        getDs().save(book);
    });
}
Also used : Book(dev.morphia.test.models.Book) Author(dev.morphia.test.models.Author) Test(org.testng.annotations.Test)

Aggregations

Book (dev.morphia.test.models.Book)3 Test (org.testng.annotations.Test)3 Author (dev.morphia.test.models.Author)2 Datastore (dev.morphia.Datastore)1 UpdateOptions (dev.morphia.UpdateOptions)1 Circle (dev.morphia.test.models.Circle)1 ObjectId (org.bson.types.ObjectId)1