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

PHPLIB-1342 Add tests on Arithmetic Expression Operators #52

Merged
merged 1 commit into from
Jan 29, 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
12 changes: 12 additions & 0 deletions generator/config/expression/abs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ arguments:
name: value
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/abs/#example'
pipeline:
-
$project:
delta:
$abs:
$subtract:
- '$startTemp'
- '$endTemp'
10 changes: 10 additions & 0 deletions generator/config/expression/ceil.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ arguments:
- resolvesToNumber
description: |
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.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ceil/#example'
pipeline:
-
$project:
value: 1
ceilingValue:
$ceil: '$value'
12 changes: 12 additions & 0 deletions generator/config/expression/divide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ arguments:
name: divisor
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/divide/#example'
pipeline:
-
$project:
city: 1
workdays:
$divide:
- '$hours'
- 8
12 changes: 12 additions & 0 deletions generator/config/expression/exp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ arguments:
name: exponent
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/exp/#example'
pipeline:
-
$project:
effectiveRate:
$subtract:
-
$exp: '$interestRate'
- 1
10 changes: 10 additions & 0 deletions generator/config/expression/floor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ arguments:
name: expression
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/floor/#example'
pipeline:
-
$project:
value: 1
floorValue:
$floor: '$value'
10 changes: 10 additions & 0 deletions generator/config/expression/ln.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ arguments:
- resolvesToNumber
description: |
Any valid expression as long as it resolves to a non-negative number. For more information on expressions, see Expressions.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/ln/#example'
pipeline:
-
$project:
x: '$year'
y:
$ln: '$sales'
15 changes: 15 additions & 0 deletions generator/config/expression/log.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ arguments:
- resolvesToNumber
description: |
Any valid expression as long as it resolves to a positive number greater than 1.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log/#example'
pipeline:
-
$project:
bitsNeeded:
$floor:
$add:
- 1
-
$log:
- '$int'
- 2
12 changes: 12 additions & 0 deletions generator/config/expression/log10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ arguments:
- resolvesToNumber
description: |
Any valid expression as long as it resolves to a non-negative number.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/#example'
pipeline:
-
$project:
pH:
$multiply:
- -1
-
$log10: '$H3O'
11 changes: 11 additions & 0 deletions generator/config/expression/mod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ arguments:
name: divisor
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mod/#example'
pipeline:
-
$project:
remainder:
$mod:
- '$hours'
- '$tasks'
13 changes: 13 additions & 0 deletions generator/config/expression/multiply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ arguments:
description: |
The arguments can be any valid expression as long as they resolve to numbers.
Starting in MongoDB 6.1 you can optimize the $multiply operation. To improve performance, group references at the end of the argument list.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply/#example'
pipeline:
-
$project:
date: 1
item: 1
total:
$multiply:
- '$price'
- '$quantity'
14 changes: 14 additions & 0 deletions generator/config/expression/pow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ arguments:
name: exponent
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/pow/#example'
pipeline:
-
$project:
variance:
$pow:
-
# The builder renders $stdDevPop with the array form, even with a single value
# $stdDevPop: '$scores.score'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked the result is the same if the value is in an array or a single value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because StdDevPopOperator encodes as a single element, which utilizes its only $expression property (a list)? I suppose $expressions would be a more descriptive name for the property but I realize the class is auto-generated and derived from the name of the variadic arg itself.

In any event, I think it makes sense to capture this explanation in a YAML comment here to explain exactly why you're changing the expected pipeline.

IIRC, this kind of thing came up in previous PRs, since there are other operators that omit logic to encode using the "short" form.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument is variadic so we always get an array. That's one reason why I made an accumulator operator distrinct from the expression operator.
Comment added.

$stdDevPop: ['$scores.score']
- 2
11 changes: 11 additions & 0 deletions generator/config/expression/round.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ arguments:
optional: true
description: |
Can be any valid expression that resolves to an integer between -20 and 100, exclusive.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/round/#example'
pipeline:
-
$project:
roundedValue:
$round:
- '$value'
- 1
24 changes: 24 additions & 0 deletions generator/config/expression/sqrt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ arguments:
- resolvesToNumber
description: |
The argument can be any valid expression as long as it resolves to a non-negative number.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/sqrt/#example'
pipeline:
-
$project:
distance:
$sqrt:
$add:
-
$pow:
-
$subtract:
- '$p2.y'
- '$p1.y'
- 2
-
$pow:
-
$subtract:
- '$p2.x'
- '$p1.x'
- 2
11 changes: 11 additions & 0 deletions generator/config/expression/trunc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ arguments:
optional: true
description: |
Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. Defaults to 0.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/#example'
pipeline:
-
$project:
truncatedValue:
$trunc:
- '$value'
- 1
32 changes: 32 additions & 0 deletions tests/Builder/Expression/AbsOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

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

/**
* Test $abs expression
*/
class AbsOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
delta: Expression::abs(
Expression::subtract(
Expression::numberFieldPath('startTemp'),
Expression::numberFieldPath('endTemp'),
),
),
),
);

$this->assertSamePipeline(Pipelines::AbsExample, $pipeline);
}
}
30 changes: 30 additions & 0 deletions tests/Builder/Expression/CeilOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

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

/**
* Test $ceil expression
*/
class CeilOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
value: 1,
ceilingValue: Expression::ceil(
Expression::numberFieldPath('value'),
),
),
);

$this->assertSamePipeline(Pipelines::CeilExample, $pipeline);
}
}
31 changes: 31 additions & 0 deletions tests/Builder/Expression/DivideOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

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

/**
* Test $divide expression
*/
class DivideOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
city: 1,
workdays: Expression::divide(
Expression::numberFieldPath('hours'),
8,
),
),
);

$this->assertSamePipeline(Pipelines::DivideExample, $pipeline);
}
}
32 changes: 32 additions & 0 deletions tests/Builder/Expression/ExpOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

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

/**
* Test $exp expression
*/
class ExpOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
effectiveRate: Expression::subtract(
Expression::exp(
Expression::numberFieldPath('interestRate'),
),
1,
),
),
);

$this->assertSamePipeline(Pipelines::ExpExample, $pipeline);
}
}
30 changes: 30 additions & 0 deletions tests/Builder/Expression/FloorOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

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

/**
* Test $floor expression
*/
class FloorOperatorTest extends PipelineTestCase
{
public function testExample(): void
{
$pipeline = new Pipeline(
Stage::project(
value: 1,
floorValue: Expression::floor(
Expression::numberFieldPath('value'),
),
),
);

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