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

PHPLIB-1351 Add tests on Miscellaneous Operators #39

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion generator/config/expression/getField.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ arguments:
-
name: field
type:
- string
- resolvesToString
description: |
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.
If field begins with a dollar sign ($), place the field name inside of a $literal expression to return its value.
Expand All @@ -23,3 +23,47 @@ arguments:
description: |
Default: $$CURRENT
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).
tests:
-
name: 'Query Fields that Contain Periods'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-fields-that-contain-periods--.-'
pipeline:
-
$match:
$expr:
$gt:
-
# the builder uses the verbose form with parameter names
# $getField: 'price.usd'
$getField:
field: 'price.usd'
- 200
-
name: 'Query Fields that Start with a Dollar Sign'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-fields-that-start-with-a-dollar-sign----'
pipeline:
-
$match:
$expr:
$gt:
-
$getField:
# the builder uses the verbose form with parameter names
# $literal: '$price'
field:
$literal: '$price'
- 200
-
name: 'Query a Field in a Sub-document'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/getField/#query-a-field-in-a-sub-document'
pipeline:
-
$match:
$expr:
$lte:
-
$getField:
field:
$literal: '$small'
input: '$quantity'
- 20
39 changes: 39 additions & 0 deletions generator/config/expression/rand.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,42 @@ type:
encode: object
description: |
Returns a random float between 0 and 1
tests:
-
name: 'Generate Random Data Points'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rand/#generate-random-data-points'
pipeline:
-
$set:
amount:
$multiply:
-
$rand: {}
- 100
-
$set:
amount:
$floor: '$amount'
-
# $merge: 'donors'
$merge:
into: 'donors'
-
name: 'Select Random Items From a Collection'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rand/#select-random-items-from-a-collection'
pipeline:
-
$match:
district: 3
-
$match:
$expr:
$lt:
- 0.5
-
$rand: {}
-
$project:
_id: 0
name: 1
registered: 1
28 changes: 28 additions & 0 deletions generator/config/expression/toHashedIndexKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# $schema: ../schema.json
name: $toHashedIndexKey
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toHashedIndexKey/'
type:
- resolvesToLong
encode: single
description: |
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.
arguments:
-
name: value
type:
- expression
description: |
key or string to hash
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toHashedIndexKey/#example'
pipeline:
-
$documents:
-
val: 'string to hash'
-
$addFields:
hashedVal:
$toHashedIndexKey: '$val'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: $sampleRate
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sampleRate/'
type:
- resolvesToAny
- query
encode: single
description: |
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.
Expand All @@ -14,3 +14,13 @@ arguments:
description: |
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.
For example, a sample rate of 0.33 selects roughly one document in three.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sampleRate/#examples'
pipeline:
-
$match:
$sampleRate: 0.33
-
$count: 'numMatches'
29 changes: 15 additions & 14 deletions src/Builder/Expression/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/Builder/Expression/GetFieldOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/Builder/Expression/ToHashedIndexKeyOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Builder/Query/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions tests/Builder/Expression/GetFieldOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $getField expression
*/
class GetFieldOperatorTest extends PipelineTestCase
{
public function testQueryAFieldInASubdocument(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::expr(
Expression::lte(
Expression::getField(
field: Expression::literal('$small'),
input: Expression::intFieldPath('quantity'),
),
20,
),
),
),
);

$this->assertSamePipeline(Pipelines::GetFieldQueryAFieldInASubdocument, $pipeline);
}

public function testQueryFieldsThatContainPeriods(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::expr(
Expression::gt(
Expression::getField('price.usd'),
200,
),
),
),
);

$this->assertSamePipeline(Pipelines::GetFieldQueryFieldsThatContainPeriods, $pipeline);
}

public function testQueryFieldsThatStartWithADollarSign(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::expr(
Expression::gt(
Expression::getField(
Expression::literal('$price'),
),
200,
),
),
),
);

$this->assertSamePipeline(Pipelines::GetFieldQueryFieldsThatStartWithADollarSign, $pipeline);
}
}
Loading