summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/ts_second_increment.js
blob: 038c58ec3ed091d9408e34ad35df5e87c21d92a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Validates the correctness of the '$tsSecond' and the '$tsIncrement' expression in the aggregation
// pipeline.

load("jstests/libs/sbe_assert_error_override.js");  // Override error-code-checking APIs.

(function() {
"use strict";

var testDB = db.getSiblingDB("expression_ts_second_increment");

assert.commandWorked(testDB.dropDatabase());

var coll = testDB.getCollection("test");

assert.commandWorked(
    coll.insert({_id: 0, bsonTime: Timestamp(1622731060, 10), invalidBsonTime: 1622731060}));

(function testtsSecond() {
    // Projects the seconds component of the bson timestamp using the 'tsSecond' on the 'bsonTime'
    // field and verifies that the value is correct.
    let result =
        coll.aggregate([{$project: {bsonTime: 1, bsonTimeSeconds: {$tsSecond: "$bsonTime"}}}])
            .toArray();
    assert.eq(result.length, 1);
    assert.eq(result[0].bsonTimeSeconds, result[0].bsonTime.getTime(), result);

    // Passes a non existing field path to the '$tsSecond' and verifies a null timestamp is
    // returned.
    result = coll.aggregate(
                     [{$project: {bsonTime: 1, bsonTimeSeconds: {$tsSecond: "$nonExistingField"}}}])
                 .toArray();
    assert.eq(result.length, 1);
    assert.eq(result[0].bsonTimeSeconds, null, result);

    // Projects the seconds component of bson timestamp using the 'tsSecond' on the
    // 'invalidBsonTime' field and verifies that an expected error code is thrown.
    const nonTimestampError = assert.throws(
        () => coll.aggregate(
                      [{$project: {bsonTime: 1, bsonTimeSeconds: {$tsSecond: "$invalidBsonTime"}}}])
                  .toArray());
    assert.commandFailedWithCode(nonTimestampError, 5687301);
})();

(function testtsIncrement() {
    // Projects the increment component of bson timestamp using the 'tsSecond' on the 'bsonTime'
    // field and verifies that the value is correct.
    let result =
        coll.aggregate([{$project: {bsonTime: 1, bsonTimeIncrements: {$tsIncrement: "$bsonTime"}}}])
            .toArray();
    assert.eq(result.length, 1);
    assert.eq(result[0].bsonTimeIncrements, result[0].bsonTime.getInc(), result);

    // Passes a non existing field path to the '$tsIncrement' and verifies a null timestamp is
    // returned.
    result =
        coll.aggregate(
                [{$project: {bsonTime: 1, bsonTimeSeconds: {$tsIncrement: "$nonExistingField"}}}])
            .toArray();
    assert.eq(result.length, 1);
    assert.eq(result[0].bsonTimeSeconds, null, result);

    // Projects the increment component of bson timestamp using the 'tsIncrement' on the
    // 'invalidBsonTime' field and verifies that an expected error code is thrown.
    const nonTimestampError = assert.throws(
        () =>
            coll.aggregate([{
                    $project: {bsonTime: 1, bsonTimeIncrements: {$tsIncrement: "$invalidBsonTime"}}
                }])
                .toArray());
    assert.commandFailedWithCode(nonTimestampError, 5687302);
})();
})();