Search in sources :

Example 6 with Pic

use of org.mongodb.morphia.query.TestQuery.Pic 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());
}
Also used : ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) Pic(org.mongodb.morphia.query.TestQuery.Pic) ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) Test(org.junit.Test)

Example 7 with Pic

use of org.mongodb.morphia.query.TestQuery.Pic in project morphia by mongodb.

the class TestQueriesOnReferences method testQueryOverReference.

@Test
public void testQueryOverReference() throws Exception {
    final ContainsPic cpk = new ContainsPic();
    final Pic p = new Pic();
    getDs().save(p);
    cpk.setPic(p);
    getDs().save(cpk);
    final Query<ContainsPic> query = getDs().find(ContainsPic.class);
    final ContainsPic object = query.field("pic").equal(p).get();
    Assert.assertNotNull(object);
}
Also used : ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) Pic(org.mongodb.morphia.query.TestQuery.Pic) ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) Test(org.junit.Test)

Example 8 with Pic

use of org.mongodb.morphia.query.TestQuery.Pic in project morphia by mongodb.

the class TestQueriesOnReferences method testQueryOverLazyReference.

@Test
public void testQueryOverLazyReference() throws Exception {
    final ContainsPic cpk = new ContainsPic();
    final Pic p = new Pic();
    getDs().save(p);
    final PicWithObjectId withObjectId = new PicWithObjectId();
    getDs().save(withObjectId);
    cpk.setLazyPic(p);
    cpk.setLazyObjectIdPic(withObjectId);
    getDs().save(cpk);
    Query<ContainsPic> query = getDs().find(ContainsPic.class);
    Assert.assertNotNull(query.field("lazyPic").equal(p).get());
    query = getDs().find(ContainsPic.class);
    Assert.assertNotNull(query.field("lazyObjectIdPic").equal(withObjectId).get());
}
Also used : ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) Pic(org.mongodb.morphia.query.TestQuery.Pic) ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) PicWithObjectId(org.mongodb.morphia.query.TestQuery.PicWithObjectId) Test(org.junit.Test)

Example 9 with Pic

use of org.mongodb.morphia.query.TestQuery.Pic in project morphia by mongodb.

the class TestUpdateOps method testUpdateKeyList.

@Test
public void testUpdateKeyList() 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);
    cpk.keys = singletonList(picKey);
    //test with Key<Pic>
    final UpdateResults res = ds.update(ds.find(ContainsPicKey.class).filter("name", cpk.name), ds.createUpdateOperations(ContainsPicKey.class).set("keys", cpk.keys), new UpdateOptions());
    assertThat(res.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.keys, hasItem(picKey));
}
Also used : Pic(org.mongodb.morphia.query.TestQuery.Pic) ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 ContainsPic (org.mongodb.morphia.query.TestQuery.ContainsPic)9 Pic (org.mongodb.morphia.query.TestQuery.Pic)9 PicWithObjectId (org.mongodb.morphia.query.TestQuery.PicWithObjectId)1 UpdateResults (org.mongodb.morphia.query.UpdateResults)1