use of org.mongodb.morphia.query.TestQuery.Pic 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.Pic 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.Pic in project morphia by mongodb.
the class TestUpdateOps method testUpdateKeyRef.
@Test
public void testUpdateKeyRef() throws Exception {
final ContainsPicKey cpk = new ContainsPicKey();
cpk.name = "cpk one";
Datastore ds = getDs();
ds.save(cpk);
final Pic pic = new Pic();
pic.setName("fist again");
final Key<Pic> picKey = ds.save(pic);
// picKey = getDs().getKey(pic);
//test with Key<Pic>
assertThat(ds.update(ds.find(ContainsPicKey.class).filter("name", cpk.name), ds.createUpdateOperations(ContainsPicKey.class).set("pic", pic), new UpdateOptions()).getUpdatedCount(), is(1));
//test reading the object.
final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).get();
assertThat(cpk2, is(notNullValue()));
assertThat(cpk.name, is(cpk2.name));
assertThat(cpk2.pic, is(notNullValue()));
assertThat(picKey, is(cpk2.pic));
ds.update(ds.find(ContainsPicKey.class).filter("name", cpk.name), ds.createUpdateOperations(ContainsPicKey.class).set("pic", picKey), new UpdateOptions());
//test reading the object.
final ContainsPicKey cpk3 = ds.find(ContainsPicKey.class).get();
assertThat(cpk3, is(notNullValue()));
assertThat(cpk.name, is(cpk3.name));
assertThat(cpk3.pic, is(notNullValue()));
assertThat(picKey, is(cpk3.pic));
}
use of org.mongodb.morphia.query.TestQuery.Pic 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.Pic 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()));
}
Aggregations