summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorMindaugas Malinauskas <mindaugas.malinauskas@mongodb.com>2021-01-21 17:24:14 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-02-11 13:24:45 +0000
commite9b02873749f2331f1853d00e13c8a67b39bf53a (patch)
treef643cb0b752fa091a7f9853ec71a5dba9cfe5d7c /jstests
parent6d419110f2dbd9b07ee96a601356ddbe99628dc5 (diff)
downloadmongo-e9b02873749f2331f1853d00e13c8a67b39bf53a.tar.gz
SERVER-53388 Week start parameter for $dateDiff aggregation expression
Diffstat (limited to 'jstests')
-rw-r--r--jstests/aggregation/expressions/date_diff.js105
-rw-r--r--jstests/libs/sbe_assert_error_override.js3
2 files changed, 101 insertions, 7 deletions
diff --git a/jstests/aggregation/expressions/date_diff.js b/jstests/aggregation/expressions/date_diff.js
index 0d643c5caa2..bf4fc3554b3 100644
--- a/jstests/aggregation/expressions/date_diff.js
+++ b/jstests/aggregation/expressions/date_diff.js
@@ -46,6 +46,20 @@ const aggregationPipelineWithDateDiff = [{
}
}
}];
+const aggregationPipelineWithDateDiffAndStartOfWeek = [{
+ $project: {
+ _id: false,
+ date_diff: {
+ $dateDiff: {
+ startDate: "$startDate",
+ endDate: "$endDate",
+ unit: "$unit",
+ timezone: "$timeZone",
+ startOfWeek: "$startOfWeek"
+ }
+ }
+ }
+}];
const testCases = [
{
// Parameters are constants, timezone is not specified.
@@ -66,12 +80,13 @@ const testCases = [
},
{
// Parameters are field paths.
- pipeline: aggregationPipelineWithDateDiff,
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
inputDocuments: [{
startDate: new Date("2020-11-01T18:23:36Z"),
endDate: new Date("2020-11-02T00:00:00Z"),
unit: "hour",
- timeZone: "America/New_York"
+ timeZone: "America/New_York",
+ startOfWeek: "IGNORED" // Ignored when unit is not week.
}],
expectedResults: [{date_diff: NumberLong("6")}]
},
@@ -163,13 +178,13 @@ const testCases = [
expectedResults: [{date_diff: null}],
},
{
- // Wrong 'unit' type.
+ // Invalid 'unit' type.
pipeline: aggregationPipelineWithDateDiff,
inputDocuments: [{startDate: someDate, endDate: someDate, unit: 5, timeZone: "UTC"}],
expectedErrorCode: 5166306,
},
{
- // Wrong 'unit' value.
+ // Invalid 'unit' value.
pipeline: aggregationPipelineWithDateDiff,
inputDocuments: [{startDate: someDate, endDate: someDate, unit: "decade", timeZone: "UTC"}],
expectedErrorCode: 9,
@@ -187,17 +202,95 @@ const testCases = [
expectedResults: [{date_diff: null}],
},
{
- // Wrong 'timezone' type.
+ // Invalid 'timezone' type.
pipeline: aggregationPipelineWithDateDiff,
inputDocuments: [{startDate: someDate, endDate: someDate, unit: "hour", timeZone: 1}],
expectedErrorCode: 40517,
},
{
- // Wrong 'timezone' value.
+ // Invalid 'timezone' value.
pipeline: aggregationPipelineWithDateDiff,
inputDocuments:
[{startDate: someDate, endDate: someDate, unit: "hour", timeZone: "America/Invalid"}],
expectedErrorCode: 40485,
+ },
+ {
+ // Specified 'startOfWeek'.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [{
+ startDate: new Date("2021-01-24T18:23:36Z"), // Sunday.
+ endDate: new Date("2021-01-25T02:23:36Z"), // Monday.
+ unit: "week",
+ timeZone: "GMT",
+ startOfWeek: "MONDAY"
+ }],
+ expectedResults: [{date_diff: NumberLong("1")}],
+ },
+ {
+ // Specified 'startOfWeek' and timezone.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [{
+ startDate: new Date("2021-01-17T05:00:00Z"), // Sunday in New York.
+ endDate: new Date("2021-01-17T04:59:00Z"), // Saturday in New York.
+ unit: "week",
+ timeZone: "America/New_York",
+ startOfWeek: "sunday"
+ }],
+ expectedResults: [{date_diff: NumberLong("-1")}],
+ },
+ {
+ // Unspecified 'startOfWeek' - defaults to Sunday.
+ pipeline: [{
+ $project: {
+ _id: false,
+ date_diff: {$dateDiff: {startDate: "$startDate", endDate: "$endDate", unit: "week"}}
+ }
+ }],
+ inputDocuments: [{
+ startDate: new Date("2021-01-24T18:23:36Z"), // Sunday.
+ endDate: new Date("2021-01-25T02:23:36Z"), // Monday.
+ }],
+ expectedResults: [{date_diff: NumberLong("0")}],
+ },
+ {
+ // Null 'startOfWeek'.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [{startDate: someDate, endDate: someDate, unit: "week", startOfWeek: null}],
+ expectedResults: [{date_diff: null}],
+ },
+ {
+ // Missing 'startOfWeek' value, invalid other fields.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [{startDate: 1, endDate: 2, unit: "week", timeZone: 1}],
+ expectedResults: [{date_diff: null}],
+ },
+ {
+ // Invalid 'startOfWeek' type.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [
+ {startDate: someDate, endDate: someDate, unit: "week", timeZone: "GMT", startOfWeek: 1}
+ ],
+ expectedErrorCode: 5338800,
+ },
+ {
+ // Invalid 'startOfWeek' type, unit is not the week.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [
+ {startDate: someDate, endDate: someDate, unit: "hour", timeZone: "GMT", startOfWeek: 1}
+ ],
+ expectedResults: [{date_diff: NumberLong("0")}],
+ },
+ {
+ // Invalid 'startOfWeek' value.
+ pipeline: aggregationPipelineWithDateDiffAndStartOfWeek,
+ inputDocuments: [{
+ startDate: someDate,
+ endDate: someDate,
+ unit: "week",
+ timeZone: "GMT",
+ startOfWeek: "FRIDIE"
+ }],
+ expectedErrorCode: 9,
}
];
testCases.forEach(executeTestCase);
diff --git a/jstests/libs/sbe_assert_error_override.js b/jstests/libs/sbe_assert_error_override.js
index 256af6b042e..8dde30b3a00 100644
--- a/jstests/libs/sbe_assert_error_override.js
+++ b/jstests/libs/sbe_assert_error_override.js
@@ -21,7 +21,7 @@
// Below is the list of known equivalent error code groups. As new groups of equivalent error codes
// are discovered, they should be added to this list.
const equivalentErrorCodesList = [
- [9, 5166503, 5166605],
+ [9, 5166503, 5166605, 5338802],
[28651, 5073201],
[16006, 4997703, 4998202],
[28689, 5126701],
@@ -81,6 +81,7 @@ const equivalentErrorCodesList = [
[51111, 5073402],
[51151, 5126606],
[51156, 5073403],
+ [5338800, 5338801],
];
// This map is generated based on the contents of 'equivalentErrorCodesList'. This map should _not_