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

PHPLIB-1343 Add tests on Array Expression Operators #22

Merged
merged 2 commits into from
Jan 15, 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
16 changes: 16 additions & 0 deletions generator/config/expression/arrayElemAt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ arguments:
name: idx
type:
- resolvesToInt
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/#example'
pipeline:
-
$project:
name: 1
first:
$arrayElemAt:
- '$favorites'
- 0
last:
$arrayElemAt:
- '$favorites'
- -1
37 changes: 37 additions & 0 deletions generator/config/expression/arrayToObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,40 @@ arguments:
name: array
type:
- resolvesToArray
tests:
-
name: '$arrayToObject Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayToObject/#-arraytoobject--example'
pipeline:
-
$project:
item: 1
dimensions:
# The example renders a single value, but the builder generates an array for consistency
# $arrayToObject: '$dimensions'
$arrayToObject:
- '$dimensions'
-
name: '$objectToArray and $arrayToObject Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayToObject/#-objecttoarray----arraytoobject-example'
pipeline:
-
$addFields:
instock:
$objectToArray: '$instock'
-
$addFields:
instock:
$concatArrays:
- '$instock'
-
-
k: 'total'
v:
$sum:
- '$instock.v'
-
$addFields:
instock:
$arrayToObject:
- '$instock'
11 changes: 11 additions & 0 deletions generator/config/expression/concatArrays.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ arguments:
type:
- resolvesToArray
variadic: array
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatArrays/#example'
pipeline:
-
$project:
items:
$concatArrays:
- '$instock'
- '$ordered'
60 changes: 60 additions & 0 deletions generator/config/expression/filter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,63 @@ arguments:
description: |
A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array.
If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. If the limit is null, $filter returns all matching array elements.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#examples'
pipeline:
-
$project:
items:
$filter:
input: '$items'
as: 'item'
cond:
$gte:
- '$$item.price'
- 100
-
name: 'Using the limit field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#using-the-limit-field'
pipeline:
-
$project:
items:
$filter:
input: '$items'
cond:
$gte:
- '$$item.price'
- 100
as: 'item'
limit: 1
-
name: 'limit as a Numeric Expression'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#limit-as-a-numeric-expression'
pipeline:
-
$project:
items:
$filter:
input: '$items'
cond:
$lte:
- '$$item.price'
- 150
as: 'item'
limit: 2
-
name: 'limit Greater than Possible Matches'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#limit-greater-than-possible-matches'
pipeline:
-
$project:
items:
$filter:
input: '$items'
cond:
$gte:
- '$$item.price'
- 100
as: 'item'
limit: 5
12 changes: 12 additions & 0 deletions generator/config/expression/in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ arguments:
- resolvesToArray
description: |
Any valid expression that resolves to an array.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/in/#example'
pipeline:
-
$project:
store location: '$location'
has bananas:
$in:
- 'bananas'
- '$in_stock'
11 changes: 11 additions & 0 deletions generator/config/expression/indexOfArray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ arguments:
description: |
An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfArray uses the <end> value as the <start> index value instead of the <end> value.
If unspecified, the ending index position for the search is the end of the string.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfArray/#example'
pipeline:
-
$project:
index:
$indexOfArray:
- '$items'
- 2
25 changes: 24 additions & 1 deletion generator/config/expression/isArray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,27 @@ arguments:
name: expression
type:
- expression
variadic: array
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isArray/#example'
pipeline:
-
$project:
items:
$cond:
if:
$and:
# The example in the docs uses the short syntax for $isArray,
# but the aggregation builder always uses the more verbose syntax.
-
$isArray:
- '$instock'
-
$isArray:
- '$ordered'
then:
$concatArrays:
- '$instock'
- '$ordered'
else: 'One or more fields is not an array.'
48 changes: 48 additions & 0 deletions generator/config/expression/map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,51 @@ arguments:
- expression
description: |
An expression that is applied to each element of the input array. The expression references each element individually with the variable name specified in as.
tests:
-
name: 'Add to Each Element of an Array'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#add-to-each-element-of-an-array'
pipeline:
-
$project:
adjustedGrades:
$map:
input: '$quizzes'
as: 'grade'
in:
$add:
- '$$grade'
- 2
-
name: 'Truncate Each Array Element'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#truncate-each-array-element'
pipeline:
-
$project:
city: '$city'
integerValues:
$map:
input: '$distances'
as: 'decimalValue'
in:
# The example renders a single value, but the builder generates an array for consistency
# $trunc: '$$decimalValue'
$trunc:
- '$$decimalValue'
-
name: 'Convert Celsius Temperatures to Fahrenheit'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#convert-celsius-temperatures-to-fahrenheit'
pipeline:
-
$addFields:
tempsF:
$map:
input: '$tempsC'
as: 'tempInCelsius'
in:
$add:
-
$multiply:
- '$$tempInCelsius'
- 1.8
- 32
11 changes: 11 additions & 0 deletions generator/config/expression/maxN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ arguments:
- resolvesToInt
description: |
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN-array-element/#example'
pipeline:
-
$addFields:
maxScores:
$maxN:
n: 2
input: '$score'
11 changes: 11 additions & 0 deletions generator/config/expression/minN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ arguments:
- resolvesToInt
description: |
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN-array-element/#example'
pipeline:
-
$addFields:
minScores:
$minN:
n: 2
input: '$score'
30 changes: 30 additions & 0 deletions generator/config/expression/objectToArray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,33 @@ arguments:
- resolvesToObject
description: |
Any valid expression as long as it resolves to a document object. $objectToArray applies to the top-level fields of its argument. If the argument is a document that itself contains embedded document fields, the $objectToArray does not recursively apply to the embedded document fields.
tests:
# "$objectToArray and $arrayToObject Example" omitted as it's already in arrayToObject.yaml
-
name: '$objectToArray Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/#-objecttoarray-example'
pipeline:
-
$project:
item: 1
dimensions:
$objectToArray: '$dimensions'
-
name: '$objectToArray to Sum Nested Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/#-objecttoarray-to-sum-nested-fields'
pipeline:
-
$project:
warehouses:
$objectToArray: '$instock'
-
# The example in the docs uses the short syntax for $unwind,
# but the aggregation builder always uses the more verbose syntax.
# $unwind: '$warehouses'
$unwind:
path: '$warehouses'
-
$group:
_id: '$warehouses.k'
total:
$sum: '$warehouses.v'
14 changes: 14 additions & 0 deletions generator/config/expression/range.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ arguments:
optional: true
description: |
An integer that specifies the increment value. Can be any valid expression that resolves to a non-zero integer. Defaults to 1.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/range/#example'
pipeline:
-
$project:
_id: 0
city: 1
Rest stops:
$range:
- 0
- '$distance'
- 25
Loading