PageRenderTime 44ms CodeModel.GetById 18ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/regressiontests/views/tests/generic/date_based.py

https://code.google.com/p/mango-py/
Python | 140 lines | 114 code | 20 blank | 6 comment | 1 complexity | 95de966aa94111b60ea517a32dec5685 MD5 | raw file
  1# coding: utf-8
  2from django.test import TestCase
  3from datetime import datetime, date
  4from datetime import timedelta
  5from regressiontests.views.models import Article, Author, DateArticle
  6
  7class ObjectDetailTest(TestCase):
  8    fixtures = ['testdata.json']
  9    def setUp(self):
 10        # Correct the date for the current article
 11        current_article = Article.objects.get(title="Current Article")
 12        current_article.date_created = datetime.now()
 13        current_article.save()
 14
 15    def test_finds_past(self):
 16        "date_based.object_detail can view a page in the past"
 17        response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/')
 18        self.assertEqual(response.status_code, 200)
 19        self.assertEqual(response.context['object'].title, "Old Article")
 20
 21    def test_object_detail_finds_today(self):
 22        "date_based.object_detail can view a page from today"
 23        today_url = datetime.now().strftime('%Y/%m/%d')
 24        response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url)
 25        self.assertEqual(response.status_code, 200)
 26        self.assertEqual(response.context['object'].title, "Current Article")
 27
 28    def test_object_detail_ignores_future(self):
 29        "date_based.object_detail can view a page from the future, but only if allowed."
 30        response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/')
 31        self.assertEqual(response.status_code, 404)
 32
 33    def test_object_detail_allowed_future_if_enabled(self):
 34        "date_based.object_detail can view a page from the future if explicitly allowed."
 35        response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/allow_future/')
 36        self.assertEqual(response.status_code, 200)
 37        self.assertEqual(response.context['object'].title, "Future Article")
 38
 39class MonthArchiveTest(TestCase):
 40    def test_archive_month_includes_only_month(self):
 41        "Regression for #3031: Archives around Feburary include only one month"
 42        author = Author(name="John Smith")
 43        author.save()
 44
 45        # 2004 was a leap year, so it should be weird enough to not cheat
 46        first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1)
 47        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
 48        two_seconds = timedelta(0, 2, 0)
 49        article = Article(title="example", author=author)
 50
 51        article.date_created = first_second_of_feb
 52        article.save()
 53        response = self.client.get('/views/date_based/archive_month/2004/02/')
 54        self.assertEqual(response.status_code, 200)
 55        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
 56        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
 57
 58        article.date_created = first_second_of_feb-two_seconds
 59        article.save()
 60        response = self.client.get('/views/date_based/archive_month/2004/02/')
 61        self.assertEqual(response.status_code, 404)
 62
 63        article.date_created = first_second_of_mar-two_seconds
 64        article.save()
 65        response = self.client.get('/views/date_based/archive_month/2004/02/')
 66        self.assertEqual(response.status_code, 200)
 67        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
 68        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
 69
 70        article.date_created = first_second_of_mar
 71        article.save()
 72        response = self.client.get('/views/date_based/archive_month/2004/02/')
 73        self.assertEqual(response.status_code, 404)
 74
 75        article2 = DateArticle(title="example", author=author)
 76
 77        article2.date_created = first_second_of_feb.date()
 78        article2.save()
 79        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
 80        self.assertEqual(response.status_code, 200)
 81        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
 82        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
 83
 84        article2.date_created = (first_second_of_feb-two_seconds).date()
 85        article2.save()
 86        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
 87        self.assertEqual(response.status_code, 404)
 88
 89        article2.date_created = (first_second_of_mar-two_seconds).date()
 90        article2.save()
 91        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
 92        self.assertEqual(response.status_code, 200)
 93        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
 94        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))
 95
 96        article2.date_created = first_second_of_mar.date()
 97        article2.save()
 98        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
 99        self.assertEqual(response.status_code, 404)
100
101        now = datetime.now()
102        prev_month = now.date().replace(day=1)
103        if prev_month.month == 1:
104            prev_month = prev_month.replace(year=prev_month.year-1, month=12)
105        else:
106            prev_month = prev_month.replace(month=prev_month.month-1)
107        article2.date_created = now
108        article2.save()
109        response = self.client.get('/views/date_based/datefield/archive_month/%s/' % now.strftime('%Y/%m'))
110        self.assertEqual(response.status_code, 200)
111        self.assertEqual(response.context['next_month'], None)
112        self.assertEqual(response.context['previous_month'], prev_month)
113        
114    def test_archive_month_date_list(self):
115        author = Author(name="John Smith")
116        author.save()
117        date1 = datetime(2010, 1, 1, 0, 0, 0)
118        date2 = datetime(2010, 1, 2, 0, 0, 0)
119        Article.objects.create(title='example1', author=author, date_created=date1)
120        Article.objects.create(title='example2', author=author, date_created=date2)
121        response = self.client.get('/views/date_based/archive_month/2010/1/')
122        self.assertEqual(response.status_code, 200)
123        self.assertEqual(len(response.context['date_list']), 2)
124        self.assertEqual(response.context['date_list'][0], date1)
125        # Checks that the same date is not included more than once in the list
126        Article.objects.create(title='example2', author=author, date_created=date2)
127        response = self.client.get('/views/date_based/archive_month/2010/1/')
128        self.assertEqual(len(response.context['date_list']), 2)
129
130class DayArchiveTests(TestCase):
131
132    def test_year_month_day_format(self):
133        """
134        Make sure day views don't get confused with numeric month formats (#7944)
135        """
136        author = Author.objects.create(name="John Smith")
137        article = Article.objects.create(title="example", author=author, date_created=datetime(2004, 1, 21, 0, 0, 1))
138        response = self.client.get('/views/date_based/archive_day/2004/1/21/')
139        self.assertEqual(response.status_code, 200)
140        self.assertEqual(response.context['object_list'][0], article)