Search in sources :

Example 1 with Pic

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

the class TestUpdateOperations method testUpdateKeyList.

@Test
public void testUpdateKeyList() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    cpk.keys = MorphiaReference.wrap(List.of(pic));
    // test with Key<Pic>
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    final UpdateResult res = query.update(set("keys", cpk.keys)).execute();
    assertThat(res.getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    MatcherAssert.assertThat(cpk2.keys.get(), Matchers.hasItem(pic));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) 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)

Example 2 with Pic

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

the class TestUpdateOperations method testUpdateKeyRef.

@Test
public void testUpdateKeyRef() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    assertThat(query.update(set("pic", pic)).execute().getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    assertThat(cpk2.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk2.pic.get()));
    query.update(set("pic", pic)).execute();
    // test reading the object.
    final ContainsPicKey cpk3 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk3, is(notNullValue()));
    assertThat(cpk.name, is(cpk3.name));
    assertThat(cpk3.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk3.pic.get()));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) Test(org.testng.annotations.Test)

Example 3 with Pic

use of dev.morphia.test.query.TestQuery.Pic 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 4 with Pic

use of dev.morphia.test.query.TestQuery.Pic 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

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