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

PHPLIB-1334 Add tests on Logical Query Operators #21

Merged
merged 1 commit into from
Jan 12, 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
36 changes: 36 additions & 0 deletions generator/config/query/and.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,39 @@ arguments:
- query
variadic: array
variadicMin: 1
tests:
-
name: 'AND Queries With Multiple Expressions Specifying the Same Field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-field'
pipeline:
-
$match:
$and:
-
price:
$ne: 1.99
-
price:
$exists: true
-
name: 'AND Queries With Multiple Expressions Specifying the Same Operator'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator'
pipeline:
-
$match:
$and:
-
$or:
-
qty:
$lt: 10
-
qty:
$gt: 50
-
$or:
-
sale: true
-
price:
$lt: 5
43 changes: 43 additions & 0 deletions generator/config/query/nor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,46 @@ arguments:
- query
variadic: array
variadicMin: 1
tests:
-
name: 'Query with Two Expressions'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-query-with-two-expressions'
pipeline:
-
$match:
$nor:
-
price: 1.99
-
sale: true
-
name: 'Additional Comparisons'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-and-additional-comparisons'
pipeline:
-
$match:
$nor:
-
price: 1.99
-
qty:
$lt: 20
-
sale: true
-
name: '$nor and $exists'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nor/#-nor-and--exists'
pipeline:
-
$match:
$nor:
-
price: 1.99
-
price:
$exists: false
-
sale: true
-
sale:
$exists: false
21 changes: 21 additions & 0 deletions generator/config/query/not.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,24 @@ arguments:
name: expression
type:
- fieldQuery
tests:
-
name: 'Syntax'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/not/#syntax'
pipeline:
-
$match:
price:
$not:
$gt: 1.99
-
name: 'Regular Expressions'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/not/#-not-and-regular-expressions'
pipeline:
-
$match:
price:
$not:
$regularExpression:
pattern: '^p.*'
options: ''
32 changes: 32 additions & 0 deletions generator/config/query/or.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,35 @@ arguments:
- query
variadic: array
variadicMin: 1
tests:
-
name: '$or Clauses'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/or/#-or-clauses-and-indexes'
pipeline:
-
$match:
$or:
-
quantity:
$lt: 20
-
price: 10

-
name: 'Error Handling'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/or/#error-handling'
pipeline:
-
$match:
$or:
-
x:
$eq: 0
-
$expr:
$eq:
-
$divide:
- 1
- '$x'
- 3
62 changes: 62 additions & 0 deletions tests/Builder/Query/AndOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Query;

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

/**
* Test $and query
*/
class AndOperatorTest extends PipelineTestCase
{
public function testANDQueriesWithMultipleExpressionsSpecifyingTheSameField(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::and(
Query::query(
price: Query::ne(1.99),
),
Query::query(
price: Query::exists(true),
),
),
),
);

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

public function testANDQueriesWithMultipleExpressionsSpecifyingTheSameOperator(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::and(
Query::or(
Query::query(
qty: Query::lt(10),
),
Query::query(
qty: Query::gt(50),
),
),
Query::or(
Query::query(
sale: true,
),
Query::query(
price: Query::lt(5),
),
),
),
),
);

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

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Query;

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

/**
* Test $nor query
*/
class NorOperatorTest extends PipelineTestCase
{
public function testAdditionalComparisons(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::nor(
Query::query(
price: 1.99,
),
Query::query(
qty: Query::lt(20),
),
Query::query(
sale: true,
),
),
),
);

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

public function testNorAndExists(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::nor(
Query::query(
price: 1.99,
),
Query::query(
price: Query::exists(false),
),
Query::query(
sale: true,
),
Query::query(
sale: Query::exists(false),
),
),
),
);

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

public function testQueryWithTwoExpressions(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::nor(
Query::query(
price: 1.99,
),
Query::query(
sale: true,
),
),
),
);

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

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Query;

use MongoDB\BSON\Regex;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $not query
*/
class NotOperatorTest extends PipelineTestCase
{
public function testRegularExpressions(): void
{
$pipeline = new Pipeline(
Stage::match(
price: Query::not(
new Regex('^p.*'),
),
),
);

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

public function testSyntax(): void
{
$pipeline = new Pipeline(
Stage::match(
price: Query::not(
Query::gt(1.99),
),
),
);

$this->assertSamePipeline(Pipelines::NotSyntax, $pipeline);
}
}
59 changes: 59 additions & 0 deletions tests/Builder/Query/OrOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 $or query
*/
class OrOperatorTest extends PipelineTestCase
{
public function testErrorHandling(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::or(
Query::query(
x: Query::eq(0),
),
Query::expr(
Expression::eq(
Expression::divide(
1,
Expression::intFieldPath('x'),
),
3,
),
),
),
),
);

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

public function testOrClauses(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::or(
Query::query(
quantity: Query::lt(20),
),
Query::query(
price: 10,
),
),
),
);

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