use of org.mongodb.morphia.query.TestQuery.ContainsPic in project morphia by mongodb.
the class TestQueriesOnReferences method testMissingReferences.
@Test(expected = MappingException.class)
public void testMissingReferences() {
final ContainsPic cpk = new ContainsPic();
final Pic p = new Pic();
cpk.setPic(p);
getDs().save(p);
getDs().save(cpk);
getDs().delete(p);
getDs().find(ContainsPic.class).asList();
}
use of org.mongodb.morphia.query.TestQuery.ContainsPic in project morphia by mongodb.
the class TestUpdateOps method testInsertWithRef.
@Test
public void testInsertWithRef() throws Exception {
final Pic pic = new Pic();
pic.setName("fist");
final Key<Pic> picKey = getDs().save(pic);
assertInserted(getDs().update(getDs().find(ContainsPic.class).filter("name", "first").filter("pic", picKey), getDs().createUpdateOperations(ContainsPic.class).set("name", "A"), new UpdateOptions().upsert(true)));
assertThat(getDs().find(ContainsPic.class).count(), is(1L));
getDs().delete(getDs().find(ContainsPic.class));
assertInserted(getDs().update(getDs().find(ContainsPic.class).filter("name", "first").filter("pic", pic), getDs().createUpdateOperations(ContainsPic.class).set("name", "second"), new UpdateOptions().upsert(true)));
assertThat(getDs().find(ContainsPic.class).count(), is(1L));
//test reading the object.
final ContainsPic cp = getDs().find(ContainsPic.class).get();
assertThat(cp, is(notNullValue()));
assertThat(cp.getName(), is("second"));
assertThat(cp.getPic(), is(notNullValue()));
assertThat(cp.getPic().getName(), is(notNullValue()));
assertThat(cp.getPic().getName(), is("fist"));
}
use of org.mongodb.morphia.query.TestQuery.ContainsPic in project morphia by mongodb.
the class TestQueriesOnReferences method testWithKeyQuery.
@Test
public void testWithKeyQuery() {
final ContainsPic cpk = new ContainsPic();
final Pic p = new Pic();
cpk.setPic(p);
getDs().save(p);
getDs().save(cpk);
ContainsPic containsPic = getDs().find(ContainsPic.class).field("pic").equal(new Key<Pic>(Pic.class, "Pic", p.getId())).get();
Assert.assertEquals(cpk.getId(), containsPic.getId());
containsPic = getDs().find(ContainsPic.class).field("pic").equal(new Key<Pic>(Pic.class, "Pic", p.getId())).get();
Assert.assertEquals(cpk.getId(), containsPic.getId());
}
use of org.mongodb.morphia.query.TestQuery.ContainsPic in project morphia by mongodb.
the class TestUpdateOps method testUpdateRef.
@Test
public void testUpdateRef() throws Exception {
final ContainsPic cp = new ContainsPic();
cp.setName("cp one");
getDs().save(cp);
final Pic pic = new Pic();
pic.setName("fist");
final Key<Pic> picKey = getDs().save(pic);
//test with Key<Pic>
assertThat(getDs().update(getDs().find(ContainsPic.class).filter("name", cp.getName()), getDs().createUpdateOperations(ContainsPic.class).set("pic", pic), new UpdateOptions()).getUpdatedCount(), is(1));
//test reading the object.
final ContainsPic cp2 = getDs().find(ContainsPic.class).get();
assertThat(cp2, is(notNullValue()));
assertThat(cp.getName(), is(cp2.getName()));
assertThat(cp2.getPic(), is(notNullValue()));
assertThat(cp2.getPic().getName(), is(notNullValue()));
assertThat(pic.getName(), is(cp2.getPic().getName()));
getDs().update(getDs().find(ContainsPic.class).filter("name", cp.getName()), getDs().createUpdateOperations(ContainsPic.class).set("pic", picKey), new UpdateOptions());
//test reading the object.
final ContainsPic cp3 = getDs().find(ContainsPic.class).get();
assertThat(cp3, is(notNullValue()));
assertThat(cp.getName(), is(cp3.getName()));
assertThat(cp3.getPic(), is(notNullValue()));
assertThat(cp3.getPic().getName(), is(notNullValue()));
assertThat(pic.getName(), is(cp3.getPic().getName()));
}
use of org.mongodb.morphia.query.TestQuery.ContainsPic in project morphia by mongodb.
the class TestQueriesOnReferences method testKeyExists.
@Test
public void testKeyExists() {
final ContainsPic cpk = new ContainsPic();
final Pic p = new Pic();
cpk.setPic(p);
getDs().save(p);
getDs().save(cpk);
Assert.assertNotNull(getDs().find(ContainsPic.class).field("pic").exists().project("pic", true).get());
Assert.assertNull(getDs().find(ContainsPic.class).field("pic").doesNotExist().project("pic", true).get());
}
Aggregations