summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server6121.js
blob: b7ea33abdaad1376efa836a57256d450811f4277 (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
73
74
75
76
77
78
/*
 * SERVER-6121: aggregation framework converts Timestamp to long long in result set
 *
 * This test validates the SERVER-6121 ticket. Add support for timestamps to Aggregation and
 * ensure they can do everything dates can. Previously timestamps were awkwardly used as dates
 * and long longs.
 */

/*
 * 1) Clear and create testing db
 * 2) Run an aggregation with all date expressions on a timestamp and a date
 * 3) Run an aggregation that will show timestamp and date can be compared
 * 4) Run an aggregation comparing two timestamps to show inc matters
 */

// load test utilities
load('jstests/aggregation/extras/utils.js');

// Clear db
db.s6121.drop();
// Populate db
db.s6121.save({date: new Timestamp(1341337661, 1)});
db.s6121.save({date: new Date(1341337661000)});
// Aggregate checking various combinations of the constant and the field
var s6121 = db.s6121
                .aggregate({
                    $project: {
                        _id: 0,
                        dayOfMonth: {$dayOfMonth: '$date'},
                        dayOfWeek: {$dayOfWeek: '$date'},
                        dayOfYear: {$dayOfYear: '$date'},
                        hour: {$hour: '$date'},
                        minute: {$minute: '$date'},
                        month: {$month: '$date'},
                        second: {$second: '$date'},
                        week: {$week: '$date'},
                        year: {$year: '$date'}
                    }
                })
                .toArray();
// Assert the two entries are equal
assert.eq(s6121[0], s6121[1], 's6121 failed');

// Clear db for timestamp to date compare test
// For historical reasons the compare the same if they are the same 64-bit representation.
// That means that the Timestamp has an "inc" that is the same as the Date has millis.
db.s6121.drop();
db.s6121.save({time: new Timestamp(0, 1234), date: new Date(1234)});
db.s6121.save({time: new Timestamp(1, 1234), date: new Date(1234)});
printjson(db.s6121.find().toArray());
var s6121 = db.s6121.aggregate({
    $project: {
        _id: 0,
        // comparison is different code path based on order (same as in bson)
        ts_date: {$eq: ['$time', '$date']},
        date_ts: {$eq: ['$date', '$time']}
    }
});
assert.eq(s6121.toArray(), [{ts_date: false, date_ts: false}, {ts_date: false, date_ts: false}]);

// Clear db for timestamp comparison tests
db.s6121.drop();
db.s6121.save({time: new Timestamp(1341337661, 1), time2: new Timestamp(1341337661, 2)});
var s6121 = db.s6121.aggregate({
    $project: {
        _id: 0,
        cmp: {$cmp: ['$time', '$time2']},
        eq: {$eq: ['$time', '$time2']},
        gt: {$gt: ['$time', '$time2']},
        gte: {$gte: ['$time', '$time2']},
        lt: {$lt: ['$time', '$time2']},
        lte: {$lte: ['$time', '$time2']},
        ne: {$ne: ['$time', '$time2']}
    }
});
var s6121result = [{cmp: -1, eq: false, gt: false, gte: false, lt: true, lte: true, ne: true}];
// Assert the results are as expected
assert.eq(s6121.toArray(), s6121result, 's6121 failed comparing two timestamps');