| | 240 | sub sorting : Tests(6) { |
| | 241 | my ($tmp, @tmp); |
| | 242 | my @foo = map { Foo->load($_) } (1..2); |
| | 243 | |
| | 244 | ## Load using descending sort (newest) |
| | 245 | $tmp = Foo->load(undef, { |
| | 246 | sort => 'created_on', |
| | 247 | direction => 'descend', |
| | 248 | limit => 1 }); |
| | 249 | is_object($tmp, $foo[1], 'Newest Foo is Foo #2'); |
| | 250 | |
| | 251 | ## Load using ascending sort (oldest) |
| | 252 | $tmp = Foo->load(undef, { |
| | 253 | sort => 'created_on', |
| | 254 | direction => 'ascend', |
| | 255 | limit => 1 }); |
| | 256 | is_object($tmp, $foo[0], 'Oldest Foo is Foo #1'); |
| | 257 | |
| | 258 | ## Load using descending sort with limit = 2 |
| | 259 | @tmp = Foo->load(undef, { |
| | 260 | sort => 'created_on', |
| | 261 | direction => 'descend', |
| | 262 | limit => 2 }); |
| | 263 | are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load() finds Foos #2 and #1'); |
| | 264 | |
| | 265 | ## Load using descending sort by created_on, no limit |
| | 266 | @tmp = Foo->load(undef, { |
| | 267 | sort => 'created_on', |
| | 268 | direction => 'descend' }); |
| | 269 | are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load() finds Foos #2 and #1'); |
| | 270 | |
| | 271 | ## Load using ascending sort by status, no limit |
| | 272 | @tmp = Foo->load(undef, { sort => 'status', }); |
| | 273 | are_objects(\@tmp, [ reverse @foo ], 'All Foos lowest-status-first load() finds Foos #2 and #1'); |
| | 274 | |
| | 275 | ## Load using 'last' where status == 2 |
| | 276 | $tmp = Foo->load({ status => 2 }, { |
| | 277 | sort => 'created_on', |
| | 278 | direction => 'descend', |
| | 279 | limit => 1 }); |
| | 280 | is_object($tmp, $foo[0], 'Newest status=2 Foo is Foo #1'); |
| | 281 | } |
| | 282 | |
| | 283 | sub ranges : Tests(9) { |
| | 284 | my $tmp; |
| | 285 | my @foo = map { Foo->load($_) } (1..2); |
| | 286 | |
| | 287 | ## Load using range search, one less than foo[1]->created_on and newer |
| | 288 | $tmp = Foo->load( |
| | 289 | { created_on => [ $foo[1]->column('created_on')-1 ] }, |
| | 290 | { range => { created_on => 1 } }); |
| | 291 | is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2 is Foo #2'); |
| | 292 | |
| | 293 | ## Load using EXCLUSIVE range search, up through the momment $foo[1] created |
| | 294 | $tmp = Foo->load( |
| | 295 | { created_on => [ $foo[1]->column('created_on')-1, |
| | 296 | $foo[1]->column('created_on') ] }, |
| | 297 | { range => { created_on => 1 } }); |
| | 298 | ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos"); |
| | 299 | |
| | 300 | $tmp = Foo->load( |
| | 301 | { created_on => [ $foo[1]->column('created_on'), |
| | 302 | $foo[1]->column('created_on')+1 ] }, |
| | 303 | { range => { created_on => 1 } }); |
| | 304 | ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos"); |
| | 305 | |
| | 306 | ## Load using INCLUSIVE range search, up through the momment $foo[1] created |
| | 307 | $tmp = Foo->load( |
| | 308 | { created_on => [ $foo[1]->column('created_on')-1, |
| | 309 | $foo[1]->column('created_on') ] }, |
| | 310 | { range_incl => { created_on => 1 } }); |
| | 311 | ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)'); |
| | 312 | is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date is Foo #2"); |
| | 313 | |
| | 314 | $tmp = Foo->load( |
| | 315 | { created_on => [ $foo[1]->column('created_on'), |
| | 316 | $foo[1]->column('created_on')+1 ] }, |
| | 317 | { range_incl => { created_on => 1 } }); |
| | 318 | ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)'); |
| | 319 | is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date is Foo #2"); |
| | 320 | |
| | 321 | ## Check that range searches return nothing when nothing is in the range. |
| | 322 | $tmp = Foo->load( { created_on => [ undef, '19690101000000' ] }, |
| | 323 | { range => { created_on => 1 } }); |
| | 324 | ok(!$tmp, 'Prehistoric date range load() found no Foos'); |
| | 325 | |
| | 326 | ## Range search, all items with created_on less than foo[1]->created_on |
| | 327 | $tmp = Foo->load( |
| | 328 | { created_on => [ undef, $foo[1]->column('created_on')-1 ] }, |
| | 329 | { range => { created_on => 1 } }); |
| | 330 | is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1 is Foo #1"); |
| | 331 | } |
| | 332 | |
| 308 | | ## Load using descending sort (newest) |
| 309 | | $tmp = Foo->load(undef, { |
| 310 | | sort => 'created_on', |
| 311 | | direction => 'descend', |
| 312 | | limit => 1 }); |
| 313 | | is_object($tmp, $foo[1], 'Newest Foo is Foo #2'); |
| 314 | | |
| 315 | | ## Load using ascending sort (oldest) |
| 316 | | $tmp = Foo->load(undef, { |
| 317 | | sort => 'created_on', |
| 318 | | direction => 'ascend', |
| 319 | | limit => 1 }); |
| 320 | | is_object($tmp, $foo[0], 'Oldest Foo is Foo #1'); |
| 321 | | |
| 322 | | ## Load using descending sort with limit = 2 |
| 323 | | @tmp = Foo->load(undef, { |
| 324 | | sort => 'created_on', |
| 325 | | direction => 'descend', |
| 326 | | limit => 2 }); |
| 327 | | are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load() finds Foos #2 and #1'); |
| 328 | | |
| 329 | | ## Load using descending sort by created_on, no limit |
| 330 | | @tmp = Foo->load(undef, { |
| 331 | | sort => 'created_on', |
| 332 | | direction => 'descend' }); |
| 333 | | are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load() finds Foos #2 and #1'); |
| 334 | | |
| 335 | | ## Load using ascending sort by status, no limit |
| 336 | | @tmp = Foo->load(undef, { sort => 'status', }); |
| 337 | | are_objects(\@tmp, \@foo, 'All Foos lowest-status-first load() finds Foos #1 and #2'); |
| 338 | | |
| 339 | | ## Load using 'last' where status == 0 |
| 340 | | $tmp = Foo->load({ status => 0 }, { |
| 341 | | sort => 'created_on', |
| 342 | | direction => 'descend', |
| 343 | | limit => 1 }); |
| 344 | | is_object($tmp, $foo[0], 'Newest status=0 Foo is Foo #1'); |
| 345 | | |
| 346 | | ## Load using range search, one less than foo[1]->created_on and newer |
| 347 | | $tmp = Foo->load( |
| 348 | | { created_on => [ $foo[1]->column('created_on')-1 ] }, |
| 349 | | { range => { created_on => 1 } }); |
| 350 | | is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2 is Foo #2'); |
| 351 | | |
| 352 | | ## Load using EXCLUSIVE range search, up through the momment $foo[1] created |
| 353 | | $tmp = Foo->load( |
| 354 | | { created_on => [ $foo[1]->column('created_on')-1, |
| 355 | | $foo[1]->column('created_on') ] }, |
| 356 | | { range => { created_on => 1 } }); |
| 357 | | ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos"); |
| 358 | | |
| 359 | | $tmp = Foo->load( |
| 360 | | { created_on => [ $foo[1]->column('created_on'), |
| 361 | | $foo[1]->column('created_on')+1 ] }, |
| 362 | | { range => { created_on => 1 } }); |
| 363 | | ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos"); |
| 364 | | |
| 365 | | ## Load using INCLUSIVE range search, up through the momment $foo[1] created |
| 366 | | $tmp = Foo->load( |
| 367 | | { created_on => [ $foo[1]->column('created_on')-1, |
| 368 | | $foo[1]->column('created_on') ] }, |
| 369 | | { range_incl => { created_on => 1 } }); |
| 370 | | ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)'); |
| 371 | | is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date is Foo #2"); |
| 372 | | |
| 373 | | $tmp = Foo->load( |
| 374 | | { created_on => [ $foo[1]->column('created_on'), |
| 375 | | $foo[1]->column('created_on')+1 ] }, |
| 376 | | { range_incl => { created_on => 1 } }); |
| 377 | | ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)'); |
| 378 | | is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date is Foo #2"); |
| 379 | | |
| 380 | | ## Check that range searches return nothing when nothing is in the range. |
| 381 | | $tmp = Foo->load( { created_on => [ undef, '19690101000000' ] }, |
| 382 | | { range => { created_on => 1 } }); |
| 383 | | ok(!$tmp, 'Prehistoric date range load() found no Foos'); |
| 384 | | |
| 385 | | ## Range search, all items with created_on less than foo[1]->created_on |
| 386 | | $tmp = Foo->load( |
| 387 | | { created_on => [ undef, $foo[1]->column('created_on')-1 ] }, |
| 388 | | { range => { created_on => 1 } }); |
| 389 | | is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1 is Foo #1"); |