|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2014 the original author or authors. |
| 2 | + * Copyright 2013-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
30 | 30 | import org.springframework.data.domain.Sort.Direction;
|
31 | 31 |
|
32 | 32 | import com.mongodb.BasicDBObject;
|
| 33 | +import com.mongodb.BasicDBObjectBuilder; |
33 | 34 | import com.mongodb.DBObject;
|
34 | 35 |
|
35 | 36 | /**
|
36 | 37 | * Unit tests for {@link Aggregation}.
|
37 | 38 | *
|
38 | 39 | * @author Oliver Gierke
|
39 | 40 | * @author Thomas Darimont
|
| 41 | + * @author Christoph Strobl |
40 | 42 | */
|
41 | 43 | public class AggregationUnitTests {
|
42 | 44 |
|
@@ -283,6 +285,40 @@ public void shouldSupportReferencingSystemVariables() {
|
283 | 285 | is((DBObject) new BasicDBObject("_id", "$someKey").append("doc", new BasicDBObject("$first", "$$ROOT"))));
|
284 | 286 | }
|
285 | 287 |
|
| 288 | + /** |
| 289 | + * @see DATAMONGO-1254 |
| 290 | + */ |
| 291 | + @Test |
| 292 | + public void shouldExposeAliasedFieldnameForProjectionsIncludingOperationsDownThePipeline() { |
| 293 | + |
| 294 | + DBObject agg = Aggregation.newAggregation(// |
| 295 | + project("date") // |
| 296 | + .and("tags").minus(10).as("tags_count")// |
| 297 | + , group("date")// |
| 298 | + .sum("tags_count").as("count")// |
| 299 | + ).toDbObject("foo", Aggregation.DEFAULT_CONTEXT); |
| 300 | + |
| 301 | + DBObject group = extractPipelineElement(agg, 1, "$group"); |
| 302 | + assertThat(getAsDBObject(group, "count"), is(new BasicDBObjectBuilder().add("$sum", "$tags_count").get())); |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * @see DATAMONGO-1254 |
| 307 | + */ |
| 308 | + @Test |
| 309 | + public void shouldUseAliasedFieldnameForProjectionsIncludingOperationsDownThePipelineWhenUsingSpEL() { |
| 310 | + |
| 311 | + DBObject agg = Aggregation.newAggregation(// |
| 312 | + project("date") // |
| 313 | + .andExpression("tags-10")// |
| 314 | + , group("date")// |
| 315 | + .sum("tags_count").as("count")// |
| 316 | + ).toDbObject("foo", Aggregation.DEFAULT_CONTEXT); |
| 317 | + |
| 318 | + DBObject group = extractPipelineElement(agg, 1, "$group"); |
| 319 | + assertThat(getAsDBObject(group, "count"), is(new BasicDBObjectBuilder().add("$sum", "$tags_count").get())); |
| 320 | + } |
| 321 | + |
286 | 322 | private DBObject extractPipelineElement(DBObject agg, int index, String operation) {
|
287 | 323 |
|
288 | 324 | List<DBObject> pipeline = (List<DBObject>) agg.get("pipeline");
|
|
0 commit comments