| 2397 | |
| 2398 | |
| 2399 | class DateHierarchyTests(TestCase): |
| 2400 | fixtures = ['admin-views-users.xml'] |
| 2401 | |
| 2402 | def setUp(self): |
| 2403 | self.client.login(username='super', password='secret') |
| 2404 | |
| 2405 | def assert_contains_year_link(self, response, date): |
| 2406 | self.assertContains(response, '?release_date__year=%d"' % (date.year,)) |
| 2407 | |
| 2408 | def assert_contains_month_link(self, response, date): |
| 2409 | self.assertContains( |
| 2410 | response, '?release_date__year=%d&release_date__month=%d"' % ( |
| 2411 | date.year, date.month)) |
| 2412 | |
| 2413 | def assert_contains_day_link(self, response, date): |
| 2414 | self.assertContains( |
| 2415 | response, '?release_date__year=%d&' |
| 2416 | 'release_date__month=%d&release_date__day=%d"' % ( |
| 2417 | date.year, date.month, date.day)) |
| 2418 | |
| 2419 | def test_empty(self): |
| 2420 | """ |
| 2421 | Ensure that no date hierarchy links display with empty changelist. |
| 2422 | """ |
| 2423 | response = self.client.get( |
| 2424 | reverse('admin:admin_views_podcast_changelist')) |
| 2425 | self.assertNotContains(response, 'release_date__year=') |
| 2426 | self.assertNotContains(response, 'release_date__month=') |
| 2427 | self.assertNotContains(response, 'release_date__day=') |
| 2428 | |
| 2429 | def test_single(self): |
| 2430 | """ |
| 2431 | Ensure that single day-level date hierarchy appears for single object. |
| 2432 | """ |
| 2433 | DATE = datetime.date(2000,6,30) |
| 2434 | Podcast.objects.create(release_date=DATE) |
| 2435 | response = self.client.get( |
| 2436 | reverse('admin:admin_views_podcast_changelist')) |
| 2437 | self.assert_contains_day_link(response, DATE) |
| 2438 | |
| 2439 | def test_within_month(self): |
| 2440 | """ |
| 2441 | Ensure that day-level links appear for changelist within single month. |
| 2442 | """ |
| 2443 | DATES = (datetime.date(2000,6,30), |
| 2444 | datetime.date(2000,6,15), |
| 2445 | datetime.date(2000,6,3)) |
| 2446 | for date in DATES: |
| 2447 | Podcast.objects.create(release_date=date) |
| 2448 | response = self.client.get( |
| 2449 | reverse('admin:admin_views_podcast_changelist')) |
| 2450 | for date in DATES: |
| 2451 | self.assert_contains_day_link(response, date) |
| 2452 | |
| 2453 | def test_within_year(self): |
| 2454 | """ |
| 2455 | Ensure that month-level links appear for changelist within single year. |
| 2456 | """ |
| 2457 | DATES = (datetime.date(2000,1,30), |
| 2458 | datetime.date(2000,3,15), |
| 2459 | datetime.date(2000,5,3)) |
| 2460 | for date in DATES: |
| 2461 | Podcast.objects.create(release_date=date) |
| 2462 | response = self.client.get( |
| 2463 | reverse('admin:admin_views_podcast_changelist')) |
| 2464 | # no day-level links |
| 2465 | self.assertNotContains(response, 'release_date__day=') |
| 2466 | for date in DATES: |
| 2467 | self.assert_contains_month_link(response, date) |
| 2468 | |
| 2469 | def test_multiple_years(self): |
| 2470 | """ |
| 2471 | Ensure that year-level links appear for year-spanning changelist. |
| 2472 | """ |
| 2473 | DATES = (datetime.date(2001,1,30), |
| 2474 | datetime.date(2003,3,15), |
| 2475 | datetime.date(2005,5,3)) |
| 2476 | for date in DATES: |
| 2477 | Podcast.objects.create(release_date=date) |
| 2478 | response = self.client.get( |
| 2479 | reverse('admin:admin_views_podcast_changelist')) |
| 2480 | # no day/month-level links |
| 2481 | self.assertNotContains(response, 'release_date__day=') |
| 2482 | self.assertNotContains(response, 'release_date__month=') |
| 2483 | for date in DATES: |
| 2484 | self.assert_contains_year_link(response, date) |
| 2485 | |
| 2486 | # and make sure GET parameters still behave correctly |
| 2487 | for date in DATES: |
| 2488 | response = self.client.get( |
| 2489 | '%s?release_date__year=%d' % ( |
| 2490 | reverse('admin:admin_views_podcast_changelist'), |
| 2491 | date.year)) |
| 2492 | self.assert_contains_month_link(response, date) |
| 2493 | |
| 2494 | response = self.client.get( |
| 2495 | '%s?release_date__year=%d&release_date__month=%d' % ( |
| 2496 | reverse('admin:admin_views_podcast_changelist'), |
| 2497 | date.year, date.month)) |
| 2498 | self.assert_contains_day_link(response, date) |