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

PHPLIB-1350 PHPLIB-1350 Add tests on Date Expression Operators and add Yaml tags for BSON types #34

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ To run the generator, you need to have PHP 8.1+ installed and Composer.

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

### Test pipelines

Each operator can contain a `tests` section with a list if pipelines. To represent specific BSON objects,
it is necessary to use Yaml tags:

| BSON Type | Example |
|-------------|----------------------------------------------|
| Regex | `!regex '^abc'` <br/> `!regex ['^abc', 'i']` |
| Int64 | `!long '123456789'` |
| Decimal128 | `!double '0.9'` |
| UTCDateTime | `!date 0` |
| Binary | `!binary 'IA=='` |

To add new test cases to operators, you can get inspiration from the official MongoDB documentation and use
the `generator/js2yaml.html` web page to manually convert a pipeline array from JS to Yaml.
97 changes: 97 additions & 0 deletions generator/config/expression/dateAdd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,100 @@ arguments:
optional: true
description: |
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.
tests:
-
name: 'Add a Future Date'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#add-a-future-date'
pipeline:
-
$project:
expectedDeliveryDate:
$dateAdd:
startDate: '$purchaseDate'
unit: 'day'
amount: 3
-
# $merge: 'shipping'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted you left a comment denoting the original since our $merge operator always uses the into argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this in few other place. So that if we find there is a bug, it's easier to see the original example.

$merge:
into: 'shipping'
-
name: 'Filter on a Date Range'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#filter-on-a-date-range'
pipeline:
-
$match:
$expr:
$gt:
- '$deliveryDate'
-
$dateAdd:
startDate: '$purchaseDate'
unit: 'day'
amount: 5
-
$project:
_id: 0
custId: 1
purchased:
$dateToString:
format: '%Y-%m-%d'
date: '$purchaseDate'
delivery:
$dateToString:
format: '%Y-%m-%d'
date: '$deliveryDate'
-
name: 'Adjust for Daylight Savings Time'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateAdd/#adjust-for-daylight-savings-time'
pipeline:
-
$project:
_id: 0
location: 1
start:
$dateToString:
format: '%Y-%m-%d %H:%M'
date: '$login'
days:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateAdd:
startDate: '$login'
unit: 'day'
amount: 1
timezone: '$location'
hours:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateAdd:
startDate: '$login'
unit: 'hour'
amount: 24
timezone: '$location'
startTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date: '$login'
timezone: '$location'
daysTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateAdd:
startDate: '$login'
unit: 'day'
amount: 1
timezone: '$location'
timezone: '$location'
hoursTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateAdd:
startDate: '$login'
unit: 'hour'
amount: 24
timezone: '$location'
timezone: '$location'
69 changes: 69 additions & 0 deletions generator/config/expression/dateDiff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,72 @@ arguments:
optional: true
description: |
Used when the unit is equal to week. Defaults to Sunday. The startOfWeek parameter is an expression that resolves to a case insensitive string
tests:
-
name: 'Elapsed Time'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#elapsed-time'
pipeline:
-
$group:
_id: ~
averageTime:
$avg:
$dateDiff:
startDate: '$purchased'
endDate: '$delivered'
unit: 'day'
-
$project:
_id: 0
numDays:
$trunc:
- '$averageTime'
- 1
-
name: 'Result Precision'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#result-precision'
pipeline:
-
$project:
Start: '$start'
End: '$end'
years:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'year'
months:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'month'
days:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'day'
_id: 0
-
name: 'Weeks Per Month'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateDiff/#weeks-per-month'
pipeline:
-
$project:
wks_default:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'week'
wks_monday:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'week'
startOfWeek: 'Monday'
wks_friday:
$dateDiff:
startDate: '$start'
endDate: '$end'
unit: 'week'
startOfWeek: 'fri'
_id: 0
30 changes: 30 additions & 0 deletions generator/config/expression/dateFromParts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ arguments:
name: year
type:
- resolvesToNumber
optional: true
description: |
Calendar year. Can be any expression that evaluates to a number.
-
name: isoWeekYear
type:
- resolvesToNumber
optional: true
description: |
ISO Week Date Year. Can be any expression that evaluates to a number.
-
Expand Down Expand Up @@ -82,3 +84,31 @@ arguments:
optional: true
description: |
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.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromParts/#example'
pipeline:
-
$project:
date:
$dateFromParts:
year: 2017
month: 2
day: 8
hour: 12
date_iso:
$dateFromParts:
isoWeekYear: 2017
isoWeek: 6
isoDayOfWeek: 3
hour: 12
date_timezone:
$dateFromParts:
year: 2016
month: 12
day: 31
hour: 23
minute: 46
second: 12
timezone: 'America/New_York'
35 changes: 35 additions & 0 deletions generator/config/expression/dateFromString.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,38 @@ arguments:
description: |
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.
If you do not specify onNull and dateString is null or missing, then $dateFromString outputs null.
tests:
-
name: 'Converting Dates'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#converting-dates'
pipeline:
-
$project:
date:
$dateFromString:
dateString: '$date'
timezone: 'America/New_York'
-
name: 'onError'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#onerror'
pipeline:
-
$project:
date:
$dateFromString:
dateString: '$date'
timezone: '$timezone'
onError: '$date'
-
name: 'onNull'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateFromString/#onnull'
pipeline:
-
$project:
date:
$dateFromString:
dateString: '$date'
timezone: '$timezone'
# onNull: new Date(0)
onNull: !date 0

102 changes: 102 additions & 0 deletions generator/config/expression/dateSubtract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,105 @@ arguments:
optional: true
description: |
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.
tests:
-
name: 'Subtract A Fixed Amount'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#subtract-a-fixed-amount'
pipeline:
-
$match:
$expr:
$eq:
-
# $month: '$logout'
$month:
date: '$logout'
- 1
-
$project:
logoutTime:
$dateSubtract:
startDate: '$logout'
unit: 'hour'
amount: 3
-
# $merge: 'connectionTime'
$merge:
into: 'connectionTime'
-
name: 'Filter by Relative Dates'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#filter-by-relative-dates'
pipeline:
-
$match:
$expr:
$gt:
- '$logoutTime'
-
$dateSubtract:
startDate: '$$NOW'
unit: 'week'
amount: 1
-
$project:
_id: 0
custId: 1
loggedOut:
$dateToString:
format: '%Y-%m-%d'
date: '$logoutTime'
-
name: 'Adjust for Daylight Savings Time'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/#adjust-for-daylight-savings-time'
pipeline:
-
$project:
_id: 0
location: 1
start:
$dateToString:
format: '%Y-%m-%d %H:%M'
date: '$login'
days:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateSubtract:
startDate: '$login'
unit: 'day'
amount: 1
timezone: '$location'
hours:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateSubtract:
startDate: '$login'
unit: 'hour'
amount: 24
timezone: '$location'
startTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date: '$login'
timezone: '$location'
daysTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateSubtract:
startDate: '$login'
unit: 'day'
amount: 1
timezone: '$location'
timezone: '$location'
hoursTZInfo:
$dateToString:
format: '%Y-%m-%d %H:%M'
date:
$dateSubtract:
startDate: '$login'
unit: 'hour'
amount: 24
timezone: '$location'
timezone: '$location'
Loading