| reporting_period_spec.rb - reportable - Fork of reportable required by WarVox, … | |
| git clone git://jay.scot/reportable | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| reporting_period_spec.rb (15815B) | |
| --- | |
| 1 require File.join(File.dirname(File.dirname(File.expand_path(__FILE__)))… | |
| 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 g… | |
| 8 date_time = DateTime.now | |
| 9 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 10 | |
| 11 reporting_period.date_time.should == DateTime.new(date_time.year, … | |
| 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(Saulab… | |
| 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 … | |
| 24 date_time = DateTime.new(2008, 11, 27) | |
| 25 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 26 | |
| 27 reporting_period.date_time.should == Date.new(date_time.year, da… | |
| 28 end | |
| 29 | |
| 30 it 'should return the date of the monday of the week date_time is … | |
| 31 date_time = DateTime.new(2008, 11, 24) | |
| 32 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 33 | |
| 34 reporting_period.date_time.should == Date.new(date_time.year, da… | |
| 35 end | |
| 36 | |
| 37 it 'should return the date of the monday of the week date_time is … | |
| 38 date_time = DateTime.new(2008, 11, 1) | |
| 39 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 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 … | |
| 45 date_time = DateTime.new(2009, 1, 1) | |
| 46 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 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(Saulab… | |
| 56 | |
| 57 reporting_period.date_time.should == Date.new(date_time.year, date… | |
| 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 … | |
| 65 date_time = DateTime.now | |
| 66 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 67 | |
| 68 reporting_period.last_date_time.should == DateTime.new(date_time.y… | |
| 69 end | |
| 70 | |
| 71 it 'should return the date part with hour = 23 and minute = seconds … | |
| 72 date_time = DateTime.now | |
| 73 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 74 | |
| 75 reporting_period.last_date_time.should == DateTime.new(date_time.y… | |
| 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 … | |
| 81 date_time = DateTime.new(2008, 11, 27) | |
| 82 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 83 | |
| 84 reporting_period.last_date_time.should == Date.new(date_time.yea… | |
| 85 end | |
| 86 | |
| 87 it 'should return the date of the sunday of the week date_time is … | |
| 88 date_time = DateTime.new(2008, 10, 30) | |
| 89 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 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 … | |
| 95 date_time = DateTime.new(2008, 12, 29) | |
| 96 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saul… | |
| 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… | |
| 104 date_time = DateTime.new(2009, 4, 29) | |
| 105 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 106 | |
| 107 reporting_period.last_date_time.should == Date.new(date_time.year,… | |
| 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 … | |
| 115 grouping = Saulabs::Reportable::Grouping.new(:hour) | |
| 116 grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1,… | |
| 117 | |
| 118 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').… | |
| 119 end | |
| 120 | |
| 121 it 'should return a reporting period with the date part only for gro… | |
| 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, '').… | |
| 126 end | |
| 127 | |
| 128 it 'should return a reporting period with the date part of the monda… | |
| 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, '').… | |
| 133 end | |
| 134 | |
| 135 it 'should return a reporting period with the correct date and with … | |
| 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, '').… | |
| 140 end | |
| 141 | |
| 142 it "should return a reporting period with the correct date when a Da… | |
| 143 grouping = Saulabs::Reportable::Grouping.new(:day) | |
| 144 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, Date… | |
| 145 end | |
| 146 | |
| 147 it "should return a reporting period with the correct date when a Da… | |
| 148 grouping = Saulabs::Reportable::Grouping.new(:hour) | |
| 149 Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, Date… | |
| 150 end | |
| 151 | |
| 152 end | |
| 153 | |
| 154 describe '#next' do | |
| 155 | |
| 156 it 'should return a reporting period with date and time one hour aft… | |
| 157 now = Time.now | |
| 158 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 159 expected = now + 1.hour | |
| 160 | |
| 161 reporting_period.next.date_time.should == DateTime.new(expected.ye… | |
| 162 end | |
| 163 | |
| 164 it 'should return a reporting period with date one day after the cur… | |
| 165 now = Time.now | |
| 166 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 167 expected = now + 1.day | |
| 168 | |
| 169 reporting_period.next.date_time.should == Date.new(expected.year, … | |
| 170 end | |
| 171 | |
| 172 it 'should return a reporting period with date one week after the cu… | |
| 173 now = DateTime.now | |
| 174 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 175 expected = reporting_period.date_time + 1.week | |
| 176 | |
| 177 reporting_period.next.date_time.should == Date.new(expected.year, … | |
| 178 end | |
| 179 | |
| 180 it 'should return a reporting period with date of the first day in t… | |
| 181 now = Time.now | |
| 182 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 183 expected = reporting_period.date_time + 1.month | |
| 184 | |
| 185 reporting_period.next.date_time.should == Date.new(expected.year, … | |
| 186 end | |
| 187 | |
| 188 end | |
| 189 | |
| 190 describe '#previous' do | |
| 191 | |
| 192 it 'should return a reporting period with date and time one hour bef… | |
| 193 now = Time.now | |
| 194 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 195 expected = now - 1.hour | |
| 196 | |
| 197 reporting_period.previous.date_time.should == DateTime.new(expecte… | |
| 198 end | |
| 199 | |
| 200 it 'should return a reporting period with date one day before the cu… | |
| 201 now = Time.now | |
| 202 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 203 expected = now - 1.day | |
| 204 | |
| 205 reporting_period.previous.date_time.should == Date.new(expected.ye… | |
| 206 end | |
| 207 | |
| 208 it 'should return a reporting period with date one week before the c… | |
| 209 now = DateTime.now | |
| 210 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 211 expected = reporting_period.date_time - 1.week | |
| 212 | |
| 213 reporting_period.previous.date_time.should == Date.new(expected.ye… | |
| 214 end | |
| 215 | |
| 216 it 'should return a reporting period with date of the first day in t… | |
| 217 now = Time.now | |
| 218 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulab… | |
| 219 expected = reporting_period.date_time - 1.month | |
| 220 | |
| 221 reporting_period.previous.date_time.should == Date.new(expected.ye… | |
| 222 end | |
| 223 | |
| 224 end | |
| 225 | |
| 226 describe '#==' do | |
| 227 | |
| 228 it 'should return true for 2 reporting periods with the same date_ti… | |
| 229 now = DateTime.now | |
| 230 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 231 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 232 | |
| 233 (reporting_period1 == reporting_period2).should == true | |
| 234 end | |
| 235 | |
| 236 it 'should return false for 2 reporting periods with the same date_t… | |
| 237 now = Time.now | |
| 238 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 239 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 240 | |
| 241 (reporting_period1 == reporting_period2).should == false | |
| 242 end | |
| 243 | |
| 244 it 'should return true for 2 reporting periods with the same groupin… | |
| 245 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 246 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 247 | |
| 248 (reporting_period1 == reporting_period2).should == true | |
| 249 end | |
| 250 | |
| 251 it 'should return false for 2 reporting periods with the same groupi… | |
| 252 reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 253 reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saula… | |
| 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(Sa… | |
| 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(Sa… | |
| 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 we… | |
| 285 date_time = DateTime.new(2009, 5, 4) #monday (first day of the… | |
| 286 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Sa… | |
| 287 | |
| 288 reporting_period.should == DateTime.new(2009, 5, 7) #thursday … | |
| 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 mo… | |
| 296 date_time = DateTime.new(2009, 5, 1) | |
| 297 reporting_period = Saulabs::Reportable::ReportingPeriod.new(Sa… | |
| 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… | |
| 316 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saul… | |
| 317 expected = @now - 3.hours | |
| 318 | |
| 319 reporting_period.date_time.should == DateTime.new(expected.year, e… | |
| 320 end | |
| 321 | |
| 322 it 'should return a reporting period with the date part of (DateTime… | |
| 323 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saul… | |
| 324 expected = @now - 3.days | |
| 325 | |
| 326 reporting_period.date_time.should == Date.new(expected.year, expec… | |
| 327 end | |
| 328 | |
| 329 it 'should return a reporting period with the date of the first day … | |
| 330 DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0… | |
| 331 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saul… | |
| 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 … | |
| 337 DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0… | |
| 338 reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saul… | |
| 339 | |
| 340 reporting_period.date_time.should == DateTime.new(2008, 12, 8) #th… | |
| 341 end | |
| 342 | |
| 343 end | |
| 344 | |
| 345 end |