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

PHPLIB-1358 Add tests on Type Expression Operators #49

Merged
merged 1 commit into from
Jan 24, 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
47 changes: 47 additions & 0 deletions generator/config/expression/convert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,50 @@ arguments:
description: |
The value to return if the input is null or missing. The arguments can be any valid expression.
If unspecified, $convert returns null if the input is null or missing.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/convert/#example'
pipeline:
-
$addFields:
convertedPrice:
$convert:
input: '$price'
to: 'decimal'
onError: 'Error'
onNull: !bson_decimal128 '0'
convertedQty:
$convert:
input: '$qty'
to: 'int'
onError:
$concat:
- 'Could not convert '
-
$toString: '$qty'
- ' to type integer.'
onNull: 0
-
$project:
totalPrice:
$switch:
branches:
-
case:
$eq:
-
$type: '$convertedPrice'
- 'string'
then: 'NaN'
-
case:
$eq:
-
$type: '$convertedQty'
- 'string'
then: 'NaN'
default:
$multiply:
- '$convertedPrice'
- '$convertedQty'
61 changes: 60 additions & 1 deletion generator/config/expression/isNumber.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,63 @@ arguments:
name: expression
type:
- expression
variadic: array
tests:
-
name: 'Use $isNumber to Check if a Field is Numeric'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isNumber/#use--isnumber-to-check-if-a-field-is-numeric'
pipeline:
-
$addFields:
isNumber:
$isNumber: '$reading'
hasType:
$type: '$reading'
-
name: 'Conditionally Modify Fields using $isNumber'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isNumber/#conditionally-modify-fields-using--isnumber'
pipeline:
-
$addFields:
points:
$cond:
if:
$isNumber: '$grade'
then: '$grade'
else:
$switch:
branches:
-
case:
$eq:
- '$grade'
- 'A'
then: 4
-
case:
$eq:
- '$grade'
- 'B'
then: 3
-
case:
$eq:
- '$grade'
- 'C'
then: 2
-
case:
$eq:
- '$grade'
- 'D'
then: 1
-
case:
$eq:
- '$grade'
- 'F'
then: 0
-
$group:
_id: '$student_id'
GPA:
$avg: '$points'
27 changes: 27 additions & 0 deletions generator/config/expression/toBool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,30 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toBool/#example'
pipeline:
-
$addFields:
convertedShippedFlag:
$switch:
branches:
-
case:
$eq:
- '$shipped'
- 'false'
then: false
-
case:
$eq:
- '$shipped'
- ''
then: false
default:
$toBool: '$shipped'
-
$match:
convertedShippedFlag: false
9 changes: 9 additions & 0 deletions generator/config/expression/toDecimal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDecimal/#example'
pipeline:
-
$addFields:
convertedPrice:
$toDecimal: '$price'
13 changes: 13 additions & 0 deletions generator/config/expression/toDouble.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDouble/#example'
pipeline:
-
$addFields:
degrees:
$toDouble:
$substrBytes:
- '$temp'
- 0
- 4
9 changes: 9 additions & 0 deletions generator/config/expression/toInt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toInt/#example'
pipeline:
-
$addFields:
convertedQty:
$toInt: '$qty'
12 changes: 12 additions & 0 deletions generator/config/expression/toLong.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toLong/#example'
pipeline:
-
$addFields:
convertedQty:
$toLong: '$qty'
-
$sort:
convertedQty: -1
12 changes: 12 additions & 0 deletions generator/config/expression/toObjectId.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toObjectId/#example'
pipeline:
-
$addFields:
convertedId:
$toObjectId: '$_id'
-
$sort:
convertedId: -1
9 changes: 9 additions & 0 deletions generator/config/expression/type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/type/#example'
pipeline:
-
$project:
a:
$type: '$a'
12 changes: 12 additions & 0 deletions generator/js2yaml.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ <h1>Convert JS examples into Yaml</h1>
return new TaggedValue('bson_binary', value);
}

function Decimal128(value) {
return new TaggedValue('bson_decimal128', value)
}

function Int32(value) {
return parseInt(value);
}

function Int64(value) {
return new TaggedValue('bson_int64', value)
}

function convert(jsString) {
try {
return toYaml(eval(jsString), 1);
Expand Down
7 changes: 3 additions & 4 deletions src/Builder/Expression/FactoryTrait.php

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

20 changes: 4 additions & 16 deletions src/Builder/Expression/IsNumberOperator.php

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

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

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\BSON\Decimal128;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

use function MongoDB\object;

/**
* Test $convert expression
*/
class ConvertOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::addFields(
convertedPrice: Expression::convert(
input: Expression::fieldPath('price'),
to: 'decimal',
onError: 'Error',
onNull: new Decimal128('0'),
),
convertedQty: Expression::convert(
input: Expression::fieldPath('qty'),
to: 'int',
onError: Expression::concat(
'Could not convert ',
Expression::toString(
Expression::fieldPath('qty'),
),
' to type integer.',
),
onNull: 0,
),
),
Stage::project(
totalPrice: Expression::switch(
branches: [
object(
case: Expression::eq(
Expression::type(
Expression::fieldPath('convertedPrice'),
),
'string',
),
then: 'NaN',
),
object(
case: Expression::eq(
Expression::type(
Expression::fieldPath('convertedQty'),
),
'string',
),
then: 'NaN',
),
],
default: Expression::multiply(
Expression::fieldPath('convertedPrice'),
Expression::fieldPath('convertedQty'),
),
),
),
);

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