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

Commit 040833d

Browse files
authored
Add tests for $search and $searchMeta stages (#61)
1 parent 592b0a0 commit 040833d

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

generator/config/stage/search.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,30 @@ arguments:
1212
name: search
1313
type:
1414
- object
15+
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#aggregation-variable'
20+
pipeline:
21+
-
22+
$search:
23+
near:
24+
path: 'released'
25+
origin: !bson_utcdatetime '2011-09-01T00:00:00.000+00:00'
26+
pivot: 7776000000
27+
-
28+
$project:
29+
_id: 0
30+
title: 1
31+
released: 1
32+
-
33+
$limit: 5
34+
-
35+
$facet:
36+
docs: []
37+
meta:
38+
-
39+
$replaceWith: '$$SEARCH_META'
40+
-
41+
$limit: 1

generator/config/stage/searchMeta.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ arguments:
1212
name: meta
1313
type:
1414
- object
15+
16+
tests:
17+
-
18+
name: 'Example'
19+
link: 'https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#example'
20+
pipeline:
21+
-
22+
$searchMeta:
23+
range:
24+
path: 'year'
25+
gte: 1998
26+
lt: 1999
27+
count:
28+
type: 'total'

tests/Builder/Stage/Pipelines.php

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Stage;
6+
7+
use MongoDB\Builder\Pipeline;
8+
use MongoDB\Builder\Stage;
9+
use MongoDB\Tests\Builder\PipelineTestCase;
10+
11+
use function MongoDB\object;
12+
13+
/**
14+
* Test $searchMeta stage
15+
*/
16+
class SearchMetaStageTest extends PipelineTestCase
17+
{
18+
public function testExample(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::searchMeta(object(
22+
range: object(
23+
path: 'year',
24+
gte: 1998,
25+
lt: 1999,
26+
),
27+
count: object(type: 'total'),
28+
)),
29+
);
30+
31+
$this->assertSamePipeline(Pipelines::SearchMetaExample, $pipeline);
32+
}
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Stage;
6+
7+
use DateTime;
8+
use MongoDB\BSON\UTCDateTime;
9+
use MongoDB\Builder\Expression;
10+
use MongoDB\Builder\Pipeline;
11+
use MongoDB\Builder\Stage;
12+
use MongoDB\Tests\Builder\PipelineTestCase;
13+
14+
use function MongoDB\object;
15+
16+
/**
17+
* Test $search stage
18+
*/
19+
class SearchStageTest extends PipelineTestCase
20+
{
21+
public function testExample(): void
22+
{
23+
$pipeline = new Pipeline(
24+
Stage::search(object(
25+
near: object(
26+
path: 'released',
27+
origin: new UTCDateTime(new DateTime('2011-09-01T00:00:00.000+00:00')),
28+
pivot: 7776000000,
29+
),
30+
)),
31+
Stage::project(_id: 0, title: 1, released: 1),
32+
Stage::limit(5),
33+
Stage::facet(
34+
docs: [],
35+
meta: new Pipeline(
36+
Stage::replaceWith(Expression::variable('SEARCH_META')),
37+
Stage::limit(1),
38+
),
39+
),
40+
);
41+
42+
$this->assertSamePipeline(Pipelines::SearchExample, $pipeline);
43+
}
44+
}

0 commit comments

Comments
 (0)