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

Commit 7315073

Browse files
authored
PHPLIB-1356 Add tests on Timestamp Expression Operators (#47)
1 parent d0f0565 commit 7315073

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed

generator/config/expression/tsIncrement.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,28 @@ arguments:
1212
name: expression
1313
type:
1414
- resolvesToTimestamp
15+
tests:
16+
-
17+
name: 'Obtain the Incrementing Ordinal from a Timestamp Field'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/tsIncrement/#obtain-the-incrementing-ordinal-from-a-timestamp-field'
19+
pipeline:
20+
-
21+
$project:
22+
_id: 0
23+
saleTimestamp: 1
24+
saleIncrement:
25+
$tsIncrement: '$saleTimestamp'
26+
-
27+
name: 'Use $tsSecond in a Change Stream Cursor to Monitor Collection Changes'
28+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/tsSecond/#use--tssecond-in-a-change-stream-cursor-to-monitor-collection-changes'
29+
pipeline:
30+
-
31+
$match:
32+
$expr:
33+
$eq:
34+
-
35+
$mod:
36+
-
37+
$tsIncrement: '$clusterTime'
38+
- 2
39+
- 0

generator/config/expression/tsSecond.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,22 @@ arguments:
1212
name: expression
1313
type:
1414
- resolvesToTimestamp
15+
tests:
16+
-
17+
name: 'Obtain the Number of Seconds from a Timestamp Field'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/tsSecond/#obtain-the-number-of-seconds-from-a-timestamp-field'
19+
pipeline:
20+
-
21+
$project:
22+
_id: 0
23+
saleTimestamp: 1
24+
saleSeconds:
25+
$tsSecond: '$saleTimestamp'
26+
-
27+
name: 'Use $tsSecond in a Change Stream Cursor to Monitor Collection Changes'
28+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/tsSecond/#use--tssecond-in-a-change-stream-cursor-to-monitor-collection-changes'
29+
pipeline:
30+
-
31+
$addFields:
32+
clusterTimeSeconds:
33+
$tsSecond: '$clusterTime'

tests/Builder/Expression/Pipelines.php

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\Builder\Expression;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Query;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $tsIncrement expression
15+
*/
16+
class TsIncrementOperatorTest extends PipelineTestCase
17+
{
18+
public function testObtainTheIncrementingOrdinalFromATimestampField(): void
19+
{
20+
$pipeline = new Pipeline(
21+
Stage::project(
22+
_id: 0,
23+
saleTimestamp: 1,
24+
saleIncrement: Expression::tsIncrement(
25+
Expression::timestampFieldPath('saleTimestamp'),
26+
),
27+
),
28+
);
29+
30+
$this->assertSamePipeline(Pipelines::TsIncrementObtainTheIncrementingOrdinalFromATimestampField, $pipeline);
31+
}
32+
33+
public function testUseTsSecondInAChangeStreamCursorToMonitorCollectionChanges(): void
34+
{
35+
$pipeline = new Pipeline(
36+
Stage::match(
37+
Query::expr(
38+
Expression::eq(
39+
Expression::mod(
40+
Expression::tsIncrement(
41+
Expression::timestampFieldPath('clusterTime'),
42+
),
43+
2,
44+
),
45+
0,
46+
),
47+
),
48+
),
49+
);
50+
51+
$this->assertSamePipeline(Pipelines::TsIncrementUseTsSecondInAChangeStreamCursorToMonitorCollectionChanges, $pipeline);
52+
}
53+
}
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\Expression;
6+
7+
use MongoDB\Builder\Expression;
8+
use MongoDB\Builder\Pipeline;
9+
use MongoDB\Builder\Stage;
10+
use MongoDB\Tests\Builder\PipelineTestCase;
11+
12+
/**
13+
* Test $tsSecond expression
14+
*/
15+
class TsSecondOperatorTest extends PipelineTestCase
16+
{
17+
public function testObtainTheNumberOfSecondsFromATimestampField(): void
18+
{
19+
$pipeline = new Pipeline(
20+
Stage::project(
21+
_id: 0,
22+
saleTimestamp: 1,
23+
saleSeconds: Expression::tsSecond(
24+
Expression::timestampFieldPath('saleTimestamp'),
25+
),
26+
),
27+
);
28+
29+
$this->assertSamePipeline(Pipelines::TsSecondObtainTheNumberOfSecondsFromATimestampField, $pipeline);
30+
}
31+
32+
public function testUseTsSecondInAChangeStreamCursorToMonitorCollectionChanges(): void
33+
{
34+
$pipeline = new Pipeline(
35+
Stage::addFields(
36+
clusterTimeSeconds: Expression::tsSecond(
37+
Expression::timestampFieldPath('clusterTime'),
38+
),
39+
),
40+
);
41+
42+
$this->assertSamePipeline(Pipelines::TsSecondUseTsSecondInAChangeStreamCursorToMonitorCollectionChanges, $pipeline);
43+
}
44+
}

0 commit comments

Comments
 (0)