DATAMONGO-2047 - Polishing.

Retain previous options when calling withTimezone(…)/onNull…(…). Add tests. Javadoc.

Original pull request: #593.
This commit is contained in:
Mark Paluch
2018-08-13 11:59:46 +02:00
parent 488462d5b3
commit ac0aed8449
3 changed files with 52 additions and 3 deletions

View File

@@ -1401,7 +1401,7 @@ public class DateOperators {
public DateToString withTimezone(Timezone timezone) {
Assert.notNull(timezone, "Timezone must not be null.");
return new DateToString(argumentMap(get("date"), (String) get("format"), timezone));
return new DateToString(append("timezone", timezone));
}
/**
@@ -1461,6 +1461,25 @@ public class DateOperators {
return args;
}
protected java.util.Map<String, Object> append(String key, Object value) {
java.util.Map<String, Object> clone = new LinkedHashMap<String, Object>(argumentMap());
if (value instanceof Timezone) {
if (ObjectUtils.nullSafeEquals(value, Timezone.none())) {
clone.remove("timezone");
} else {
clone.put("timezone", ((Timezone) value).value);
}
} else {
clone.put(key, value);
}
return clone;
}
public interface FormatBuilder {
/**

View File

@@ -1213,7 +1213,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/**
* Generates a {@code $dateToString} expression that takes the date representation of the previously mentioned field
* using the server default format. <br />
* strong>NOTE:</strong> Requires MongoDB 4.0 or later.
* <strong>NOTE:</strong> Requires MongoDB 4.0 or later.
*
* @return
* @since 1.10.15

View File

@@ -1301,7 +1301,7 @@ public class ProjectionOperationUnitTests {
is(JSON.parse("{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\" } } } }")));
}
@Test // DATAMONGO-1834
@Test // DATAMONGO-1834, DATAMONGO-2047
public void shouldRenderDateToStringAggregationExpressionWithTimezone() {
DBObject agg = project()
@@ -1310,6 +1310,13 @@ public class ProjectionOperationUnitTests {
assertThat(agg, is(JSON.parse(
"{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\", \"timezone\" : \"America/Chicago\" } } } } } }")));
DBObject removedTimezone = project().and(DateOperators.dateOf("date")
.withTimezone(Timezone.valueOf("America/Chicago")).toString("%H:%M:%S:%L").withTimezone(Timezone.none()))
.as("time").toDBObject(Aggregation.DEFAULT_CONTEXT);
assertThat(removedTimezone,
is(JSON.parse("{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\" } } } } } }")));
}
@Test // DATAMONGO-2047
@@ -1323,6 +1330,29 @@ public class ProjectionOperationUnitTests {
.parse("{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : \"$fallback-field\" } } } }")));
}
@Test // DATAMONGO-2047
public void shouldRenderDateToStringWithOnNullExpression() {
DBObject agg = project()
.and(DateOperators.dateOf("date").toStringWithDefaultFormat()
.onNullReturnValueOf(LiteralOperators.valueOf("my-literal").asLiteral()))
.as("time").toDBObject(Aggregation.DEFAULT_CONTEXT);
assertThat(agg, is(JSON.parse(
"{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : { \"$literal\": \"my-literal\"} } } } }")));
}
@Test // DATAMONGO-2047
public void shouldRenderDateToStringWithOnNullAndTimezone() {
DBObject agg = project().and(DateOperators.dateOf("date").toStringWithDefaultFormat()
.onNullReturnValueOf("fallback-field").withTimezone(Timezone.ofField("foo"))).as("time")
.toDBObject(Aggregation.DEFAULT_CONTEXT);
assertThat(agg, is(JSON.parse(
"{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : \"$fallback-field\", \"timezone\": \"$foo\" } } } }")));
}
@Test // DATAMONGO-1536
public void shouldRenderSumAggregationExpression() {