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);
}
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());
}
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);
}
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);
}
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());
}
Aggregations