Skip to content

Commit dc0a03e

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAMONGO-1133 - Assert that field aliasing is honored in aggregation operations.
Added some test to show that field aliases are honored during object rendering in aggregation operations. Original pull request: #279.
1 parent 504c44c commit dc0a03e

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private void cleanDb() {
124124
mongoTemplate.dropCollection(Person.class);
125125
mongoTemplate.dropCollection(Reservation.class);
126126
mongoTemplate.dropCollection(Venue.class);
127+
mongoTemplate.dropCollection(MeterData.class);
127128
}
128129

129130
/**
@@ -1046,6 +1047,30 @@ public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() {
10461047
assertThat((Double) firstResult.get("distance"), closeTo(117.620092203928, 0.00001));
10471048
}
10481049

1050+
/**
1051+
* @see DATAMONGO-1133
1052+
*/
1053+
@Test
1054+
public void shouldHonorFieldAliasesForFieldReferences() {
1055+
1056+
mongoTemplate.insert(new MeterData("m1", "counter1", 42));
1057+
mongoTemplate.insert(new MeterData("m1", "counter1", 13));
1058+
mongoTemplate.insert(new MeterData("m1", "counter1", 45));
1059+
1060+
TypedAggregation<MeterData> agg = newAggregation(MeterData.class, //
1061+
match(where("resourceId").is("m1")), //
1062+
group("counterName").sum("counterVolume").as("totalValue") //
1063+
);
1064+
1065+
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, DBObject.class);
1066+
1067+
assertThat(results.getMappedResults(), hasSize(1));
1068+
DBObject result = results.getMappedResults().get(0);
1069+
1070+
assertThat(result.get("_id"), is(equalTo((Object) "counter1")));
1071+
assertThat(result.get("totalValue"), is(equalTo((Object) 42.0)));
1072+
}
1073+
10491074
private void assertLikeStats(LikeStats like, String id, long count) {
10501075

10511076
assertThat(like, is(notNullValue()));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.mongodb.core.mapping.Field;
20+
21+
/**
22+
* @author Thomas Darimont
23+
*/
24+
public class MeterData {
25+
26+
@Id String resourceId;
27+
@Field("counter_name") String counterName;
28+
double counterVolume;
29+
30+
public MeterData() {}
31+
32+
public MeterData(String resourceId, String counterName, double counterVolume) {
33+
34+
this.resourceId = resourceId;
35+
this.counterName = counterName;
36+
this.counterVolume = counterVolume;
37+
}
38+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -169,6 +169,24 @@ public void rendersAggregationOptionsInTypedAggregationContextCorrectly() {
169169
assertThat(dbo.get("cursor"), is((Object) new BasicDBObject("foo", 1)));
170170
}
171171

172+
/**
173+
* @see DATAMONGO-1133
174+
*/
175+
@Test
176+
public void shouldHonorAliasedFieldsInGroupExpressions() {
177+
178+
TypeBasedAggregationOperationContext context = getContext(MeterData.class);
179+
TypedAggregation<MeterData> agg = newAggregation(MeterData.class,
180+
group("counterName").sum("counterVolume").as("totalCounterVolume"));
181+
182+
DBObject dbo = agg.toDbObject("meterData", context);
183+
DBObject group = getPipelineElementFromAggregationAt(dbo, 0);
184+
185+
DBObject definition = (DBObject) group.get("$group");
186+
187+
assertThat(definition.get("_id"), is(equalTo((Object) "$counter_name")));
188+
}
189+
172190
@Document(collection = "person")
173191
public static class FooPerson {
174192

0 commit comments

Comments
 (0)