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

Commit 8b445be

Browse files
authored
PHPLIB-1351 Add tests on Miscellaneous Operators (#39)
1 parent 2603b9c commit 8b445be

15 files changed

+658
-80
lines changed

generator/config/expression/getField.yaml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ arguments:
1111
-
1212
name: field
1313
type:
14-
- string
14+
- resolvesToString
1515
description: |
1616
Field in the input object for which you want to return a value. field can be any valid expression that resolves to a string constant.
1717
If field begins with a dollar sign ($), place the field name inside of a $literal expression to return its value.
@@ -23,3 +23,47 @@ arguments:
2323
description: |
2424
Default: $$CURRENT
2525
A valid expression that contains the field for which you want to return a value. input must resolve to an object, missing, null, or undefined. If omitted, defaults to the document currently being processed in the pipeline ($$CURRENT).
26+
tests:
27+
-
28+
name: 'Query Fields that Contain Periods'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-fields-that-contain-periods--.-'
30+
pipeline:
31+
-
32+
$match:
33+
$expr:
34+
$gt:
35+
-
36+
# the builder uses the verbose form with parameter names
37+
# $getField: 'price.usd'
38+
$getField:
39+
field: 'price.usd'
40+
- 200
41+
-
42+
name: 'Query Fields that Start with a Dollar Sign'
43+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-fields-that-start-with-a-dollar-sign----'
44+
pipeline:
45+
-
46+
$match:
47+
$expr:
48+
$gt:
49+
-
50+
$getField:
51+
# the builder uses the verbose form with parameter names
52+
# $literal: '$price'
53+
field:
54+
$literal: '$price'
55+
- 200
56+
-
57+
name: 'Query a Field in a Sub-document'
58+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-a-field-in-a-sub-document'
59+
pipeline:
60+
-
61+
$match:
62+
$expr:
63+
$lte:
64+
-
65+
$getField:
66+
field:
67+
$literal: '$small'
68+
input: '$quantity'
69+
- 20

generator/config/expression/rand.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,42 @@ type:
66
encode: object
77
description: |
88
Returns a random float between 0 and 1
9+
tests:
10+
-
11+
name: 'Generate Random Data Points'
12+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rand/#generate-random-data-points'
13+
pipeline:
14+
-
15+
$set:
16+
amount:
17+
$multiply:
18+
-
19+
$rand: {}
20+
- 100
21+
-
22+
$set:
23+
amount:
24+
$floor: '$amount'
25+
-
26+
# $merge: 'donors'
27+
$merge:
28+
into: 'donors'
29+
-
30+
name: 'Select Random Items From a Collection'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rand/#select-random-items-from-a-collection'
32+
pipeline:
33+
-
34+
$match:
35+
district: 3
36+
-
37+
$match:
38+
$expr:
39+
$lt:
40+
- 0.5
41+
-
42+
$rand: {}
43+
-
44+
$project:
45+
_id: 0
46+
name: 1
47+
registered: 1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# $schema: ../schema.json
2+
name: $toHashedIndexKey
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toHashedIndexKey/'
4+
type:
5+
- resolvesToLong
6+
encode: single
7+
description: |
8+
Computes and returns the hash value of the input expression using the same hash function that MongoDB uses to create a hashed index. A hash function maps a key or string to a fixed-size numeric value.
9+
arguments:
10+
-
11+
name: value
12+
type:
13+
- expression
14+
description: |
15+
key or string to hash
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toHashedIndexKey/#example'
20+
pipeline:
21+
-
22+
$documents:
23+
-
24+
val: 'string to hash'
25+
-
26+
$addFields:
27+
hashedVal:
28+
$toHashedIndexKey: '$val'

generator/config/expression/sampleRate.yaml renamed to generator/config/query/sampleRate.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: $sampleRate
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sampleRate/'
44
type:
5-
- resolvesToAny
5+
- query
66
encode: single
77
description: |
88
Randomly select documents at a given rate. Although the exact number of documents selected varies on each run, the quantity chosen approximates the sample rate expressed as a percentage of the total number of documents.
@@ -14,3 +14,13 @@ arguments:
1414
description: |
1515
The selection process uses a uniform random distribution. The sample rate is a floating point number between 0 and 1, inclusive, which represents the probability that a given document will be selected as it passes through the pipeline.
1616
For example, a sample rate of 0.33 selects roughly one document in three.
17+
tests:
18+
-
19+
name: 'Example'
20+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sampleRate/#examples'
21+
pipeline:
22+
-
23+
$match:
24+
$sampleRate: 0.33
25+
-
26+
$count: 'numMatches'

src/Builder/Expression/FactoryTrait.php

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

src/Builder/Expression/GetFieldOperator.php

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

src/Builder/Expression/ToHashedIndexKeyOperator.php

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

src/Builder/Query/FactoryTrait.php

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

src/Builder/Expression/SampleRateOperator.php renamed to src/Builder/Query/SampleRateOperator.php

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Query;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $getField expression
15+
*/
16+
class GetFieldOperatorTest extends PipelineTestCase
17+
{
18+
public function testQueryAFieldInASubdocument(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::match(
22+
Query::expr(
23+
Expression::lte(
24+
Expression::getField(
25+
field: Expression::literal('$small'),
26+
input: Expression::intFieldPath('quantity'),
27+
),
28+
20,
29+
),
30+
),
31+
),
32+
);
33+
34+
$this->assertSamePipeline(Pipelines::GetFieldQueryAFieldInASubdocument, $pipeline);
35+
}
36+
37+
public function testQueryFieldsThatContainPeriods(): void
38+
{
39+
$pipeline = new Pipeline(
40+
Stage::match(
41+
Query::expr(
42+
Expression::gt(
43+
Expression::getField('price.usd'),
44+
200,
45+
),
46+
),
47+
),
48+
);
49+
50+
$this->assertSamePipeline(Pipelines::GetFieldQueryFieldsThatContainPeriods, $pipeline);
51+
}
52+
53+
public function testQueryFieldsThatStartWithADollarSign(): void
54+
{
55+
$pipeline = new Pipeline(
56+
Stage::match(
57+
Query::expr(
58+
Expression::gt(
59+
Expression::getField(
60+
Expression::literal('$price'),
61+
),
62+
200,
63+
),
64+
),
65+
),
66+
);
67+
68+
$this->assertSamePipeline(Pipelines::GetFieldQueryFieldsThatStartWithADollarSign, $pipeline);
69+
}
70+
}

0 commit comments

Comments
 (0)