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

PHPLIB-1341 Add tests on Miscellaneous Query Operators #53

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
18 changes: 18 additions & 0 deletions generator/config/query/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ arguments:
name: comment
type:
- string
tests:
-
name: 'Attach a Comment to an Aggregation Expression'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/comment/#attach-a-comment-to-an-aggregation-expression'
pipeline:
-
$match:
x:
$gt: 0
$comment: 'Don''t allow negative inputs.'
Copy link
Member

Choose a reason for hiding this comment

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

Noted that this syntax is specific to Single-Quoted Style. Would it make more sense to use a Double-Quoted Style and avoid escaping altogether, or did you prefer single-quoted for consistency?

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 use single quotes everywhere to avoid risky parsing.

-
$group:
_id:
$mod:
- '$x'
- 2
total:
$sum: '$x'
18 changes: 18 additions & 0 deletions generator/config/query/rand.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@ type:
encode: object
description: |
Generates a random float between 0 and 1.
tests:
-
name: 'Select Random Items From a Collection'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/rand/#select-random-items-from-a-collection'
pipeline:
-
$match:
district: 3
$expr:
$lt:
- 0.5
-
$rand: {}
-
$project:
_id: 0
name: 1
registered: 1
2 changes: 1 addition & 1 deletion generator/js2yaml.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ <h1>Convert JS examples into Yaml</h1>
case 'boolean':
return object ? ' true' : ' false';
case 'string':
return " '" + object.replace(/'/g, "\\'") + "'";
return " '" + object.replace(/'/g, "''") + "'";
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 necessary for the single-quoted style being used above?

case 'number':
return ' ' + object.toString();
case 'object':
Expand Down
39 changes: 39 additions & 0 deletions tests/Builder/Query/CommentOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Query;

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

/**
* Test $comment query
*/
class CommentOperatorTest extends PipelineTestCase
{
public function testAttachACommentToAnAggregationExpression(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::comment('Don\'t allow negative inputs.'),
x: Query::gt(0),
Copy link
Member Author

@GromNaN GromNaN Jan 24, 2024

Choose a reason for hiding this comment

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

Named arguments must be after positional arguments, even when we use variadic.

),
Stage::group(
_id: Expression::mod(
Expression::numberFieldPath('x'),
2,
),
total: Accumulator::sum(
Expression::numberFieldPath('x'),
),
),
);

$this->assertSamePipeline(Pipelines::CommentAttachACommentToAnAggregationExpression, $pipeline);
}
}
75 changes: 75 additions & 0 deletions tests/Builder/Query/Pipelines.php

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

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

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Query;

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

/**
* Test $rand query
*/
class RandOperatorTest extends PipelineTestCase
{
public function testSelectRandomItemsFromACollection(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::expr(
Expression::lt(
0.5,
Expression::rand(),
),
),
district: 3,
),
Stage::project(
_id: 0,
name: 1,
registered: 1,
),
);

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