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));
}
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()));
}
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"));
}
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()));
}
Aggregations