DATAMONGO-2023 - Polishing.

Add tests verifying the behavior when using both typed and untyped aggregation.

Original Pull Request: #585
This commit is contained in:
Christoph Strobl
2018-07-09 19:38:13 +02:00
parent ee9d8768a2
commit 3ff0975ab9
2 changed files with 35 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -591,6 +591,22 @@ public class AggregationUnitTests {
assertThat($project.containsField("plts.ests"), is(true));
}
@Test // DATAMONGO-2023
public void mapsNativeKeywordsCorrectly() {
Aggregation agg = Aggregation.newAggregation(new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return new BasicDBObject("$sample", new BasicDBObject("size", "my-custom-value"));
}
});
DBObject $sample = extractPipelineElement(agg.toDbObject("collection-1", Aggregation.DEFAULT_CONTEXT), 0,
"$sample");
assertThat($sample, is(equalTo(BasicDBObjectBuilder.start("size", "my-custom-value").get())));
}
private DBObject extractPipelineElement(DBObject agg, int index, String operation) {
List<DBObject> pipeline = (List<DBObject>) agg.get("pipeline");

View File

@@ -52,6 +52,7 @@ import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
@@ -367,6 +368,23 @@ public class TypeBasedAggregationOperationContextUnitTests {
new BasicDbListBuilder().add("$nested1.value1").add("$field2.nestedValue2").get())))));
}
@Test // DATAMONGO-2023
public void mapsNativeKeywordsForTypedAggregationCorrectly() {
AggregationOperationContext context = getContext(FooPerson.class);
DBObject agg = Aggregation.newAggregation(new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return new BasicDBObject("$sample", new BasicDBObject("name", "foo"));
}
}).toDbObject("collection", context);
BasicDBObject $sample = (BasicDBObject) getPipelineElementFromAggregationAt(agg, 0).get("$sample");
assertThat($sample, is(equalTo(BasicDBObjectBuilder.start("name", "foo").get())));
}
@Document(collection = "person")
@AllArgsConstructor
public static class FooPerson {