DATAMONGO-2442 - Fix thenValueOf in $cond builder.

We now create a field reference when calling the builder instead of using the value as is.

Original pull request: #818.
This commit is contained in:
Christoph Strobl
2020-01-08 08:41:27 +01:00
committed by Mark Paluch
parent eb5a331822
commit c2ba361d24
3 changed files with 11 additions and 11 deletions

View File

@@ -207,7 +207,7 @@ public class ConditionalOperators {
public OtherwiseBuilder thenValueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return createThenBuilder().then(fieldReference);
return createThenBuilder().thenValueOf(fieldReference);
}
private ThenBuilder createThenBuilder() {

View File

@@ -443,14 +443,14 @@ public class AggregationUnitTests {
assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition);
}
@Test // DATAMONGO-861
@Test // DATAMONGO-861, DATAMONGO-2242
public void referencingProjectionAliasesShouldRenderProjectionConditionalWithFieldReferenceCorrectly() {
Document agg = Aggregation.newAggregation(//
project().and("color").as("chroma"), project().and("luminosity") //
.applyCondition(ConditionalOperators //
.when("chroma") //
.thenValueOf("bright") //
.then("bright") //
.otherwise("dark"))) //
.toDocument("foo", Aggregation.DEFAULT_CONTEXT);

View File

@@ -54,7 +54,7 @@ public class CondExpressionUnitTests {
newBuilder().when("isYellow").then(newBuilder().when("field").then("then-value")).otherwise("otherwise");
}
@Test // DATAMONGO-861, DATAMONGO-1542
@Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void simpleBuilderShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when("isYellow").thenValueOf("bright").otherwise("dark");
@@ -62,13 +62,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", "$isYellow") //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
@Test // DATAMONGO-861, DATAMONGO-1542
@Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void simpleCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf("bright")
@@ -77,13 +77,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", new Document("$gte", Arrays.<Object> asList("$luminosity", 100))) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
@Test // DATAMONGO-861
@Test // DATAMONGO-861, DATAMONGO-2242
public void andCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100) //
@@ -99,13 +99,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", Arrays.<Object> asList(luminosity, new Document("$and", Arrays.asList(hue, saturation)))) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "$dark-field");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
@Test // DATAMONGO-861, DATAMONGO-1542
@Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void twoArgsCriteriaShouldRenderCorrectly() {
Criteria criteria = Criteria.where("luminosity").gte(100) //
@@ -119,7 +119,7 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", Arrays.asList(gte, is)) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));