reporting_period_spec.rb - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(HTM) git clone git://jay.scot/reportable
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
reporting_period_spec.rb (15815B)
---
1 require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))),'spec_helper')
2
3 describe Saulabs::Reportable::ReportingPeriod do
4
5 describe '#date_time' do
6
7 it 'should return the date and time with minutes = seconds = 0 for grouping :hour' do
8 date_time = DateTime.now
9 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
10
11 reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 0, 0)
12 end
13
14 it 'should return the date part only for grouping :day' do
15 date_time = DateTime.now
16 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
17
18 reporting_period.date_time.should == date_time.to_date
19 end
20
21 describe 'for grouping :week' do
22
23 it 'should return the date of the monday of the week date_time is in for any day in that week' do
24 date_time = DateTime.new(2008, 11, 27)
25 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
26
27 reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24)
28 end
29
30 it 'should return the date of the monday of the week date_time is in when the specified date is a monday already' do
31 date_time = DateTime.new(2008, 11, 24)
32 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
33
34 reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24)
35 end
36
37 it 'should return the date of the monday of the week date_time is in when the monday is in a different month than the specified date' do
38 date_time = DateTime.new(2008, 11, 1)
39 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
40
41 reporting_period.date_time.should == Date.new(2008, 10, 27)
42 end
43
44 it 'should return the date of the monday of the week date_time is in when the monday is in a different year than the specified date' do
45 date_time = DateTime.new(2009, 1, 1)
46 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
47
48 reporting_period.date_time.should == Date.new(2008, 12, 29)
49 end
50
51 end
52
53 it 'should return the date with day = 1 for grouping :month' do
54 date_time = Time.now
55 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
56
57 reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 1)
58 end
59
60 end
61
62 describe '#last_date_time' do
63
64 it 'should return the date and time with minutes = seconds = 59 for grouping :hour' do
65 date_time = DateTime.now
66 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
67
68 reporting_period.last_date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 59, 59)
69 end
70
71 it 'should return the date part with hour = 23 and minute = seconds = 59 for grouping :day' do
72 date_time = DateTime.now
73 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
74
75 reporting_period.last_date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, 23, 59, 59)
76 end
77
78 describe 'for grouping :week' do
79
80 it 'should return the date of the sunday of the week date_time is in for any day in that week' do
81 date_time = DateTime.new(2008, 11, 27)
82 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
83
84 reporting_period.last_date_time.should == Date.new(date_time.year, date_time.month, 30)
85 end
86
87 it 'should return the date of the sunday of the week date_time is in when the sunday is in a different month than the specified date' do
88 date_time = DateTime.new(2008, 10, 30)
89 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
90
91 reporting_period.last_date_time.should == Date.new(2008, 11, 2)
92 end
93
94 it 'should return the date of the sunday of the week date_time is in when the sunday is in a different year than the specified date' do
95 date_time = DateTime.new(2008, 12, 29)
96 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
97
98 reporting_period.last_date_time.should == Date.new(2009, 1, 4)
99 end
100
101 end
102
103 it 'should return the date of the last day of the month for grouping :month' do
104 date_time = DateTime.new(2009, 4, 29)
105 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
106
107 reporting_period.last_date_time.should == Date.new(date_time.year, date_time.month, 30)
108 end
109
110 end
111
112 describe '.from_db_string' do
113
114 it 'should return a reporting period with the correct date and time and with minutes = seconds = 0 for grouping :hour' do
115 grouping = Saulabs::Reportable::Grouping.new(:hour)
116 grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1, 12])
117
118 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == DateTime.new(2008, 1, 1, 12, 0, 0)
119 end
120
121 it 'should return a reporting period with the date part only for grouping :day' do
122 grouping = Saulabs::Reportable::Grouping.new(:day)
123 grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1])
124
125 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
126 end
127
128 it 'should return a reporting period with the date part of the monday of the week the date is in for grouping :week' do
129 grouping = Saulabs::Reportable::Grouping.new(:week)
130 grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
131
132 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2007, 12, 31)
133 end
134
135 it 'should return a reporting period with the correct date and with day = 1 for grouping :month' do
136 grouping = Saulabs::Reportable::Grouping.new(:month)
137 grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
138
139 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
140 end
141
142 it "should return a reporting period with the correct date when a Date object is passed" do
143 grouping = Saulabs::Reportable::Grouping.new(:day)
144 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, Date.new(2008, 1, 1)).date_time.should == Date.new(2008, 1, 1)
145 end
146
147 it "should return a reporting period with the correct date when a DateTime object is passed" do
148 grouping = Saulabs::Reportable::Grouping.new(:hour)
149 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, DateTime.new(2008, 1, 1, 12, 0, 0)).date_time.should == DateTime.new(2008, 1, 1, 12, 0, 0)
150 end
151
152 end
153
154 describe '#next' do
155
156 it 'should return a reporting period with date and time one hour after the current period for grouping :hour' do
157 now = Time.now
158 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), now)
159 expected = now + 1.hour
160
161 reporting_period.next.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
162 end
163
164 it 'should return a reporting period with date one day after the current period for grouping :day' do
165 now = Time.now
166 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
167 expected = now + 1.day
168
169 reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
170 end
171
172 it 'should return a reporting period with date one week after the current period for grouping :week' do
173 now = DateTime.now
174 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), now)
175 expected = reporting_period.date_time + 1.week
176
177 reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
178 end
179
180 it 'should return a reporting period with date of the first day in the month one month after the current period' do
181 now = Time.now
182 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
183 expected = reporting_period.date_time + 1.month
184
185 reporting_period.next.date_time.should == Date.new(expected.year, expected.month, 1)
186 end
187
188 end
189
190 describe '#previous' do
191
192 it 'should return a reporting period with date and time one hour before the current period for grouping :hour' do
193 now = Time.now
194 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), now)
195 expected = now - 1.hour
196
197 reporting_period.previous.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
198 end
199
200 it 'should return a reporting period with date one day before the current period for grouping :day' do
201 now = Time.now
202 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
203 expected = now - 1.day
204
205 reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
206 end
207
208 it 'should return a reporting period with date one week before the current period for grouping :week' do
209 now = DateTime.now
210 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), now)
211 expected = reporting_period.date_time - 1.week
212
213 reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
214 end
215
216 it 'should return a reporting period with date of the first day in the month one month before the current period' do
217 now = Time.now
218 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
219 expected = reporting_period.date_time - 1.month
220
221 reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, 1)
222 end
223
224 end
225
226 describe '#==' do
227
228 it 'should return true for 2 reporting periods with the same date_time and grouping' do
229 now = DateTime.now
230 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
231 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
232
233 (reporting_period1 == reporting_period2).should == true
234 end
235
236 it 'should return false for 2 reporting periods with the same date_time but different groupings' do
237 now = Time.now
238 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
239 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
240
241 (reporting_period1 == reporting_period2).should == false
242 end
243
244 it 'should return true for 2 reporting periods with the same grouping but different date_times if the date times evaluate to the same reporting period identifier' do
245 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now)
246 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now + 1.day)
247
248 (reporting_period1 == reporting_period2).should == true
249 end
250
251 it 'should return false for 2 reporting periods with the same grouping but different date_times if the date times evaluate to different reporting period identifiers' do
252 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now)
253 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now + 2.months)
254
255 (reporting_period1 == reporting_period2).should == false
256 end
257
258 describe 'when invoked with DateTimes or Times' do
259
260 describe 'for grouping :hour' do
261
262 it 'should return true when the date and hour are equal' do
263 date_time = DateTime.new(2008, 10, 30, 12)
264 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
265
266 reporting_period.should == date_time
267 end
268
269 end
270
271 describe 'for grouping :day' do
272
273 it 'should return true when the date is equal' do
274 date_time = DateTime.new(2008, 10, 30)
275 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
276
277 reporting_period.should == date_time
278 end
279
280 end
281
282 describe 'for grouping :week' do
283
284 it 'should return true when the date of the first day in that week is equal' do
285 date_time = DateTime.new(2009, 5, 4) #monday (first day of the week for reports_asp_sparkline)
286 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
287
288 reporting_period.should == DateTime.new(2009, 5, 7) #thursday of same week, should be equal
289 end
290
291 end
292
293 describe 'for grouping :month' do
294
295 it 'should return true when the date of the first day in that month is equal' do
296 date_time = DateTime.new(2009, 5, 1)
297 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
298
299 reporting_period.should == DateTime.new(2009, 5, 17)
300 end
301
302 end
303
304 end
305
306 end
307
308 describe '.first' do
309
310 before do
311 @now = DateTime.now
312 DateTime.stub!(:now).and_return(@now)
313 end
314
315 it 'should return a reporting period with the date part of (DateTime.now - limit.hours with minutes = seconds = 0 for grouping :hour' do
316 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:hour), 3)
317 expected = @now - 3.hours
318
319 reporting_period.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour, 0, 0)
320 end
321
322 it 'should return a reporting period with the date part of (DateTime.now - limit.days) for grouping :day' do
323 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:day), 3)
324 expected = @now - 3.days
325
326 reporting_period.date_time.should == Date.new(expected.year, expected.month, expected.day)
327 end
328
329 it 'should return a reporting period with the date of the first day of the month at (DateTime.now - limit.months) for grouping :month' do
330 DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0))
331 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:month), 3)
332
333 reporting_period.date_time.should == DateTime.new(2008, 9, 1)
334 end
335
336 it 'should return a reporting period with the date of the monday of the week at (DateTime.now - limit.weeks) for grouping :week' do
337 DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) #wednesday
338 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:week), 3)
339
340 reporting_period.date_time.should == DateTime.new(2008, 12, 8) #the monday 3 weeks earlier
341 end
342
343 end
344
345 end