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

Commit aa7cffc

Browse files
authored
PHPLIB-1362 Add tests on Window Operators (#51)
* PHPLIB-1362 Add tests on Window Operators * Add tests on $minN accumulator
1 parent 5951e36 commit aa7cffc

35 files changed

+1636
-127
lines changed

generator/config/accumulator/covariancePop.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ arguments:
1616
name: expression2
1717
type:
1818
- resolvesToNumber
19+
tests:
20+
-
21+
name: 'Example'
22+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/covariancePop/#example'
23+
pipeline:
24+
-
25+
$setWindowFields:
26+
partitionBy: '$state'
27+
sortBy:
28+
orderDate: 1
29+
output:
30+
covariancePopForState:
31+
$covariancePop:
32+
-
33+
# $year: '$orderDate'
34+
$year:
35+
date: '$orderDate'
36+
- '$quantity'
37+
window:
38+
documents:
39+
- 'unbounded'
40+
- 'current'

generator/config/accumulator/covarianceSamp.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ arguments:
1616
name: expression2
1717
type:
1818
- resolvesToNumber
19+
tests:
20+
-
21+
name: 'Example'
22+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/covarianceSamp/#example'
23+
pipeline:
24+
-
25+
$setWindowFields:
26+
partitionBy: '$state'
27+
sortBy:
28+
orderDate: 1
29+
output:
30+
covarianceSampForState:
31+
$covarianceSamp:
32+
-
33+
# $year: '$orderDate'
34+
$year:
35+
date: '$orderDate'
36+
- '$quantity'
37+
window:
38+
documents:
39+
- 'unbounded'
40+
- 'current'

generator/config/accumulator/denseRank.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,28 @@ encode: object
77
description: |
88
Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition. There are no gaps in the ranks. Ties receive the same rank.
99
New in MongoDB 5.0.
10+
tests:
11+
-
12+
name: 'Dense Rank Partitions by an Integer Field'
13+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/#dense-rank-partitions-by-an-integer-field'
14+
pipeline:
15+
-
16+
$setWindowFields:
17+
partitionBy: '$state'
18+
sortBy:
19+
quantity: -1
20+
output:
21+
denseRankQuantityForState:
22+
$denseRank: {}
23+
-
24+
name: 'Dense Rank Partitions by a Date Field'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/#dense-rank-partitions-by-a-date-field'
26+
pipeline:
27+
-
28+
$setWindowFields:
29+
partitionBy: '$state'
30+
sortBy:
31+
orderDate: 1
32+
output:
33+
denseRankOrderDateForState:
34+
$denseRank: {}

generator/config/accumulator/derivative.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ arguments:
2121
description: |
2222
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
2323
If the sortBy field is not a date, you must omit a unit. If you specify a unit, you must specify a date in the sortBy field.
24+
tests:
25+
-
26+
name: 'Example'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/derivative/#example'
28+
pipeline:
29+
-
30+
$setWindowFields:
31+
partitionBy: '$truckID'
32+
sortBy:
33+
timeStamp: 1
34+
output:
35+
truckAverageSpeed:
36+
$derivative:
37+
input: '$miles'
38+
unit: 'hour'
39+
window:
40+
range:
41+
- -30
42+
- 0
43+
unit: 'second'
44+
-
45+
$match:
46+
truckAverageSpeed:
47+
$gt: 50

generator/config/accumulator/documentNumber.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ encode: object
77
description: |
88
Returns the position of a document (known as the document number) in the $setWindowFields stage partition. Ties result in different adjacent document numbers.
99
New in MongoDB 5.0.
10+
tests:
11+
-
12+
name: 'Document Number for Each State'
13+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/documentNumber/#document-number-for-each-state'
14+
pipeline:
15+
-
16+
$setWindowFields:
17+
partitionBy: '$state'
18+
sortBy:
19+
quantity: -1
20+
output:
21+
documentNumberForState:
22+
$documentNumber: {}

generator/config/accumulator/expMovingAvg.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,32 @@ arguments:
2929
description: |
3030
A double that specifies the exponential decay value to use in the exponential moving average calculation. A higher alpha value assigns a lower mathematical significance to previous results from the calculation.
3131
You must specify either N or alpha. You cannot specify both.
32+
tests:
33+
-
34+
name: 'Exponential Moving Average Using N'
35+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/expMovingAvg/#exponential-moving-average-using-n'
36+
pipeline:
37+
-
38+
$setWindowFields:
39+
partitionBy: '$stock'
40+
sortBy:
41+
date: 1
42+
output:
43+
expMovingAvgForStock:
44+
$expMovingAvg:
45+
input: '$price'
46+
N: 2
47+
-
48+
name: 'Exponential Moving Average Using alpha'
49+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/expMovingAvg/#exponential-moving-average-using-alpha'
50+
pipeline:
51+
-
52+
$setWindowFields:
53+
partitionBy: '$stock'
54+
sortBy:
55+
date: 1
56+
output:
57+
expMovingAvgForStock:
58+
$expMovingAvg:
59+
input: '$price'
60+
alpha: 0.75
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# $schema: ../schema.json
2+
name: $integral
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/'
4+
type:
5+
- window
6+
encode: object
7+
description: |
8+
Returns the approximation of the area under a curve.
9+
New in MongoDB 5.0.
10+
arguments:
11+
-
12+
name: input
13+
type:
14+
- resolvesToNumber
15+
- resolvesToDate
16+
-
17+
name: unit
18+
type:
19+
- resolvesToString
20+
optional: true
21+
description: |
22+
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
23+
If the sortBy field is not a date, you must omit a unit. If you specify a unit, you must specify a date in the sortBy field.
24+
tests:
25+
-
26+
name: 'Example'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/#example'
28+
pipeline:
29+
-
30+
$setWindowFields:
31+
partitionBy: '$powerMeterID'
32+
sortBy:
33+
timeStamp: 1
34+
output:
35+
powerMeterKilowattHours:
36+
$integral:
37+
input: '$kilowatts'
38+
unit: 'hour'
39+
window:
40+
range:
41+
- 'unbounded'
42+
- 'current'
43+
unit: 'hour'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# $schema: ../schema.json
2+
name: $linearFill
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/'
4+
type:
5+
- window
6+
encode: single
7+
description: |
8+
Fills null and missing fields in a window using linear interpolation based on surrounding field values.
9+
Available in the $setWindowFields stage.
10+
New in MongoDB 5.3.
11+
arguments:
12+
-
13+
name: expression
14+
type:
15+
- resolvesToNumber
16+
tests:
17+
-
18+
name: 'Fill Missing Values with Linear Interpolation'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/#fill-missing-values-with-linear-interpolation'
20+
pipeline:
21+
-
22+
$setWindowFields:
23+
sortBy:
24+
time: 1
25+
output:
26+
price:
27+
$linearFill: '$price'
28+
-
29+
name: 'Use Multiple Fill Methods in a Single Stage'
30+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/#use-multiple-fill-methods-in-a-single-stage'
31+
pipeline:
32+
-
33+
$setWindowFields:
34+
sortBy:
35+
time: 1
36+
output:
37+
linearFillPrice:
38+
$linearFill: '$price'
39+
locfPrice:
40+
$locf: '$price'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# $schema: ../schema.json
2+
name: $locf
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf/'
4+
type:
5+
- window
6+
encode: single
7+
description: |
8+
Last observation carried forward. Sets values for null and missing fields in a window to the last non-null value for the field.
9+
Available in the $setWindowFields stage.
10+
New in MongoDB 5.2.
11+
arguments:
12+
-
13+
name: expression
14+
type:
15+
- expression
16+
tests:
17+
-
18+
name: 'Fill Missing Values with the Last Observed Value'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf/#fill-missing-values-with-the-last-observed-value'
20+
pipeline:
21+
-
22+
$setWindowFields:
23+
sortBy:
24+
time: 1
25+
output:
26+
price:
27+
$locf: '$price'

generator/config/accumulator/minN.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,54 @@ arguments:
2020
- resolvesToInt
2121
description: |
2222
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
23+
tests:
24+
-
25+
name: 'Find the Minimum Three Scores for a Single Game'
26+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#find-the-minimum-three-scores-for-a-single-game'
27+
pipeline:
28+
-
29+
$match:
30+
gameId: 'G1'
31+
-
32+
$group:
33+
_id: '$gameId'
34+
minScores:
35+
$minN:
36+
input:
37+
- '$score'
38+
- '$playerId'
39+
n: 3
40+
-
41+
name: 'Finding the Minimum Three Documents Across Multiple Games'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#finding-the-minimum-three-documents-across-multiple-games'
43+
pipeline:
44+
-
45+
$group:
46+
_id: '$gameId'
47+
minScores:
48+
$minN:
49+
input:
50+
- '$score'
51+
- '$playerId'
52+
n: 3
53+
-
54+
name: 'Computing n Based on the Group Key for $group'
55+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#computing-n-based-on-the-group-key-for--group'
56+
pipeline:
57+
-
58+
$group:
59+
_id:
60+
gameId: '$gameId'
61+
gamescores:
62+
$minN:
63+
input:
64+
- '$score'
65+
- '$playerId'
66+
n:
67+
$cond:
68+
if:
69+
$eq:
70+
- '$gameId'
71+
- 'G2'
72+
then: 1
73+
else: 3
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# $schema: ../schema.json
2+
name: $rank
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/'
4+
type:
5+
- window
6+
encode: object
7+
description: |
8+
Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition.
9+
New in MongoDB 5.0.
10+
tests:
11+
-
12+
name: 'Rank Partitions by an Integer Field'
13+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/#rank-partitions-by-an-integer-field'
14+
pipeline:
15+
-
16+
$setWindowFields:
17+
partitionBy: '$state'
18+
sortBy:
19+
quantity: -1
20+
output:
21+
rankQuantityForState:
22+
$rank: {}
23+
-
24+
name: 'Rank Partitions by a Date Field'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/#rank-partitions-by-a-date-field'
26+
pipeline:
27+
-
28+
$setWindowFields:
29+
partitionBy: '$state'
30+
sortBy:
31+
orderDate: 1
32+
output:
33+
rankOrderDateForState:
34+
$rank: {}

generator/config/accumulator/shift.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,34 @@ arguments:
3232
Specifies an optional default expression to evaluate if the document position is outside of the implicit $setWindowFields stage window. The implicit window contains all the documents in the partition.
3333
The default expression must evaluate to a constant value.
3434
If you do not specify a default expression, $shift returns null for documents whose positions are outside of the implicit $setWindowFields stage window.
35+
tests:
36+
-
37+
name: 'Shift Using a Positive Integer'
38+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/shift/#shift-using-a-positive-integer'
39+
pipeline:
40+
-
41+
$setWindowFields:
42+
partitionBy: '$state'
43+
sortBy:
44+
quantity: -1
45+
output:
46+
shiftQuantityForState:
47+
$shift:
48+
output: '$quantity'
49+
by: 1
50+
default: 'Not available'
51+
-
52+
name: 'Shift Using a Negative Integer'
53+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/shift/#shift-using-a-negative-integer'
54+
pipeline:
55+
-
56+
$setWindowFields:
57+
partitionBy: '$state'
58+
sortBy:
59+
quantity: -1
60+
output:
61+
shiftQuantityForState:
62+
$shift:
63+
output: '$quantity'
64+
by: -1
65+
default: 'Not available'

0 commit comments

Comments
 (0)