Browse Source

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.
pull/279/merge
Thomas Darimont 11 years ago committed by Oliver Gierke
parent
commit
60ca1b3509
  1. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java
  2. 38
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/MeterData.java
  3. 20
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

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

@ -125,6 +125,7 @@ public class AggregationTests { @@ -125,6 +125,7 @@ public class AggregationTests {
mongoTemplate.dropCollection(Person.class);
mongoTemplate.dropCollection(Reservation.class);
mongoTemplate.dropCollection(Venue.class);
mongoTemplate.dropCollection(MeterData.class);
}
/**
@ -1044,6 +1045,30 @@ public class AggregationTests { @@ -1044,6 +1045,30 @@ public class AggregationTests {
assertThat((Double) firstResult.get("distance"), closeTo(117.620092203928, 0.00001));
}
/**
* @see DATAMONGO-1133
*/
@Test
public void shouldHonorFieldAliasesForFieldReferences() {
mongoTemplate.insert(new MeterData("m1", "counter1", 42));
mongoTemplate.insert(new MeterData("m1", "counter1", 13));
mongoTemplate.insert(new MeterData("m1", "counter1", 45));
TypedAggregation<MeterData> agg = newAggregation(MeterData.class, //
match(where("resourceId").is("m1")), //
group("counterName").sum("counterVolume").as("totalValue") //
);
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, DBObject.class);
assertThat(results.getMappedResults(), hasSize(1));
DBObject result = results.getMappedResults().get(0);
assertThat(result.get("_id"), is(equalTo((Object) "counter1")));
assertThat(result.get("totalValue"), is(equalTo((Object) 42.0)));
}
private void assertLikeStats(LikeStats like, String id, long count) {
assertThat(like, is(notNullValue()));

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

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
/*
* Copyright 2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* @author Thomas Darimont
*/
public class MeterData {
@Id String resourceId;
@Field("counter_name") String counterName;
double counterVolume;
public MeterData() {}
public MeterData(String resourceId, String counterName, double counterVolume) {
this.resourceId = resourceId;
this.counterName = counterName;
this.counterVolume = counterVolume;
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 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.
@ -169,6 +169,24 @@ public class TypeBasedAggregationOperationContextUnitTests { @@ -169,6 +169,24 @@ public class TypeBasedAggregationOperationContextUnitTests {
assertThat(dbo.get("cursor"), is((Object) new BasicDBObject("foo", 1)));
}
/**
* @see DATAMONGO-1133
*/
@Test
public void shouldHonorAliasedFieldsInGroupExpressions() {
TypeBasedAggregationOperationContext context = getContext(MeterData.class);
TypedAggregation<MeterData> agg = newAggregation(MeterData.class,
group("counterName").sum("counterVolume").as("totalCounterVolume"));
DBObject dbo = agg.toDbObject("meterData", context);
DBObject group = getPipelineElementFromAggregationAt(dbo, 0);
DBObject definition = (DBObject) group.get("$group");
assertThat(definition.get("_id"), is(equalTo((Object) "$counter_name")));
}
@Document(collection = "person")
public static class FooPerson {

Loading…
Cancel
Save