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

Commit abe57b5

Browse files
authored
PHPLIB-1349 Add tests on Data Size Operators (#31)
1 parent 7deb802 commit abe57b5

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

generator/config/expression/binarySize.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ arguments:
1313
- resolvesToString
1414
- resolvesToBinData
1515
- resolvesToNull
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/binarySize/#example'
20+
pipeline:
21+
-
22+
$project:
23+
name: '$name'
24+
imageSize:
25+
$binarySize: '$binary'

generator/config/expression/bsonSize.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,37 @@ arguments:
1212
type:
1313
- resolvesToObject
1414
- resolvesToNull
15+
tests:
16+
-
17+
name: 'Return Sizes of Documents'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bsonSize/#return-sizes-of-documents'
19+
pipeline:
20+
-
21+
$project:
22+
name: 1
23+
object_size:
24+
$bsonSize: '$$ROOT'
25+
-
26+
name: 'Return Combined Size of All Documents in a Collection'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bsonSize/#return-combined-size-of-all-documents-in-a-collection'
28+
pipeline:
29+
-
30+
$group:
31+
_id: ~
32+
combined_object_size:
33+
$sum:
34+
$bsonSize: '$$ROOT'
35+
-
36+
name: 'Return Document with Largest Specified Field'
37+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bsonSize/#return-document-with-largest-specified-field'
38+
pipeline:
39+
-
40+
$project:
41+
name: '$name'
42+
task_object_size:
43+
$bsonSize: '$current_task'
44+
-
45+
$sort:
46+
task_object_size: -1
47+
-
48+
$limit: 1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\Builder\Expression;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Stage;
10+
use MongoDB\Tests\Builder\PipelineTestCase;
11+
12+
/**
13+
* Test $binarySize expression
14+
*/
15+
class BinarySizeOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
name: Expression::stringFieldPath('name'),
22+
imageSize: Expression::binarySize(
23+
Expression::binDataFieldPath('binary'),
24+
),
25+
),
26+
);
27+
28+
$this->assertSamePipeline(Pipelines::BinarySizeExample, $pipeline);
29+
}
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
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 $bsonSize expression
17+
*/
18+
class BsonSizeOperatorTest extends PipelineTestCase
19+
{
20+
public function testReturnCombinedSizeOfAllDocumentsInACollection(): void
21+
{
22+
$pipeline = new Pipeline(
23+
Stage::group(
24+
_id: null,
25+
combined_object_size: Accumulator::sum(
26+
Expression::bsonSize(
27+
Expression::variable('ROOT'),
28+
),
29+
),
30+
),
31+
);
32+
33+
$this->assertSamePipeline(Pipelines::BsonSizeReturnCombinedSizeOfAllDocumentsInACollection, $pipeline);
34+
}
35+
36+
public function testReturnDocumentWithLargestSpecifiedField(): void
37+
{
38+
$pipeline = new Pipeline(
39+
Stage::project(
40+
name: Expression::stringFieldPath('name'),
41+
task_object_size: Expression::bsonSize(
42+
Expression::objectFieldPath('current_task'),
43+
),
44+
),
45+
Stage::sort(
46+
object(task_object_size: -1),
47+
),
48+
Stage::limit(1),
49+
);
50+
51+
$this->assertSamePipeline(Pipelines::BsonSizeReturnDocumentWithLargestSpecifiedField, $pipeline);
52+
}
53+
54+
public function testReturnSizesOfDocuments(): void
55+
{
56+
$pipeline = new Pipeline(
57+
Stage::project(
58+
name: 1,
59+
object_size: Expression::bsonSize(
60+
Expression::variable('ROOT'),
61+
),
62+
),
63+
);
64+
65+
$this->assertSamePipeline(Pipelines::BsonSizeReturnSizesOfDocuments, $pipeline);
66+
}
67+
}

tests/Builder/Expression/Pipelines.php

Lines changed: 88 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)