summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server11118.js
blob: 1ec99024a827d89cce6994a100952869b0ab6cf1 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SERVER-11118 Tests for $dateToString

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

db = db.getSiblingDB("aggdb");

// Used to verify expected output format
function testFormat(date, formatStr, expectedStr) {
    db.dates.drop();
    db.dates.insert({date: date});

    var res = db.dates.aggregate([{$project: {
        _id: 0,
        formatted: {
            $dateToString: {
                format: formatStr,
                date: "$date"
            }
        }
    }}]).toArray();

    assert.eq(res[0].formatted, expectedStr);
}

// Used to verify that server recognizes bad formats
function testFormatError(formatObj, errCode) {
    db.dates.drop();
    db.dates.insert({tm: ISODate()});

    assertErrorCode(db.dates, {$project: {
        _id: 0,
        formatted: {
            $dateToString: formatObj
    }}}, errCode);
}

// Used to verify that only date values are accepted for date parameter
function testDateValueError(dateVal, errCode) {
  db.dates.drop();
  db.dates.insert({date: dateVal});

  assertErrorCode(db.dates, { $project:
    { formatted: { $dateToString : { format: "%Y", date: "$date" }} }
  }, errCode);
}

var now = ISODate();

// Use all modifiers we can test with js provided function
testFormat(now,
           "%%-%Y-%m-%d-%H-%M-%S-%L",
           [
               "%",
               now.getUTCFullYear().zeroPad(4),
               (now.getUTCMonth() + 1).zeroPad(2),
               now.getUTCDate().zeroPad(2),
               now.getUTCHours().zeroPad(2),
               now.getUTCMinutes().zeroPad(2),
               now.getUTCSeconds().zeroPad(2),
               now.getUTCMilliseconds().zeroPad(3)
           ].join("-"));


// Padding tests
var padme = ISODate("2001-02-03T04:05:06.007Z");

testFormat(padme, "%%", "%");
testFormat(padme, "%Y", padme.getUTCFullYear().zeroPad(4));
testFormat(padme, "%m", (padme.getUTCMonth() + 1).zeroPad(2));
testFormat(padme, "%d", padme.getUTCDate().zeroPad(2));
testFormat(padme, "%H", padme.getUTCHours().zeroPad(2));
testFormat(padme, "%M", padme.getUTCMinutes().zeroPad(2));
testFormat(padme, "%S", padme.getUTCSeconds().zeroPad(2));
testFormat(padme, "%L", padme.getUTCMilliseconds().zeroPad(3));

// no space and multiple characters between modifiers
testFormat(now,
           "%d%d***%d***%d**%d*%d",
           [
               now.getUTCDate().zeroPad(2),
               now.getUTCDate().zeroPad(2),
               "***",
               now.getUTCDate().zeroPad(2),
               "***",
               now.getUTCDate().zeroPad(2),
               "**",
               now.getUTCDate().zeroPad(2),
               "*",
               now.getUTCDate().zeroPad(2)
           ].join(""));

// JS doesn't have equivalents of these format specifiers
testFormat(ISODate('1999-01-02 03:04:05.006Z'), "%U-%w-%j", "00-7-002");

// Missing date
testFormatError({format: "%Y"}, 18628);

// Missing format
testFormatError({date: "$date"}, 18627);

// Extra field
testFormatError({format: "%Y", date: "$date", extra: "whyamIhere"}, 18534);

// Not an object
testFormatError(["%Y", "$date"], 18629);

// Use invalid modifier at middle of string
testFormatError({format:"%Y-%q", date: "$date"}, 18536);

// Odd number of percent signs at end
testFormatError({format: "%U-%w-%j-%%%", date:"$date"}, 18535);

// Odd number of percent signs at middle
// will get interpreted as an invalid modifier since it will try to use '%A'
testFormatError({format: "AAAAA%%%AAAAAA", date:"$date"}, 18536);

// Format parameter not a string
testFormatError({format: {iamalion: "roar"}, date:"$date"}, 18533);

///
/// Additional Tests
///

// Test document
var date = ISODate("1999-08-29");

testFormat(date, "%%d", "%d");

//A very long string of "%"s
var longstr = Array(1000).join("%%");
var halfstr = Array(1000).join("%");
testFormat(date, longstr, halfstr);

// Using subexpressions as format strings (eg "format: '$b'")
testFormat(date, "$b", "$b");

// Dates as null (should return a null)
testFormat(null, "%Y", null);

///
/// Using non-date fields as date parameter *should fail*
///

// Array
testDateValueError([], 16006);
testDateValueError([1, 2, 3], 16006);

// Sub-object
testDateValueError({}, 16006);
testDateValueError({a: 1}, 16006);

// String
testDateValueError("blahblahblah", 16006);

// Integer
testDateValueError(1234, 16006);

///
/// Using non-string fields as format strings
///

// Array
testFormatError({format: [], date: "$date"}, 18533);
testFormatError({format: [1, 2, 3], date: "$date"}, 18533);

// Integer
testFormatError({format: 1, date: "$date"}, 18533);

// Date
testFormatError({format: ISODate(), date: "$date"}, 18533);