Search in sources :

Example 16 with Rectangle

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

the class TestMapreduce method testMapReduce.

@Test
public void testMapReduce() throws Exception {
    final Random rnd = new Random();
    //create 100 circles and rectangles
    for (int i = 0; i < 100; i++) {
        getAds().insert("shapes", new Circle(rnd.nextDouble()));
        getAds().insert("shapes", new Rectangle(rnd.nextDouble(), rnd.nextDouble()));
    }
    final String map = "function () { if(this['radius']) { emit('circle', {count:1}); return; } emit('rect', {count:1}); }";
    final String reduce = "function (key, values) { var total = 0; for ( var i=0; i<values.length; i++ ) {total += values[i].count;} " + "return { count : total }; }";
    final MapreduceResults<ResultEntity> mrRes = getDs().mapReduce(new MapReduceOptions<ResultEntity>().outputType(OutputType.REPLACE).query(getAds().find(Shape.class)).map(map).reduce(reduce).resultType(ResultEntity.class));
    Assert.assertEquals(2, mrRes.createQuery().count());
    Assert.assertEquals(100, mrRes.createQuery().get().getValue().count, 0);
    final MapreduceResults<ResultEntity> inline = getDs().mapReduce(new MapReduceOptions<ResultEntity>().outputType(OutputType.INLINE).query(getAds().find(Shape.class)).map(map).reduce(reduce).resultType(ResultEntity.class));
    final Iterator<ResultEntity> iterator = inline.iterator();
    Assert.assertEquals(2, count(iterator));
    Assert.assertEquals(100, inline.iterator().next().getValue().count, 0);
}
Also used : Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Random(java.util.Random) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 17 with Rectangle

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

the class TestIdField method testIdFieldNameMapping.

@Test
public void testIdFieldNameMapping() throws Exception {
    final Rectangle r = new Rectangle(1, 12);
    final BasicDBObject dbObj = (BasicDBObject) getMorphia().toDBObject(r);
    assertFalse(dbObj.containsField("id"));
    assertTrue(dbObj.containsField(Mapper.ID_KEY));
    //_id, h, w, className
    assertEquals(4, dbObj.size());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 18 with Rectangle

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

the class TestIdField method testKeyAsId.

@Test
public void testKeyAsId() throws Exception {
    getMorphia().map(KeyAsId.class);
    final Rectangle r = new Rectangle(1, 1);
    //        Rectangle r2 = new Rectangle(11,11);
    final Key<Rectangle> rKey = getDs().save(r);
    //        Key<Rectangle> r2Key = ds.save(r2);
    final KeyAsId kai = new KeyAsId(rKey);
    final Key<KeyAsId> kaiKey = getDs().save(kai);
    final KeyAsId kaiLoaded = getDs().get(KeyAsId.class, rKey);
    assertNotNull(kaiLoaded);
    assertNotNull(kaiKey);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) Test(org.junit.Test)

Example 19 with Rectangle

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

the class TestQuery method testGetByKeysHetero.

@Test
public void testGetByKeysHetero() {
    final Iterable<Key<Object>> keys = getDs().save(asList(new FacebookUser(1, "scott"), new Rectangle(1, 1)));
    final List<Object> entities = getDs().getByKeys(keys);
    assertNotNull(entities);
    assertEquals(2, entities.size());
    int userCount = 0;
    int rectCount = 0;
    for (final Object o : entities) {
        if (o instanceof Rectangle) {
            rectCount++;
        } else if (o instanceof FacebookUser) {
            userCount++;
        }
    }
    assertEquals(1, rectCount);
    assertEquals(1, userCount);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UsesCustomIdObject(org.mongodb.morphia.TestMapper.UsesCustomIdObject) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 20 with Rectangle

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

the class TestQuery method testCompoundSortWithSortBeans.

@Test
public void testCompoundSortWithSortBeans() {
    List<Rectangle> list = asList(new Rectangle(1, 10), new Rectangle(3, 8), new Rectangle(6, 10), new Rectangle(10, 10), new Rectangle(10, 1));
    Collections.shuffle(list);
    getDs().save(list);
    compareLists(list, getDs().find(Rectangle.class).order("width,-height"), getDs().find(Rectangle.class).order(ascending("width"), descending("height")), new RectangleComparator());
    compareLists(list, getDs().find(Rectangle.class).order("-height,-width"), getDs().find(Rectangle.class).order(descending("height"), descending("width")), new RectangleComparator1());
    compareLists(list, getDs().find(Rectangle.class).order("width,height"), getDs().find(Rectangle.class).order(ascending("width"), ascending("height")), new RectangleComparator2());
    compareLists(list, getDs().find(Rectangle.class).order("width,height"), getDs().find(Rectangle.class).order("width, height"), new RectangleComparator3());
}
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