use of org.mongodb.morphia.aggregation.AggregationTest.Book in project morphia by mongodb.
the class TestMapreduce method testBypassDocumentValidation.
@Test
public void testBypassDocumentValidation() {
checkMinServerVersion(3.4);
getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
Document validator = Document.parse("{ count : { $gt : '10' } }");
ValidationOptions validationOptions = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.STRICT).validationAction(ValidationAction.ERROR);
MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
database.getCollection("counts").drop();
database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
final String map = "function () { emit(this.author, 1); return; }";
final String reduce = "function (key, values) { return values.length }";
MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().query(getDs().find(Book.class)).resultType(CountResult.class).outputType(OutputType.REPLACE).map(map).reduce(reduce);
try {
getDs().mapReduce(options);
fail("Document validation should have complained.");
} catch (MongoCommandException e) {
// expected
}
getDs().mapReduce(options.bypassDocumentValidation(true));
Assert.assertEquals(2, count(getDs().find(CountResult.class).iterator()));
}
use of org.mongodb.morphia.aggregation.AggregationTest.Book in project morphia by mongodb.
the class TestMapreduce method testCollation.
@Test
public void testCollation() {
checkMinServerVersion(3.4);
getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
final String map = "function () { emit(this.author, 1); return; }";
final String reduce = "function (key, values) { return values.length }";
Query<Book> query = getAds().find(Book.class).field("author").equal("dante");
MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().resultType(CountResult.class).outputType(OutputType.INLINE).query(query).map(map).reduce(reduce);
Iterator<CountResult> iterator = getDs().mapReduce(options).getInlineResults();
Assert.assertEquals(0, count(iterator));
options.inputCollection(getMorphia().getMapper().getCollectionName(Book.class)).collation(Collation.builder().locale("en").collationStrength(CollationStrength.SECONDARY).build());
iterator = getDs().mapReduce(options).getInlineResults();
CountResult result = iterator.next();
Assert.assertEquals("Dante", result.getAuthor());
Assert.assertEquals(3D, result.getCount(), 0);
}
Aggregations