Skip to content

DATAMONGO-1133 - Assert that Field aliasing is honored in Aggregation operations. #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1133-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private void cleanDb() {
mongoTemplate.dropCollection(Person.class);
mongoTemplate.dropCollection(Reservation.class);
mongoTemplate.dropCollection(Venue.class);
mongoTemplate.dropCollection(MeterData.class);
}

/**
Expand Down Expand Up @@ -1044,6 +1045,30 @@ public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() {
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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ public void rendersAggregationOptionsInTypedAggregationContextCorrectly() {
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 {

Expand Down