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

Commit ff4fc79

Browse files
authored
PHPLIB-1358 Add tests on Type Expression Operators (#49)
1 parent 2ccad18 commit ff4fc79

22 files changed

+995
-21
lines changed

generator/config/expression/convert.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,50 @@ arguments:
3333
description: |
3434
The value to return if the input is null or missing. The arguments can be any valid expression.
3535
If unspecified, $convert returns null if the input is null or missing.
36+
tests:
37+
-
38+
name: 'Example'
39+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/convert/#example'
40+
pipeline:
41+
-
42+
$addFields:
43+
convertedPrice:
44+
$convert:
45+
input: '$price'
46+
to: 'decimal'
47+
onError: 'Error'
48+
onNull: !bson_decimal128 '0'
49+
convertedQty:
50+
$convert:
51+
input: '$qty'
52+
to: 'int'
53+
onError:
54+
$concat:
55+
- 'Could not convert '
56+
-
57+
$toString: '$qty'
58+
- ' to type integer.'
59+
onNull: 0
60+
-
61+
$project:
62+
totalPrice:
63+
$switch:
64+
branches:
65+
-
66+
case:
67+
$eq:
68+
-
69+
$type: '$convertedPrice'
70+
- 'string'
71+
then: 'NaN'
72+
-
73+
case:
74+
$eq:
75+
-
76+
$type: '$convertedQty'
77+
- 'string'
78+
then: 'NaN'
79+
default:
80+
$multiply:
81+
- '$convertedPrice'
82+
- '$convertedQty'

generator/config/expression/isNumber.yaml

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,63 @@ arguments:
1313
name: expression
1414
type:
1515
- expression
16-
variadic: array
16+
tests:
17+
-
18+
name: 'Use $isNumber to Check if a Field is Numeric'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isNumber/#use--isnumber-to-check-if-a-field-is-numeric'
20+
pipeline:
21+
-
22+
$addFields:
23+
isNumber:
24+
$isNumber: '$reading'
25+
hasType:
26+
$type: '$reading'
27+
-
28+
name: 'Conditionally Modify Fields using $isNumber'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isNumber/#conditionally-modify-fields-using--isnumber'
30+
pipeline:
31+
-
32+
$addFields:
33+
points:
34+
$cond:
35+
if:
36+
$isNumber: '$grade'
37+
then: '$grade'
38+
else:
39+
$switch:
40+
branches:
41+
-
42+
case:
43+
$eq:
44+
- '$grade'
45+
- 'A'
46+
then: 4
47+
-
48+
case:
49+
$eq:
50+
- '$grade'
51+
- 'B'
52+
then: 3
53+
-
54+
case:
55+
$eq:
56+
- '$grade'
57+
- 'C'
58+
then: 2
59+
-
60+
case:
61+
$eq:
62+
- '$grade'
63+
- 'D'
64+
then: 1
65+
-
66+
case:
67+
$eq:
68+
- '$grade'
69+
- 'F'
70+
then: 0
71+
-
72+
$group:
73+
_id: '$student_id'
74+
GPA:
75+
$avg: '$points'

generator/config/expression/toBool.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,30 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toBool/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
convertedShippedFlag:
23+
$switch:
24+
branches:
25+
-
26+
case:
27+
$eq:
28+
- '$shipped'
29+
- 'false'
30+
then: false
31+
-
32+
case:
33+
$eq:
34+
- '$shipped'
35+
- ''
36+
then: false
37+
default:
38+
$toBool: '$shipped'
39+
-
40+
$match:
41+
convertedShippedFlag: false

generator/config/expression/toDecimal.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDecimal/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
convertedPrice:
23+
$toDecimal: '$price'

generator/config/expression/toDouble.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDouble/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
degrees:
23+
$toDouble:
24+
$substrBytes:
25+
- '$temp'
26+
- 0
27+
- 4

generator/config/expression/toInt.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toInt/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
convertedQty:
23+
$toInt: '$qty'

generator/config/expression/toLong.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toLong/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
convertedQty:
23+
$toLong: '$qty'
24+
-
25+
$sort:
26+
convertedQty: -1

generator/config/expression/toObjectId.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ arguments:
1212
name: expression
1313
type:
1414
- expression
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/toObjectId/#example'
19+
pipeline:
20+
-
21+
$addFields:
22+
convertedId:
23+
$toObjectId: '$_id'
24+
-
25+
$sort:
26+
convertedId: -1

generator/config/expression/type.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ arguments:
1111
name: expression
1212
type:
1313
- expression
14+
tests:
15+
-
16+
name: 'Example'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/type/#example'
18+
pipeline:
19+
-
20+
$project:
21+
a:
22+
$type: '$a'

generator/js2yaml.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ <h1>Convert JS examples into Yaml</h1>
7474
return new TaggedValue('bson_binary', value);
7575
}
7676

77+
function Decimal128(value) {
78+
return new TaggedValue('bson_decimal128', value)
79+
}
80+
81+
function Int32(value) {
82+
return parseInt(value);
83+
}
84+
85+
function Int64(value) {
86+
return new TaggedValue('bson_int64', value)
87+
}
88+
7789
function convert(jsString) {
7890
try {
7991
return toYaml(eval(jsString), 1);

src/Builder/Expression/FactoryTrait.php

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

src/Builder/Expression/IsNumberOperator.php

Lines changed: 4 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\BSON\Decimal128;
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 $convert expression
17+
*/
18+
class ConvertOperatorTest extends PipelineTestCase
19+
{
20+
public function testExample(): void
21+
{
22+
$pipeline = new Pipeline(
23+
Stage::addFields(
24+
convertedPrice: Expression::convert(
25+
input: Expression::fieldPath('price'),
26+
to: 'decimal',
27+
onError: 'Error',
28+
onNull: new Decimal128('0'),
29+
),
30+
convertedQty: Expression::convert(
31+
input: Expression::fieldPath('qty'),
32+
to: 'int',
33+
onError: Expression::concat(
34+
'Could not convert ',
35+
Expression::toString(
36+
Expression::fieldPath('qty'),
37+
),
38+
' to type integer.',
39+
),
40+
onNull: 0,
41+
),
42+
),
43+
Stage::project(
44+
totalPrice: Expression::switch(
45+
branches: [
46+
object(
47+
case: Expression::eq(
48+
Expression::type(
49+
Expression::fieldPath('convertedPrice'),
50+
),
51+
'string',
52+
),
53+
then: 'NaN',
54+
),
55+
object(
56+
case: Expression::eq(
57+
Expression::type(
58+
Expression::fieldPath('convertedQty'),
59+
),
60+
'string',
61+
),
62+
then: 'NaN',
63+
),
64+
],
65+
default: Expression::multiply(
66+
Expression::fieldPath('convertedPrice'),
67+
Expression::fieldPath('convertedQty'),
68+
),
69+
),
70+
),
71+
);
72+
73+
$this->assertSamePipeline(Pipelines::ConvertExample, $pipeline);
74+
}
75+
}

0 commit comments

Comments
 (0)