Skip to content

Commit a99f472

Browse files
authored
PHPLIB-1342 Add tests on Arithmetic Expression Operators (mongodb#52)
1 parent 397ff5d commit a99f472

29 files changed

+973
-0
lines changed

generator/config/expression/abs.yaml

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

generator/config/expression/ceil.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ arguments:
1313
- resolvesToNumber
1414
description: |
1515
If the argument resolves to a value of null or refers to a field that is missing, $ceil returns null. If the argument resolves to NaN, $ceil returns NaN.
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ceil/#example'
20+
pipeline:
21+
-
22+
$project:
23+
value: 1
24+
ceilingValue:
25+
$ceil: '$value'

generator/config/expression/divide.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ arguments:
1717
name: divisor
1818
type:
1919
- resolvesToNumber
20+
tests:
21+
-
22+
name: 'Example'
23+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/divide/#example'
24+
pipeline:
25+
-
26+
$project:
27+
city: 1
28+
workdays:
29+
$divide:
30+
- '$hours'
31+
- 8

generator/config/expression/exp.yaml

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

generator/config/expression/floor.yaml

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

generator/config/expression/ln.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ arguments:
1414
- resolvesToNumber
1515
description: |
1616
Any valid expression as long as it resolves to a non-negative number. For more information on expressions, see Expressions.
17+
tests:
18+
-
19+
name: 'Example'
20+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ln/#example'
21+
pipeline:
22+
-
23+
$project:
24+
x: '$year'
25+
y:
26+
$ln: '$sales'

generator/config/expression/log.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ arguments:
1919
- resolvesToNumber
2020
description: |
2121
Any valid expression as long as it resolves to a positive number greater than 1.
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log/#example'
26+
pipeline:
27+
-
28+
$project:
29+
bitsNeeded:
30+
$floor:
31+
$add:
32+
- 1
33+
-
34+
$log:
35+
- '$int'
36+
- 2

generator/config/expression/log10.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ arguments:
1313
- resolvesToNumber
1414
description: |
1515
Any valid expression as long as it resolves to a non-negative number.
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/#example'
20+
pipeline:
21+
-
22+
$project:
23+
pH:
24+
$multiply:
25+
- -1
26+
-
27+
$log10: '$H3O'

generator/config/expression/mod.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ arguments:
1717
name: divisor
1818
type:
1919
- resolvesToNumber
20+
tests:
21+
-
22+
name: 'Example'
23+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mod/#example'
24+
pipeline:
25+
-
26+
$project:
27+
remainder:
28+
$mod:
29+
- '$hours'
30+
- '$tasks'

generator/config/expression/multiply.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ arguments:
1515
description: |
1616
The arguments can be any valid expression as long as they resolve to numbers.
1717
Starting in MongoDB 6.1 you can optimize the $multiply operation. To improve performance, group references at the end of the argument list.
18+
tests:
19+
-
20+
name: 'Example'
21+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply/#example'
22+
pipeline:
23+
-
24+
$project:
25+
date: 1
26+
item: 1
27+
total:
28+
$multiply:
29+
- '$price'
30+
- '$quantity'

generator/config/expression/pow.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ arguments:
1515
name: exponent
1616
type:
1717
- resolvesToNumber
18+
tests:
19+
-
20+
name: 'Example'
21+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/pow/#example'
22+
pipeline:
23+
-
24+
$project:
25+
variance:
26+
$pow:
27+
-
28+
# The builder renders $stdDevPop with the array form, even with a single value
29+
# $stdDevPop: '$scores.score'
30+
$stdDevPop: ['$scores.score']
31+
- 2

generator/config/expression/round.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ arguments:
2727
optional: true
2828
description: |
2929
Can be any valid expression that resolves to an integer between -20 and 100, exclusive.
30+
tests:
31+
-
32+
name: 'Example'
33+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/round/#example'
34+
pipeline:
35+
-
36+
$project:
37+
roundedValue:
38+
$round:
39+
- '$value'
40+
- 1

generator/config/expression/sqrt.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,27 @@ arguments:
1313
- resolvesToNumber
1414
description: |
1515
The argument can be any valid expression as long as it resolves to a non-negative number.
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sqrt/#example'
20+
pipeline:
21+
-
22+
$project:
23+
distance:
24+
$sqrt:
25+
$add:
26+
-
27+
$pow:
28+
-
29+
$subtract:
30+
- '$p2.y'
31+
- '$p1.y'
32+
- 2
33+
-
34+
$pow:
35+
-
36+
$subtract:
37+
- '$p2.x'
38+
- '$p1.x'
39+
- 2

generator/config/expression/trunc.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ arguments:
2121
optional: true
2222
description: |
2323
Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. Defaults to 0.
24+
tests:
25+
-
26+
name: 'Example'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/#example'
28+
pipeline:
29+
-
30+
$project:
31+
truncatedValue:
32+
$trunc:
33+
- '$value'
34+
- 1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 $abs expression
14+
*/
15+
class AbsOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
delta: Expression::abs(
22+
Expression::subtract(
23+
Expression::numberFieldPath('startTemp'),
24+
Expression::numberFieldPath('endTemp'),
25+
),
26+
),
27+
),
28+
);
29+
30+
$this->assertSamePipeline(Pipelines::AbsExample, $pipeline);
31+
}
32+
}
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 $ceil expression
14+
*/
15+
class CeilOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
value: 1,
22+
ceilingValue: Expression::ceil(
23+
Expression::numberFieldPath('value'),
24+
),
25+
),
26+
);
27+
28+
$this->assertSamePipeline(Pipelines::CeilExample, $pipeline);
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 $divide expression
14+
*/
15+
class DivideOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
city: 1,
22+
workdays: Expression::divide(
23+
Expression::numberFieldPath('hours'),
24+
8,
25+
),
26+
),
27+
);
28+
29+
$this->assertSamePipeline(Pipelines::DivideExample, $pipeline);
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 $exp expression
14+
*/
15+
class ExpOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
effectiveRate: Expression::subtract(
22+
Expression::exp(
23+
Expression::numberFieldPath('interestRate'),
24+
),
25+
1,
26+
),
27+
),
28+
);
29+
30+
$this->assertSamePipeline(Pipelines::ExpExample, $pipeline);
31+
}
32+
}
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 $floor expression
14+
*/
15+
class FloorOperatorTest extends PipelineTestCase
16+
{
17+
public function testExample(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
value: 1,
22+
floorValue: Expression::floor(
23+
Expression::numberFieldPath('value'),
24+
),
25+
),
26+
);
27+
28+
$this->assertSamePipeline(Pipelines::FloorExample, $pipeline);
29+
}
30+
}

0 commit comments

Comments
 (0)