-
Notifications
You must be signed in to change notification settings - Fork 12
/
time_search_spec.rb
227 lines (167 loc) · 6.15 KB
/
time_search_spec.rb
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#encoding: utf-8
require "spec_helper"
describe "Mongoid::Metastamp::Time" do
let :eastern_zone do
"Eastern Time (US & Canada)"
end
let :pacific_zone do
"Pacific Time (US & Canada)"
end
let :utc_zone do
"UTC"
end
let :wed_oct_5_ten_am do
"2011-10-05 10:00:00"
end
context "given a 10:00 eastern and a 10:00 pacific timestamp" do
before :each do
Time.zone = eastern_zone
@ten_am_eastern = Time.zone.parse(wed_oct_5_ten_am)
@eastern_event = Event.create(timestamp: @ten_am_eastern)
Time.zone = pacific_zone
@ten_am_pacific = Time.zone.parse(wed_oct_5_ten_am)
@pacific_event = Event.create(timestamp: @ten_am_pacific)
Time.zone = utc_zone
@ten_am_utc = Time.zone.parse(wed_oct_5_ten_am)
end
describe "searching by timestamp" do
it "should not return any events when searching a 10:00 UTC range" do
Event.where(
:timestamp.gt => @ten_am_utc - 1.second,
:timestamp.lt => @ten_am_utc + 1.second
).to_a.should == []
end
it "should only return the ET event when searching a 10:00 ET range" do
Event.where(
:timestamp.gt => @ten_am_eastern - 1.second,
:timestamp.lt => @ten_am_eastern + 1.second
).to_a.should == [@eastern_event]
end
it "should only return the PT event when searching a 10:00 PT range" do
Event.where(
:timestamp.gt => @ten_am_pacific - 1.second,
:timestamp.lt => @ten_am_pacific + 1.second
).to_a.should == [@pacific_event]
end
end
describe "searching by timestamp.time" do
it "should not return any events when searching a 10:00 UTC range" do
Event.where(
"timestamp.time" => {
'$gt' => @ten_am_utc - 1.second,
'$lt' => @ten_am_utc + 1.second
}
).to_a.should == []
end
it "should only return the ET event when searching a 10:00 ET range" do
Event.where(
"timestamp.time" => {
'$gt' => @ten_am_eastern - 1.second,
'$lt' => @ten_am_eastern + 1.second
}
).to_a.should == [@eastern_event]
end
it "should only return the PT event when searching a 10:00 PT range" do
Event.where(
"timestamp.time" => {
'$gt' => @ten_am_pacific - 1.second,
'$lt' => @ten_am_pacific + 1.second
}
).to_a.should == [@pacific_event]
end
end
describe "searching by timestamp.normalized" do
it "should return both events when searching a 10:00 UTC range" do
Event.where(
"timestamp.normalized" => {
'$gt' => @ten_am_utc - 1.second,
'$lt' => @ten_am_utc + 1.second
}
).to_a.should == [@eastern_event, @pacific_event]
end
it "should not return any events when searching a 10:00 ET range" do
Event.where(
"timestamp.normalized" => {
'$gt' => @ten_am_eastern - 1.second,
'$lt' => @ten_am_eastern + 1.second
}
).to_a.should == []
end
it "should not return any events when searching a 10:00 PT range" do
Event.where(
"timestamp.normalized" => {
'$gt' => @ten_am_pacific - 1.second,
'$lt' => @ten_am_pacific + 1.second
}
).to_a.should == []
end
end
describe "searching by timestamp.year" do
it "should not return any events for 2010" do
Event.where("timestamp.year" => 2010).count.should == 0
end
it "should return both events for 2011" do
Event.where("timestamp.year" => 2011).count.should == 2
end
end
describe "searching by timestamp.month" do
it "should not return any events when before October" do
Event.where("timestamp.month" => {'$lt' => 10}).count.should == 0
end
it "should return both events for October" do
Event.where("timestamp.month" => 10).count.should == 2
end
end
describe "searching by timestamp.day" do
it "should not return any events when after the 5th" do
Event.where("timestamp.day" => {'$gt' => 5}).count.should == 0
end
it "should return both events for the 5th" do
Event.where("timestamp.day" => 5).count.should == 2
end
end
describe "searching by timestamp.wday" do
it "should not return any events when thurs - tues" do
Event.where("timestamp.wday" => {'$gte' => 4, '$lte' => 2}).count.should == 0
end
it "should return both events for wed" do
Event.where("timestamp.wday" => 3).count.should == 2
end
end
describe "searching by timestamp.hour" do
it "should not return any events when before 10 AM" do
Event.where("timestamp.hour" => {'$lt' => 10}).count.should == 0
end
it "should return both events for 10 AM" do
Event.where("timestamp.hour" => 10).count.should == 2
end
end
describe "searching by timestamp.min" do
it "should not return any events when after 1 minute" do
Event.where("timestamp.min" => {'$gte' => 1}).count.should == 0
end
it "should return both events for 0" do
Event.where("timestamp.min" => 0).count.should == 2
end
end
describe "searching by timestamp.sec" do
it "should not return any events when after 1 second" do
Event.where("timestamp.sec" => {'$gte' => 1}).count.should == 0
end
it "should return both events for 0" do
Event.where("timestamp.sec" => 0).count.should == 2
end
end
describe "searching by timestamp.zone" do
it "should return no events when searching UTC" do
Event.where("timestamp.zone" => utc_zone).to_a.should == []
end
it "should return only the eastern event when searching Eastern Time (US & Canada)" do
Event.where("timestamp.zone" => eastern_zone).to_a.should == [@eastern_event]
end
it "should return only the pacific event when searching Pacific Time (US & Canada)" do
Event.where("timestamp.zone" => pacific_zone).to_a.should == [@pacific_event]
end
end
end
end