Search in sources :

Example 1 with ContainsPic

use of dev.morphia.test.query.TestQuery.ContainsPic in project morphia by mongodb.

the class TestUpdateOperations method testInsertWithRef.

@Test
public void testInsertWithRef() {
    final Pic pic = new Pic();
    pic.setName("fist");
    final ObjectId picKey = getDs().save(pic).getId();
    Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", picKey));
    assertInserted(query.update(set("name", "A")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    getDs().find(ContainsPic.class).delete(new DeleteOptions().multi(true));
    query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", pic));
    assertInserted(query.update(set("name", "second")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    // test reading the object.
    final ContainsPic cp = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), is("second"));
    MatcherAssert.assertThat(cp.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is("fist"));
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) ObjectId(org.bson.types.ObjectId) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 2 with ContainsPic

use of dev.morphia.test.query.TestQuery.ContainsPic in project morphia by mongodb.

the class TestUpdateOperations method createContainsPic.

private void createContainsPic(int size) {
    final ContainsPic containsPic = new ContainsPic();
    containsPic.setSize(size);
    getDs().save(containsPic);
}
Also used : ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic)

Example 3 with ContainsPic

use of dev.morphia.test.query.TestQuery.ContainsPic in project morphia by mongodb.

the class TestUpdateOperations method testUpdateRef.

@Test
public void testUpdateRef() {
    final ContainsPic cp = new ContainsPic();
    cp.setName("cp one");
    getDs().save(cp);
    final Pic pic = new Pic();
    pic.setName("fist");
    getDs().save(pic);
    Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", cp.getName()));
    UpdateResult result = query.update(set("pic", pic)).execute();
    Assert.assertEquals(result.getModifiedCount(), 1);
    // test reading the object.
    final ContainsPic cp2 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp2, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp2.getName()));
    MatcherAssert.assertThat(cp2.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp2.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp2.getPic().getName()));
    // test reading the object.
    final ContainsPic cp3 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp3, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp3.getName()));
    MatcherAssert.assertThat(cp3.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp3.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp3.getPic().getName()));
}
Also used : FindOptions(dev.morphia.query.FindOptions) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Aggregations

ContainsPic (dev.morphia.test.query.TestQuery.ContainsPic)3 FindOptions (dev.morphia.query.FindOptions)2 Pic (dev.morphia.test.query.TestQuery.Pic)2 Test (org.testng.annotations.Test)2 UpdateResult (com.mongodb.client.result.UpdateResult)1 DeleteOptions (dev.morphia.DeleteOptions)1 UpdateOptions (dev.morphia.UpdateOptions)1 ObjectId (org.bson.types.ObjectId)1