summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/date_from_string_on_null.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/expressions/date_from_string_on_null.js')
-rw-r--r--jstests/aggregation/expressions/date_from_string_on_null.js100
1 files changed, 49 insertions, 51 deletions
diff --git a/jstests/aggregation/expressions/date_from_string_on_null.js b/jstests/aggregation/expressions/date_from_string_on_null.js
index 12b7d673984..caf7cf1216d 100644
--- a/jstests/aggregation/expressions/date_from_string_on_null.js
+++ b/jstests/aggregation/expressions/date_from_string_on_null.js
@@ -2,67 +2,65 @@
* Tests for the $dateFromString expression with the optional 'onNull' parameter.
*/
(function() {
- "use strict";
+"use strict";
- const onNullValue = ISODate("2017-07-04T11:56:02Z");
- const coll = db.date_from_string_on_null;
- coll.drop();
+const onNullValue = ISODate("2017-07-04T11:56:02Z");
+const coll = db.date_from_string_on_null;
+coll.drop();
- assert.writeOK(coll.insert({_id: 0}));
+assert.writeOK(coll.insert({_id: 0}));
- // Test that the 'onNull' value is returned when the 'dateString' is nullish.
- for (let inputDate of[null, undefined, "$missing"]) {
- assert.eq(
- [{_id: 0, date: onNullValue}],
- coll.aggregate({
- $project:
- {date: {$dateFromString: {dateString: inputDate, onNull: onNullValue}}}
- })
- .toArray());
- }
+// Test that the 'onNull' value is returned when the 'dateString' is nullish.
+for (let inputDate of [null, undefined, "$missing"]) {
+ assert.eq(
+ [{_id: 0, date: onNullValue}],
+ coll.aggregate(
+ {$project: {date: {$dateFromString: {dateString: inputDate, onNull: onNullValue}}}})
+ .toArray());
+}
- // Test that null is returned when the 'timezone' or 'format' is nullish, regardless of the
- // 'onNull' value.
- for (let nullishValue of[null, undefined, "$missing"]) {
- assert.eq([{_id: 0, date: null}],
- coll.aggregate({
- $project: {
- date: {
- $dateFromString: {
- dateString: "2018-02-06T11:56:02Z",
- format: nullishValue,
- onNull: onNullValue
- }
+// Test that null is returned when the 'timezone' or 'format' is nullish, regardless of the
+// 'onNull' value.
+for (let nullishValue of [null, undefined, "$missing"]) {
+ assert.eq([{_id: 0, date: null}],
+ coll.aggregate({
+ $project: {
+ date: {
+ $dateFromString: {
+ dateString: "2018-02-06T11:56:02Z",
+ format: nullishValue,
+ onNull: onNullValue
}
}
- })
- .toArray());
- assert.eq([{_id: 0, date: null}],
- coll.aggregate({
- $project: {
- date: {
- $dateFromString: {
- dateString: "2018-02-06T11:56:02Z",
- timezone: nullishValue,
- onNull: onNullValue
- }
+ }
+ })
+ .toArray());
+ assert.eq([{_id: 0, date: null}],
+ coll.aggregate({
+ $project: {
+ date: {
+ $dateFromString: {
+ dateString: "2018-02-06T11:56:02Z",
+ timezone: nullishValue,
+ onNull: onNullValue
}
}
- })
- .toArray());
- }
+ }
+ })
+ .toArray());
+}
- // Test that 'onNull' can be any type, not just an ISODate.
- for (let onNull of[{}, 5, "Not a date", null, undefined]) {
- assert.eq(
- [{_id: 0, date: onNull}],
- coll.aggregate(
- {$project: {date: {$dateFromString: {dateString: "$missing", onNull: onNull}}}})
- .toArray());
- }
+// Test that 'onNull' can be any type, not just an ISODate.
+for (let onNull of [{}, 5, "Not a date", null, undefined]) {
assert.eq(
- [{_id: 0}],
+ [{_id: 0, date: onNull}],
coll.aggregate(
- {$project: {date: {$dateFromString: {dateString: "$missing", onNull: "$missing"}}}})
+ {$project: {date: {$dateFromString: {dateString: "$missing", onNull: onNull}}}})
.toArray());
+}
+assert.eq(
+ [{_id: 0}],
+ coll.aggregate(
+ {$project: {date: {$dateFromString: {dateString: "$missing", onNull: "$missing"}}}})
+ .toArray());
})();