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

Commit 9f74b96

Browse files
committed
Add tests on $minN accumulator
1 parent 4c1d6fe commit 9f74b96

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed

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

tests/Builder/Accumulator/DenseRankAccumulatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function testDenseRankPartitionsByAnIntegerField(): void
4545
quantity: -1,
4646
),
4747
output: object(
48+
// The outputWindow is optional when no window property is set.
4849
denseRankQuantityForState: Accumulator::denseRank(),
4950
),
5051
),
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Accumulator;
6+
7+
use MongoDB\Builder\Accumulator;
8+
use MongoDB\Builder\Expression;
9+
use MongoDB\Builder\Pipeline;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
use function MongoDB\object;
14+
15+
/**
16+
* Test $minN accumulator
17+
*/
18+
class MinNAccumulatorTest extends PipelineTestCase
19+
{
20+
public function testComputingNBasedOnTheGroupKeyForGroup(): void
21+
{
22+
$pipeline = new Pipeline(
23+
Stage::group(
24+
_id: object(
25+
gameId: Expression::fieldPath('gameId'),
26+
),
27+
gamescores: Accumulator::minN(
28+
input: [
29+
Expression::fieldPath('score'),
30+
Expression::fieldPath('playerId'),
31+
],
32+
n: Expression::cond(
33+
if: Expression::eq(
34+
Expression::fieldPath('gameId'),
35+
'G2',
36+
),
37+
then: 1,
38+
else: 3,
39+
),
40+
),
41+
),
42+
);
43+
44+
$this->assertSamePipeline(Pipelines::MinNComputingNBasedOnTheGroupKeyForGroup, $pipeline);
45+
}
46+
47+
public function testFindTheMinimumThreeScoresForASingleGame(): void
48+
{
49+
$pipeline = new Pipeline(
50+
Stage::match(
51+
gameId: 'G1',
52+
),
53+
Stage::group(
54+
_id: Expression::fieldPath('gameId'),
55+
minScores: Accumulator::minN(
56+
input: [
57+
Expression::fieldPath('score'),
58+
Expression::fieldPath('playerId'),
59+
],
60+
n: 3,
61+
),
62+
),
63+
);
64+
65+
$this->assertSamePipeline(Pipelines::MinNFindTheMinimumThreeScoresForASingleGame, $pipeline);
66+
}
67+
68+
public function testFindingTheMinimumThreeDocumentsAcrossMultipleGames(): void
69+
{
70+
$pipeline = new Pipeline(
71+
Stage::group(
72+
_id: Expression::fieldPath('gameId'),
73+
minScores: Accumulator::minN(
74+
input: [
75+
Expression::fieldPath('score'),
76+
Expression::fieldPath('playerId'),
77+
],
78+
n: 3,
79+
),
80+
),
81+
);
82+
83+
$this->assertSamePipeline(Pipelines::MinNFindingTheMinimumThreeDocumentsAcrossMultipleGames, $pipeline);
84+
}
85+
}

tests/Builder/Accumulator/Pipelines.php

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)