summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server6190.js
blob: d32a652e74b0b34d6c8bf737570b8aca25e81add (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
// $week returns a date's week of the year.  Week zero is comprised of any dates before the first
// Sunday of the year.  SERVER-6190
load('jstests/aggregation/extras/utils.js');

t = db.jstests_aggregation_server6190;
t.drop();

t.save({});

function week(date) {
    return t.aggregate({$project: {a: {$week: date}}},
                       {$match: {a: {$type: 16 /* Int type expected */}}})
        .toArray()[0]
        .a;
}

function assertWeek(expectedWeek, date) {
    assert.eq(expectedWeek, week(date));
}

// Sun Jan 1 1984
assertWeek(1, new Date(Date.UTC(1984, 0, 1)));
// Mon Jan 2 1984
assertWeek(1, new Date(Date.UTC(1984, 0, 2)));
// Sat Jan 7 1984
assertWeek(1, new Date(Date.UTC(1984, 0, 7)));
// Sun Jan 8 1984
assertWeek(2, new Date(Date.UTC(1984, 0, 8)));
// Sat Feb 18 1984
assertWeek(7, new Date(Date.UTC(1984, 1, 18)));
// Sun Feb 19 1984
assertWeek(8, new Date(Date.UTC(1984, 1, 19)));

// Mon Jan 1 2007
assertWeek(0, new Date(Date.UTC(2007, 0, 1)));
// Tue Jan 2 2007
assertWeek(0, new Date(Date.UTC(2007, 0, 2)));
// Sat Jan 6 2007
assertWeek(0, new Date(Date.UTC(2007, 0, 6)));
// Sun Jan 7 2007
assertWeek(1, new Date(Date.UTC(2007, 0, 7)));
// Mon Jan 8 2007
assertWeek(1, new Date(Date.UTC(2007, 0, 8)));
// Sat Jan 13 2007
assertWeek(1, new Date(Date.UTC(2007, 0, 13)));
// Sun Jan 14 2007
assertWeek(2, new Date(Date.UTC(2007, 0, 14)));
// Sat Mar 3 2007
assertWeek(8, new Date(Date.UTC(2007, 2, 3)));
// Sun Mar 4 2007
assertWeek(9, new Date(Date.UTC(2007, 2, 4)));

// Tue Jan 1 2008
assertWeek(0, new Date(Date.UTC(2008, 0, 1)));
// Sat Jan 5 2008
assertWeek(0, new Date(Date.UTC(2008, 0, 5)));
// Sun Jan 6 2008
assertWeek(1, new Date(Date.UTC(2008, 0, 6)));
// Sat Apr 26 2008
assertWeek(16, new Date(Date.UTC(2008, 3, 26)));
// Sun Apr 27 2008
assertWeek(17, new Date(Date.UTC(2008, 3, 27)));

// Wed Jan 1 2003
assertWeek(0, new Date(Date.UTC(2003, 0, 1)));
// Sat Jan 4 2003
assertWeek(0, new Date(Date.UTC(2003, 0, 4)));
// Sun Jan 5 2003
assertWeek(1, new Date(Date.UTC(2003, 0, 5)));
// Sat Dec 27 2003
assertWeek(51, new Date(Date.UTC(2003, 11, 27)));
// Sat Dec 28 2003
assertWeek(52, new Date(Date.UTC(2003, 11, 28)));

// Thu Jan 1 2009
assertWeek(0, new Date(Date.UTC(2009, 0, 1)));
// Sat Jan 3 2009
assertWeek(0, new Date(Date.UTC(2009, 0, 3)));
// Sun Jan 4 2008
assertWeek(1, new Date(Date.UTC(2009, 0, 4)));
// Sat Oct 31 2009
assertWeek(43, new Date(Date.UTC(2009, 9, 31)));
// Sun Nov 1 2008
assertWeek(44, new Date(Date.UTC(2009, 10, 1)));

// Fri Jan 1 2010
assertWeek(0, new Date(Date.UTC(2010, 0, 1)));
// Sat Jan 2 2010
assertWeek(0, new Date(Date.UTC(2010, 0, 2)));
// Sun Jan 3 2010
assertWeek(1, new Date(Date.UTC(2010, 0, 3)));
// Sat Sept 18 2010
assertWeek(37, new Date(Date.UTC(2010, 8, 18)));
// Sun Sept 19 2010
assertWeek(38, new Date(Date.UTC(2010, 8, 19)));

// Sat Jan 1 2011
assertWeek(0, new Date(Date.UTC(2011, 0, 1)));
// Sun Jan 2 2011
assertWeek(1, new Date(Date.UTC(2011, 0, 2)));
// Sat Aug 20 2011
assertWeek(33, new Date(Date.UTC(2011, 7, 20)));
// Sun Aug 21 2011
assertWeek(34, new Date(Date.UTC(2011, 7, 21)));

// Leap year tests.

// Sat Feb 27 2016
assertWeek(8, new Date(Date.UTC(2016, 1, 27)));
// Sun Feb 28 2016
assertWeek(9, new Date(Date.UTC(2016, 1, 28)));
// Mon Feb 29 2016
assertWeek(9, new Date(Date.UTC(2016, 1, 29)));
// Tue Mar 1 2016
assertWeek(9, new Date(Date.UTC(2016, 2, 1)));

// Sat Feb 28 2032
assertWeek(8, new Date(Date.UTC(2032, 1, 28)));
// Sun Feb 29 2032
assertWeek(9, new Date(Date.UTC(2032, 1, 29)));
// Mon Mar 1 2032
assertWeek(9, new Date(Date.UTC(2032, 2, 1)));

// Fri Feb 28 2020
assertWeek(8, new Date(Date.UTC(2020, 1, 28)));
// Sat Feb 29 2020
assertWeek(8, new Date(Date.UTC(2020, 1, 29)));
// Sun Mar 1 2020
assertWeek(9, new Date(Date.UTC(2020, 2, 1)));

// Timestamp argument.
assertWeek(1, new Timestamp(new Date(Date.UTC(1984, 0, 1)).getTime() / 1000, 0));
assertWeek(1, new Timestamp(new Date(Date.UTC(1984, 0, 1)).getTime() / 1000, 1000000000));

// Numeric argument not allowed.
assertErrorCode(t, {$project: {a: {$week: 5}}}, 16006);

// String argument not allowed.
assertErrorCode(t, {$project: {a: {$week: 'foo'}}}, 16006);

// Array argument format.
assertWeek(8, [new Date(Date.UTC(2016, 1, 27))]);

// Wrong number of arguments.
assertErrorCode(t, {$project: {a: {$week: []}}}, 16020);
assertErrorCode(
    t,
    {$project: {a: {$week: [new Date(Date.UTC(2020, 1, 28)), new Date(Date.UTC(2020, 1, 29))]}}},
    16020);

// From a field path expression.
t.remove({});
t.save({a: new Date(Date.UTC(2020, 2, 1))});
assertWeek(9, '$a');