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

Commit 2660e6d

Browse files
authored
PHPLIB-1359 Add tests on Accumulators ($group, $bucket, $bucketAuto, $setWindowFields) (#29)
1 parent defeaeb commit 2660e6d

Some content is hidden

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

64 files changed

+3887
-154
lines changed

generator/config/accumulator/avg.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ arguments:
1313
name: expression
1414
type:
1515
- resolvesToNumber
16+
tests:
17+
-
18+
name: 'Use in $group Stage'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--group-stage'
20+
pipeline:
21+
- $group:
22+
_id: '$item'
23+
avgAmount:
24+
$avg:
25+
$multiply:
26+
- '$price'
27+
- '$quantity'
28+
avgQuantity:
29+
$avg: '$quantity'
30+
-
31+
name: 'Use in $setWindowFields Stage'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--setwindowfields-stage'
33+
pipeline:
34+
-
35+
$setWindowFields:
36+
partitionBy: '$state'
37+
sortBy:
38+
orderDate: 1
39+
output:
40+
averageQuantityForState:
41+
$avg: '$quantity'
42+
window:
43+
documents:
44+
- 'unbounded'
45+
- 'current'

generator/config/accumulator/bottom.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,35 @@ arguments:
2121
- expression
2222
description: |
2323
Represents the output for each element in the group and can be any expression.
24+
tests:
25+
-
26+
name: 'Find the Bottom Score'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottom/#find-the-bottom-score'
28+
pipeline:
29+
-
30+
$match:
31+
gameId: 'G1'
32+
-
33+
$group:
34+
_id: '$gameId'
35+
playerId:
36+
$bottom:
37+
output:
38+
- '$playerId'
39+
- '$score'
40+
sortBy:
41+
score: -1
42+
-
43+
name: 'Finding the Bottom Score Across Multiple Games'
44+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottom/#finding-the-bottom-score-across-multiple-games'
45+
pipeline:
46+
-
47+
$group:
48+
_id: '$gameId'
49+
playerId:
50+
$bottom:
51+
output:
52+
- '$playerId'
53+
- '$score'
54+
sortBy:
55+
score: -1

generator/config/accumulator/bottomN.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,58 @@ arguments:
2828
- expression
2929
description: |
3030
Represents the output for each element in the group and can be any expression.
31+
tests:
32+
-
33+
name: 'Find the Three Lowest Scores'
34+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#find-the-three-lowest-scores'
35+
pipeline:
36+
-
37+
$match:
38+
gameId: 'G1'
39+
-
40+
$group:
41+
_id: '$gameId'
42+
playerId:
43+
$bottomN:
44+
output:
45+
- '$playerId'
46+
- '$score'
47+
sortBy:
48+
score: -1
49+
n: 3
50+
-
51+
name: 'Finding the Three Lowest Score Documents Across Multiple Games'
52+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#finding-the-three-lowest-score-documents-across-multiple-games'
53+
pipeline:
54+
-
55+
$group:
56+
_id: '$gameId'
57+
playerId:
58+
$bottomN:
59+
output:
60+
- '$playerId'
61+
- '$score'
62+
sortBy:
63+
score: -1
64+
n: 3
65+
-
66+
name: 'Computing n Based on the Group Key for $group'
67+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#computing-n-based-on-the-group-key-for--group'
68+
pipeline:
69+
-
70+
$group:
71+
_id:
72+
gameId: '$gameId'
73+
gamescores:
74+
$bottomN:
75+
output: '$score'
76+
n:
77+
$cond:
78+
if:
79+
$eq:
80+
- '$gameId'
81+
- 'G2'
82+
then: 1
83+
else: 3
84+
sortBy:
85+
score: -1

generator/config/accumulator/count.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,29 @@ description: |
99
Returns the number of documents in the group or window.
1010
Distinct from the $count pipeline stage.
1111
New in MongoDB 5.0.
12+
tests:
13+
-
14+
name: 'Use in $group Stage'
15+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--group-stage'
16+
pipeline:
17+
-
18+
$group:
19+
_id: '$state'
20+
countNumberOfDocumentsForState:
21+
$count: {}
22+
-
23+
name: 'Use in $setWindowFields Stage'
24+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--setwindowfields-stage'
25+
pipeline:
26+
-
27+
$setWindowFields:
28+
partitionBy: '$state'
29+
sortBy:
30+
orderDate: 1
31+
output:
32+
countNumberOfDocumentsForState:
33+
$count: {}
34+
window:
35+
documents:
36+
- 'unbounded'
37+
- 'current'

generator/config/accumulator/firstN.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,68 @@ tests:
5555
$firstN:
5656
input: '$score'
5757
n: 5
58+
-
59+
name: 'Find the First Three Player Scores for a Single Game'
60+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#find-the-first-three-player-scores-for-a-single-game'
61+
pipeline:
62+
-
63+
$match:
64+
gameId: 'G1'
65+
-
66+
$group:
67+
_id: '$gameId'
68+
firstThreeScores:
69+
$firstN:
70+
input:
71+
- '$playerId'
72+
- '$score'
73+
n: 3
74+
-
75+
name: 'Finding the First Three Player Scores Across Multiple Games'
76+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#finding-the-first-three-player-scores-across-multiple-games'
77+
pipeline:
78+
-
79+
$group:
80+
_id: '$gameId'
81+
playerId:
82+
$firstN:
83+
input:
84+
- '$playerId'
85+
- '$score'
86+
n: 3
87+
-
88+
name: 'Using $sort With $firstN'
89+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#using--sort-with--firstn'
90+
pipeline:
91+
-
92+
$sort:
93+
score: -1
94+
-
95+
$group:
96+
_id: '$gameId'
97+
playerId:
98+
$firstN:
99+
input:
100+
- '$playerId'
101+
- '$score'
102+
n: 3
103+
-
104+
name: 'Computing n Based on the Group Key for $group'
105+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#computing-n-based-on-the-group-key-for--group'
106+
pipeline:
107+
-
108+
$group:
109+
_id:
110+
gameId: '$gameId'
111+
gamescores:
112+
$firstN:
113+
input: '$score'
114+
n:
115+
$cond:
116+
if:
117+
$eq:
118+
- '$gameId'
119+
- 'G2'
120+
then: 1
121+
else: 3
122+

generator/config/accumulator/max.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,34 @@ arguments:
1313
name: expression
1414
type:
1515
- expression
16+
tests:
17+
-
18+
name: 'Use in $group Stage'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#use-in--group-stage'
20+
pipeline:
21+
-
22+
$group:
23+
_id: '$item'
24+
maxTotalAmount:
25+
$max:
26+
$multiply:
27+
- '$price'
28+
- '$quantity'
29+
maxQuantity:
30+
$max: '$quantity'
31+
-
32+
name: 'Use in $setWindowFields Stage'
33+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#use-in--setwindowfields-stage'
34+
pipeline:
35+
-
36+
$setWindowFields:
37+
partitionBy: '$state'
38+
sortBy:
39+
orderDate: 1
40+
output:
41+
maximumQuantityForState:
42+
$max: '$quantity'
43+
window:
44+
documents:
45+
- 'unbounded'
46+
- 'current'

generator/config/accumulator/maxN.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 Maximum Three Scores for a Single Game'
26+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#find-the-maximum-three-scores-for-a-single-game'
27+
pipeline:
28+
-
29+
$match:
30+
gameId: 'G1'
31+
-
32+
$group:
33+
_id: '$gameId'
34+
maxThreeScores:
35+
$maxN:
36+
input:
37+
- '$score'
38+
- '$playerId'
39+
n: 3
40+
-
41+
name: 'Finding the Maximum Three Scores Across Multiple Games'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#finding-the-maximum-three-scores-across-multiple-games'
43+
pipeline:
44+
-
45+
$group:
46+
_id: '$gameId'
47+
maxScores:
48+
$maxN:
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/maxN/#computing-n-based-on-the-group-key-for--group'
56+
pipeline:
57+
-
58+
$group:
59+
_id:
60+
gameId: '$gameId'
61+
gamescores:
62+
$maxN:
63+
input:
64+
- '$score'
65+
- '$playerId'
66+
n:
67+
$cond:
68+
if:
69+
$eq:
70+
- '$gameId'
71+
- 'G2'
72+
then: 1
73+
else: 3

generator/config/accumulator/median.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,37 @@ arguments:
2525
- string # AccumulatorPercentile
2626
description: |
2727
The method that mongod uses to calculate the 50th percentile value. The method must be 'approximate'.
28+
tests:
29+
-
30+
name: 'Use $median as an Accumulator'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/median/#use-operatorname-as-an-accumulator'
32+
pipeline:
33+
-
34+
$group:
35+
_id: ~
36+
test01_median:
37+
$median:
38+
input: '$test01'
39+
method: 'approximate'
40+
-
41+
name: 'Use $median in a $setWindowField Stage'
42+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/median/#use-operatorname-in-a--setwindowfield-stage'
43+
pipeline:
44+
-
45+
$setWindowFields:
46+
sortBy:
47+
test01: 1
48+
output:
49+
test01_median:
50+
$median:
51+
input: '$test01'
52+
method: 'approximate'
53+
window:
54+
range:
55+
- -3
56+
- 3
57+
-
58+
$project:
59+
_id: 0
60+
studentId: 1
61+
test01_median: 1

generator/config/accumulator/mergeObjects.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ arguments:
1111
name: document
1212
type:
1313
- resolvesToObject
14-
variadic: array
1514
description: |
1615
Any valid expression that resolves to a document.
16+
tests:
17+
-
18+
name: '$mergeObjects as an Accumulator'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/#-mergeobjects-as-an-accumulator'
20+
pipeline:
21+
-
22+
$group:
23+
_id: '$item'
24+
mergedSales:
25+
$mergeObjects: '$quantity'

0 commit comments

Comments
 (0)