Search in sources :

Example 6 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestUpdateOps method testIncDec.

@Test
public void testIncDec() throws Exception {
    final Rectangle[] array = { new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10) };
    for (final Rectangle rect : array) {
        getDs().save(rect);
    }
    final Query<Rectangle> heightOf1 = getDs().find(Rectangle.class).filter("height", 1D);
    final Query<Rectangle> heightOf2 = getDs().find(Rectangle.class).filter("height", 2D);
    final Query<Rectangle> heightOf35 = getDs().find(Rectangle.class).filter("height", 3.5D);
    assertThat(getDs().getCount(heightOf1), is(3L));
    assertThat(getDs().getCount(heightOf2), is(0L));
    final UpdateResults results = getDs().update(heightOf1, getDs().createUpdateOperations(Rectangle.class).inc("height"));
    assertUpdated(results, 3);
    assertThat(getDs().getCount(heightOf1), is(0L));
    assertThat(getDs().getCount(heightOf2), is(3L));
    getDs().update(heightOf2, getDs().createUpdateOperations(Rectangle.class).dec("height"));
    assertThat(getDs().getCount(heightOf1), is(3L));
    assertThat(getDs().getCount(heightOf2), is(0L));
    getDs().update(heightOf1, getDs().createUpdateOperations(Rectangle.class).inc("height", 2.5D));
    assertThat(getDs().getCount(heightOf1), is(0L));
    assertThat(getDs().getCount(heightOf35), is(3L));
    getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 2.5D));
    assertThat(getDs().getCount(heightOf1), is(3L));
    assertThat(getDs().getCount(heightOf35), is(0L));
    getDs().update(getDs().find(Rectangle.class).filter("height", 1D), getDs().createUpdateOperations(Rectangle.class).set("height", 1D).inc("width", 20D));
    assertThat(getDs().getCount(Rectangle.class), is(5L));
    assertThat(getDs().find(Rectangle.class).filter("height", 1D).get(), is(notNullValue()));
    assertThat(getDs().find(Rectangle.class).filter("width", 30D).get(), is(notNullValue()));
    getDs().update(getDs().find(Rectangle.class).filter("width", 30D), getDs().createUpdateOperations(Rectangle.class).set("height", 2D).set("width", 2D));
    assertThat(getDs().find(Rectangle.class).filter("width", 1D).get(), is(nullValue()));
    assertThat(getDs().find(Rectangle.class).filter("width", 2D).get(), is(notNullValue()));
    getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 1));
    getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", Long.MAX_VALUE));
    getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 1.5f));
    getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", Double.MAX_VALUE));
    try {
        getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", new AtomicInteger(1)));
        fail("Wrong data type not recognized.");
    } catch (IllegalArgumentException ignore) {
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Rectangle(org.mongodb.morphia.testmodel.Rectangle) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 7 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestQuery method testNaturalSortAscending.

@Test
public void testNaturalSortAscending() {
    getDs().save(asList(new Rectangle(6, 10), new Rectangle(3, 8), new Rectangle(10, 10), new Rectangle(10, 1)));
    List<Rectangle> results = getDs().find(Rectangle.class).order(naturalAscending()).asList();
    assertEquals(4, results.size());
    Rectangle r;
    r = results.get(0);
    assertNotNull(r);
    assertEquals(6, r.getHeight(), 0);
    assertEquals(10, r.getWidth(), 0);
    r = results.get(1);
    assertNotNull(r);
    assertEquals(3, r.getHeight(), 0);
    assertEquals(8, r.getWidth(), 0);
    r = results.get(2);
    assertNotNull(r);
    assertEquals(10, r.getHeight(), 0);
    assertEquals(10, r.getWidth(), 0);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 8 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestQuery method testKeyList.

@Test
public void testKeyList() {
    final Rectangle rect = new Rectangle(1000, 1);
    final Key<Rectangle> rectKey = getDs().save(rect);
    assertEquals(rectKey.getId(), rect.getId());
    final FacebookUser fbUser1 = new FacebookUser(1, "scott");
    final FacebookUser fbUser2 = new FacebookUser(2, "tom");
    final FacebookUser fbUser3 = new FacebookUser(3, "oli");
    final FacebookUser fbUser4 = new FacebookUser(4, "frank");
    final Iterable<Key<FacebookUser>> fbKeys = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(1, fbUser1.getId());
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<Key<FacebookUser>>();
    for (final Key<FacebookUser> key : fbKeys) {
        fbUserKeys.add(key);
    }
    assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
    assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
    assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
    assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
    final KeysKeysKeys k1 = new KeysKeysKeys(rectKey, fbUserKeys);
    final Key<KeysKeysKeys> k1Key = getDs().save(k1);
    assertEquals(k1.getId(), k1Key.getId());
    final KeysKeysKeys k1Loaded = getDs().get(k1);
    for (final Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertNotNull(k1Loaded.getRect().getId());
}
Also used : KeysKeysKeys(org.mongodb.morphia.TestDatastore.KeysKeysKeys) Rectangle(org.mongodb.morphia.testmodel.Rectangle) ArrayList(java.util.ArrayList) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 9 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestQuery method testNaturalSortDescending.

@Test
public void testNaturalSortDescending() {
    getDs().save(asList(new Rectangle(6, 10), new Rectangle(3, 8), new Rectangle(10, 10), new Rectangle(10, 1)));
    List<Rectangle> results = getDs().find(Rectangle.class).order(naturalDescending()).asList();
    assertEquals(4, results.size());
    Rectangle r;
    r = results.get(0);
    assertNotNull(r);
    assertEquals(10, r.getHeight(), 0);
    assertEquals(1, r.getWidth(), 0);
    r = results.get(1);
    assertNotNull(r);
    assertEquals(10, r.getHeight(), 0);
    assertEquals(10, r.getWidth(), 0);
    r = results.get(2);
    assertNotNull(r);
    assertEquals(3, r.getHeight(), 0);
    assertEquals(8, r.getWidth(), 0);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 10 with Rectangle

use of org.mongodb.morphia.testmodel.Rectangle in project morphia by mongodb.

the class TestQuery method testAliasedFieldSortOld.

@Test
@SuppressWarnings("deprecation")
public void testAliasedFieldSortOld() {
    getDs().save(asList(new Rectangle(1, 10), new Rectangle(3, 8), new Rectangle(6, 10), new Rectangle(10, 10), new Rectangle(10, 1)));
    Rectangle r1 = getDs().find(Rectangle.class).limit(1).order("w").get();
    assertNotNull(r1);
    assertEquals(1, r1.getWidth(), 0);
    r1 = getDs().find(Rectangle.class).limit(1).order("-w").get();
    assertNotNull(r1);
    assertEquals(10, r1.getWidth(), 0);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 Rectangle (org.mongodb.morphia.testmodel.Rectangle)23 BasicDBObject (com.mongodb.BasicDBObject)9 ObjectId (org.bson.types.ObjectId)5 Circle (org.mongodb.morphia.testmodel.Circle)4 DBObject (com.mongodb.DBObject)3 Shape (org.mongodb.morphia.testmodel.Shape)3 DBCollection (com.mongodb.DBCollection)2 Random (java.util.Random)2 Key (org.mongodb.morphia.Key)2 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)2 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)2 DBRef (com.mongodb.DBRef)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 KeysKeysKeys (org.mongodb.morphia.TestDatastore.KeysKeysKeys)1 UsesCustomIdObject (org.mongodb.morphia.TestMapper.UsesCustomIdObject)1 UpdateResults (org.mongodb.morphia.query.UpdateResults)1 ShapeShifter (org.mongodb.morphia.testmodel.ShapeShifter)1