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

PHPLIB-1333 Add tests on Comparison Query Operators #20

Merged
merged 1 commit into from
Jan 11, 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
52 changes: 52 additions & 0 deletions generator/config/query/eq.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,55 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Equals a Specified Value'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#equals-a-specified-value'
pipeline:
-
$match:
qty:
$eq: 20

-
name: 'Field in Embedded Document Equals a Value'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#field-in-embedded-document-equals-a-value'
pipeline:
-
$match:
'item.name':
$eq: 'ab'

-
name: 'Equals an Array Value'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#equals-an-array-value'
pipeline:
-
$match:
tags:
$eq: ['A', 'B']

-
name: 'Regex Match Behaviour'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/eq/#regex-match-behaviour'
pipeline:
-
$match:
company: 'MongoDB'
-
$match:
company:
$eq: 'MongoDB'
-
$match:
company:
$regularExpression:
options: ''
pattern: '^MongoDB'
-
$match:
company:
$eq:
$regularExpression:
options: ''
pattern: '^MongoDB'
9 changes: 9 additions & 0 deletions generator/config/query/gt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Match Document Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/gt/#match-document-fields'
pipeline:
-
$match:
qty:
$gt: 20
9 changes: 9 additions & 0 deletions generator/config/query/gte.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Match Document Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/gte/#match-document-fields'
pipeline:
-
$match:
qty:
$gte: 20
25 changes: 25 additions & 0 deletions generator/config/query/in.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ arguments:
name: value
type:
- array # of expression
tests:
-
name: 'Use the $in Operator to Match Values in an Array'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/in/#use-the--in-operator-to-match-values'
pipeline:
-
$match:
tags:
$in: ['home', 'school']
-
name: 'Use the $in Operator with a Regular Expression'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/in/#use-the--in-operator-with-a-regular-expression'
pipeline:
-
$match:
tags:
$in:
-
$regularExpression:
options: ''
pattern: '^be'
-
$regularExpression:
options: ''
pattern: '^st'
9 changes: 9 additions & 0 deletions generator/config/query/lt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Match Document Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/lt/#match-document-fields'
pipeline:
-
$match:
qty:
$lt: 20
9 changes: 9 additions & 0 deletions generator/config/query/lte.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Match Document Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/lte/#match-document-fields'
pipeline:
-
$match:
qty:
$lte: 20
9 changes: 9 additions & 0 deletions generator/config/query/ne.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ arguments:
name: value
type:
- any
tests:
-
name: 'Match Document Fields'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/ne/#match-document-fields'
pipeline:
-
$match:
quantity:
$ne: 20
17 changes: 17 additions & 0 deletions generator/config/query/nin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,20 @@ arguments:
name: value
type:
- array # of expression
tests:
-
name: 'Select on Unmatching Documents'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nin/#select-on-unmatching-documents'
pipeline:
-
$match:
quantity:
$nin: [5, 15]
-
name: 'Select on Elements Not in an Array'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nin/#select-on-elements-not-in-an-array'
pipeline:
-
$match:
tags:
$nin: ['school']
70 changes: 70 additions & 0 deletions tests/Builder/Query/EqOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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 $eq query
*/
class EqOperatorTest extends PipelineTestCase
{
public function testEqualsASpecifiedValue(): void
{
$pipeline = new Pipeline(
Stage::match(
qty: Query::eq(20),
),
);

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

public function testEqualsAnArrayValue(): void
{
$pipeline = new Pipeline(
Stage::match(
tags: Query::eq(['A', 'B']),
),
);

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

public function testFieldInEmbeddedDocumentEqualsAValue(): void
{
$pipeline = new Pipeline(
Stage::match(
...['item.name' => Query::eq('ab')],
),
);

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

public function testRegexMatchBehaviour(): void
{
$pipeline = new Pipeline(
Stage::match(
company: 'MongoDB',
),
Stage::match(
company: Query::eq('MongoDB'),
),
Stage::match(
company: new Regex('^MongoDB'),
),
Stage::match(
company: Query::eq(new Regex('^MongoDB')),
),
);

$this->assertSamePipeline(Pipelines::EqRegexMatchBehaviour, $pipeline);
}
}
27 changes: 27 additions & 0 deletions tests/Builder/Query/GtOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 $gt query
*/
class GtOperatorTest extends PipelineTestCase
{
public function testMatchDocumentFields(): void
{
$pipeline = new Pipeline(
Stage::match(
qty: Query::gt(20),
),
);

$this->assertSamePipeline(Pipelines::GtMatchDocumentFields, $pipeline);
}
}
27 changes: 27 additions & 0 deletions tests/Builder/Query/GteOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 $gte query
*/
class GteOperatorTest extends PipelineTestCase
{
public function testMatchDocumentFields(): void
{
$pipeline = new Pipeline(
Stage::match(
qty: Query::gte(20),
),
);

$this->assertSamePipeline(Pipelines::GteMatchDocumentFields, $pipeline);
}
}
39 changes: 39 additions & 0 deletions tests/Builder/Query/InOperatorTest.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\BSON\Regex;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
* Test $in query
*/
class InOperatorTest extends PipelineTestCase
{
public function testUseTheInOperatorToMatchValuesInAnArray(): void
{
$pipeline = new Pipeline(
Stage::match(
tags: Query::in(['home', 'school']),
),
);

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

public function testUseTheInOperatorWithARegularExpression(): void
{
$pipeline = new Pipeline(
Stage::match(
tags: Query::in([new Regex('^be'), new Regex('^st')]),
),
);

$this->assertSamePipeline(Pipelines::InUseTheInOperatorWithARegularExpression, $pipeline);
}
}
27 changes: 27 additions & 0 deletions tests/Builder/Query/LtOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 $lt query
*/
class LtOperatorTest extends PipelineTestCase
{
public function testMatchDocumentFields(): void
{
$pipeline = new Pipeline(
Stage::match(
qty: Query::lt(20),
),
);

$this->assertSamePipeline(Pipelines::LtMatchDocumentFields, $pipeline);
}
}
27 changes: 27 additions & 0 deletions tests/Builder/Query/LteOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 $lte query
*/
class LteOperatorTest extends PipelineTestCase
{
public function testMatchDocumentFields(): void
{
$pipeline = new Pipeline(
Stage::match(
qty: Query::lte(20),
),
);

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