Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 149dca6

Browse files
authored
PHPLIB-1350 Add tests on Date Expression Operators and add Yaml tags for BSON types (#34)
* Remove unused code and unescape backslashes in JSON * Switched to Yaml tags to represent specific BSON objects
1 parent 8b445be commit 149dca6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2786
-51
lines changed

generator/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,18 @@ To run the generator, you need to have PHP 8.1+ installed and Composer.
1717

1818
The `generator/config/*.yaml` files contains the list of operators and stages that are supported by the library.
1919

20+
### Test pipelines
21+
22+
Each operator can contain a `tests` section with a list if pipelines. To represent specific BSON objects,
23+
it is necessary to use Yaml tags:
24+
25+
| BSON Type | Example |
26+
|-------------|----------------------------------------------|
27+
| Regex | `!regex '^abc'` <br/> `!regex ['^abc', 'i']` |
28+
| Int64 | `!long '123456789'` |
29+
| Decimal128 | `!double '0.9'` |
30+
| UTCDateTime | `!date 0` |
31+
| Binary | `!binary 'IA=='` |
32+
2033
To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use
2134
the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.

generator/config/expression/dateAdd.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,100 @@ arguments:
3333
optional: true
3434
description: |
3535
The timezone to carry out the operation. $timezone must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC.
36+
tests:
37+
-
38+
name: 'Add a Future Date'
39+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#add-a-future-date'
40+
pipeline:
41+
-
42+
$project:
43+
expectedDeliveryDate:
44+
$dateAdd:
45+
startDate: '$purchaseDate'
46+
unit: 'day'
47+
amount: 3
48+
-
49+
# $merge: 'shipping'
50+
$merge:
51+
into: 'shipping'
52+
-
53+
name: 'Filter on a Date Range'
54+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#filter-on-a-date-range'
55+
pipeline:
56+
-
57+
$match:
58+
$expr:
59+
$gt:
60+
- '$deliveryDate'
61+
-
62+
$dateAdd:
63+
startDate: '$purchaseDate'
64+
unit: 'day'
65+
amount: 5
66+
-
67+
$project:
68+
_id: 0
69+
custId: 1
70+
purchased:
71+
$dateToString:
72+
format: '%Y-%m-%d'
73+
date: '$purchaseDate'
74+
delivery:
75+
$dateToString:
76+
format: '%Y-%m-%d'
77+
date: '$deliveryDate'
78+
-
79+
name: 'Adjust for Daylight Savings Time'
80+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#adjust-for-daylight-savings-time'
81+
pipeline:
82+
-
83+
$project:
84+
_id: 0
85+
location: 1
86+
start:
87+
$dateToString:
88+
format: '%Y-%m-%d %H:%M'
89+
date: '$login'
90+
days:
91+
$dateToString:
92+
format: '%Y-%m-%d %H:%M'
93+
date:
94+
$dateAdd:
95+
startDate: '$login'
96+
unit: 'day'
97+
amount: 1
98+
timezone: '$location'
99+
hours:
100+
$dateToString:
101+
format: '%Y-%m-%d %H:%M'
102+
date:
103+
$dateAdd:
104+
startDate: '$login'
105+
unit: 'hour'
106+
amount: 24
107+
timezone: '$location'
108+
startTZInfo:
109+
$dateToString:
110+
format: '%Y-%m-%d %H:%M'
111+
date: '$login'
112+
timezone: '$location'
113+
daysTZInfo:
114+
$dateToString:
115+
format: '%Y-%m-%d %H:%M'
116+
date:
117+
$dateAdd:
118+
startDate: '$login'
119+
unit: 'day'
120+
amount: 1
121+
timezone: '$location'
122+
timezone: '$location'
123+
hoursTZInfo:
124+
$dateToString:
125+
format: '%Y-%m-%d %H:%M'
126+
date:
127+
$dateAdd:
128+
startDate: '$login'
129+
unit: 'hour'
130+
amount: 24
131+
timezone: '$location'
132+
timezone: '$location'

generator/config/expression/dateDiff.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,72 @@ arguments:
4343
optional: true
4444
description: |
4545
Used when the unit is equal to week. Defaults to Sunday. The startOfWeek parameter is an expression that resolves to a case insensitive string
46+
tests:
47+
-
48+
name: 'Elapsed Time'
49+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#elapsed-time'
50+
pipeline:
51+
-
52+
$group:
53+
_id: ~
54+
averageTime:
55+
$avg:
56+
$dateDiff:
57+
startDate: '$purchased'
58+
endDate: '$delivered'
59+
unit: 'day'
60+
-
61+
$project:
62+
_id: 0
63+
numDays:
64+
$trunc:
65+
- '$averageTime'
66+
- 1
67+
-
68+
name: 'Result Precision'
69+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#result-precision'
70+
pipeline:
71+
-
72+
$project:
73+
Start: '$start'
74+
End: '$end'
75+
years:
76+
$dateDiff:
77+
startDate: '$start'
78+
endDate: '$end'
79+
unit: 'year'
80+
months:
81+
$dateDiff:
82+
startDate: '$start'
83+
endDate: '$end'
84+
unit: 'month'
85+
days:
86+
$dateDiff:
87+
startDate: '$start'
88+
endDate: '$end'
89+
unit: 'day'
90+
_id: 0
91+
-
92+
name: 'Weeks Per Month'
93+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#weeks-per-month'
94+
pipeline:
95+
-
96+
$project:
97+
wks_default:
98+
$dateDiff:
99+
startDate: '$start'
100+
endDate: '$end'
101+
unit: 'week'
102+
wks_monday:
103+
$dateDiff:
104+
startDate: '$start'
105+
endDate: '$end'
106+
unit: 'week'
107+
startOfWeek: 'Monday'
108+
wks_friday:
109+
$dateDiff:
110+
startDate: '$start'
111+
endDate: '$end'
112+
unit: 'week'
113+
startOfWeek: 'fri'
114+
_id: 0

generator/config/expression/dateFromParts.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ arguments:
1111
name: year
1212
type:
1313
- resolvesToNumber
14+
optional: true
1415
description: |
1516
Calendar year. Can be any expression that evaluates to a number.
1617
-
1718
name: isoWeekYear
1819
type:
1920
- resolvesToNumber
21+
optional: true
2022
description: |
2123
ISO Week Date Year. Can be any expression that evaluates to a number.
2224
-
@@ -82,3 +84,31 @@ arguments:
8284
optional: true
8385
description: |
8486
The timezone to carry out the operation. $timezone must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC.
87+
tests:
88+
-
89+
name: 'Example'
90+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromParts/#example'
91+
pipeline:
92+
-
93+
$project:
94+
date:
95+
$dateFromParts:
96+
year: 2017
97+
month: 2
98+
day: 8
99+
hour: 12
100+
date_iso:
101+
$dateFromParts:
102+
isoWeekYear: 2017
103+
isoWeek: 6
104+
isoDayOfWeek: 3
105+
hour: 12
106+
date_timezone:
107+
$dateFromParts:
108+
year: 2016
109+
month: 12
110+
day: 31
111+
hour: 23
112+
minute: 46
113+
second: 12
114+
timezone: 'America/New_York'

generator/config/expression/dateFromString.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,38 @@ arguments:
4444
description: |
4545
If the dateString provided to $dateFromString is null or missing, it outputs the result value of the provided onNull expression. This result value can be of any type.
4646
If you do not specify onNull and dateString is null or missing, then $dateFromString outputs null.
47+
tests:
48+
-
49+
name: 'Converting Dates'
50+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#converting-dates'
51+
pipeline:
52+
-
53+
$project:
54+
date:
55+
$dateFromString:
56+
dateString: '$date'
57+
timezone: 'America/New_York'
58+
-
59+
name: 'onError'
60+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#onerror'
61+
pipeline:
62+
-
63+
$project:
64+
date:
65+
$dateFromString:
66+
dateString: '$date'
67+
timezone: '$timezone'
68+
onError: '$date'
69+
-
70+
name: 'onNull'
71+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#onnull'
72+
pipeline:
73+
-
74+
$project:
75+
date:
76+
$dateFromString:
77+
dateString: '$date'
78+
timezone: '$timezone'
79+
# onNull: new Date(0)
80+
onNull: !date 0
81+

generator/config/expression/dateSubtract.yaml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,105 @@ arguments:
3333
optional: true
3434
description: |
3535
The timezone to carry out the operation. $timezone must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is displayed in UTC.
36+
tests:
37+
-
38+
name: 'Subtract A Fixed Amount'
39+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#subtract-a-fixed-amount'
40+
pipeline:
41+
-
42+
$match:
43+
$expr:
44+
$eq:
45+
-
46+
# $month: '$logout'
47+
$month:
48+
date: '$logout'
49+
- 1
50+
-
51+
$project:
52+
logoutTime:
53+
$dateSubtract:
54+
startDate: '$logout'
55+
unit: 'hour'
56+
amount: 3
57+
-
58+
# $merge: 'connectionTime'
59+
$merge:
60+
into: 'connectionTime'
61+
-
62+
name: 'Filter by Relative Dates'
63+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#filter-by-relative-dates'
64+
pipeline:
65+
-
66+
$match:
67+
$expr:
68+
$gt:
69+
- '$logoutTime'
70+
-
71+
$dateSubtract:
72+
startDate: '$$NOW'
73+
unit: 'week'
74+
amount: 1
75+
-
76+
$project:
77+
_id: 0
78+
custId: 1
79+
loggedOut:
80+
$dateToString:
81+
format: '%Y-%m-%d'
82+
date: '$logoutTime'
83+
-
84+
name: 'Adjust for Daylight Savings Time'
85+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#adjust-for-daylight-savings-time'
86+
pipeline:
87+
-
88+
$project:
89+
_id: 0
90+
location: 1
91+
start:
92+
$dateToString:
93+
format: '%Y-%m-%d %H:%M'
94+
date: '$login'
95+
days:
96+
$dateToString:
97+
format: '%Y-%m-%d %H:%M'
98+
date:
99+
$dateSubtract:
100+
startDate: '$login'
101+
unit: 'day'
102+
amount: 1
103+
timezone: '$location'
104+
hours:
105+
$dateToString:
106+
format: '%Y-%m-%d %H:%M'
107+
date:
108+
$dateSubtract:
109+
startDate: '$login'
110+
unit: 'hour'
111+
amount: 24
112+
timezone: '$location'
113+
startTZInfo:
114+
$dateToString:
115+
format: '%Y-%m-%d %H:%M'
116+
date: '$login'
117+
timezone: '$location'
118+
daysTZInfo:
119+
$dateToString:
120+
format: '%Y-%m-%d %H:%M'
121+
date:
122+
$dateSubtract:
123+
startDate: '$login'
124+
unit: 'day'
125+
amount: 1
126+
timezone: '$location'
127+
timezone: '$location'
128+
hoursTZInfo:
129+
$dateToString:
130+
format: '%Y-%m-%d %H:%M'
131+
date:
132+
$dateSubtract:
133+
startDate: '$login'
134+
unit: 'hour'
135+
amount: 24
136+
timezone: '$location'
137+
timezone: '$location'

0 commit comments

Comments
 (0)