PageRenderTime 149ms CodeModel.GetById 116ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/generic_views/dates.py

https://code.google.com/p/mango-py/
Python | 415 lines | 313 code | 78 blank | 24 comment | 7 complexity | daa469d1e2e8b8f3783892e41a6dcfbb MD5 | raw file
  1import datetime
  2import random
  3
  4from django.core.exceptions import ImproperlyConfigured
  5from django.test import TestCase
  6
  7from regressiontests.generic_views.models import Book
  8
  9class ArchiveIndexViewTests(TestCase):
 10    fixtures = ['generic-views-test-data.json']
 11    urls = 'regressiontests.generic_views.urls'
 12
 13    def _make_books(self, n, base_date):
 14        for i in range(n):
 15            b = Book.objects.create(
 16                name='Book %d' % i,
 17                slug='book-%d' % i,
 18                pages=100+i,
 19                pubdate=base_date - datetime.timedelta(days=1))
 20
 21    def test_archive_view(self):
 22        res = self.client.get('/dates/books/')
 23        self.assertEqual(res.status_code, 200)
 24        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
 25        self.assertEqual(list(res.context['latest']), list(Book.objects.all()))
 26        self.assertTemplateUsed(res, 'generic_views/book_archive.html')
 27
 28    def test_archive_view_context_object_name(self):
 29        res = self.client.get('/dates/books/context_object_name/')
 30        self.assertEqual(res.status_code, 200)
 31        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
 32        self.assertEqual(list(res.context['thingies']), list(Book.objects.all()))
 33        self.assertFalse('latest' in res.context)
 34        self.assertTemplateUsed(res, 'generic_views/book_archive.html')
 35
 36    def test_empty_archive_view(self):
 37        Book.objects.all().delete()
 38        res = self.client.get('/dates/books/')
 39        self.assertEqual(res.status_code, 404)
 40
 41    def test_allow_empty_archive_view(self):
 42        Book.objects.all().delete()
 43        res = self.client.get('/dates/books/allow_empty/')
 44        self.assertEqual(res.status_code, 200)
 45        self.assertEqual(list(res.context['date_list']), [])
 46        self.assertEqual(list(res.context['date_list']), [])
 47        self.assertTemplateUsed(res, 'generic_views/book_archive.html')
 48
 49    def test_archive_view_template(self):
 50        res = self.client.get('/dates/books/template_name/')
 51        self.assertEqual(res.status_code, 200)
 52        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
 53        self.assertEqual(list(res.context['latest']), list(Book.objects.all()))
 54        self.assertTemplateUsed(res, 'generic_views/list.html')
 55
 56    def test_archive_view_template_suffix(self):
 57        res = self.client.get('/dates/books/template_name_suffix/')
 58        self.assertEqual(res.status_code, 200)
 59        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
 60        self.assertEqual(list(res.context['latest']), list(Book.objects.all()))
 61        self.assertTemplateUsed(res, 'generic_views/book_detail.html')
 62
 63    def test_archive_view_invalid(self):
 64        self.assertRaises(ImproperlyConfigured, self.client.get, '/dates/books/invalid/')
 65
 66    def test_paginated_archive_view(self):
 67        self._make_books(20, base_date=datetime.date.today())
 68        res = self.client.get('/dates/books/paginated/')
 69        self.assertEqual(res.status_code, 200)
 70        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
 71        self.assertEqual(list(res.context['latest']), list(Book.objects.all()[0:10]))
 72        self.assertTemplateUsed(res, 'generic_views/book_archive.html')
 73
 74        res = self.client.get('/dates/books/paginated/?page=2')
 75        self.assertEqual(res.status_code, 200)
 76        self.assertEqual(res.context['page_obj'].number, 2)
 77        self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20]))
 78
 79
 80class YearArchiveViewTests(TestCase):
 81    fixtures = ['generic-views-test-data.json']
 82    urls = 'regressiontests.generic_views.urls'
 83
 84    def test_year_view(self):
 85        res = self.client.get('/dates/books/2008/')
 86        self.assertEqual(res.status_code, 200)
 87        self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)])
 88        self.assertEqual(res.context['year'], '2008')
 89        self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
 90
 91    def test_year_view_make_object_list(self):
 92        res = self.client.get('/dates/books/2006/make_object_list/')
 93        self.assertEqual(res.status_code, 200)
 94        self.assertEqual(list(res.context['date_list']), [datetime.datetime(2006, 5, 1)])
 95        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006)))
 96        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006)))
 97        self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
 98
 99    def test_year_view_empty(self):
100        res = self.client.get('/dates/books/1999/')
101        self.assertEqual(res.status_code, 404)
102        res = self.client.get('/dates/books/1999/allow_empty/')
103        self.assertEqual(res.status_code, 200)
104        self.assertEqual(list(res.context['date_list']), [])
105        self.assertEqual(list(res.context['book_list']), [])
106
107    def test_year_view_allow_future(self):
108        # Create a new book in the future
109        year = datetime.date.today().year + 1
110        b = Book.objects.create(name="The New New Testement", pages=600, pubdate=datetime.date(year, 1, 1))
111        res = self.client.get('/dates/books/%s/' % year)
112        self.assertEqual(res.status_code, 404)
113
114        res = self.client.get('/dates/books/%s/allow_empty/' % year)
115        self.assertEqual(res.status_code, 200)
116        self.assertEqual(list(res.context['book_list']), [])
117
118        res = self.client.get('/dates/books/%s/allow_future/' % year)
119        self.assertEqual(res.status_code, 200)
120        self.assertEqual(list(res.context['date_list']), [datetime.datetime(year, 1, 1)])
121
122    def test_year_view_paginated(self):
123        res = self.client.get('/dates/books/2006/paginated/')
124        self.assertEqual(res.status_code, 200)
125        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2006)))
126        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2006)))
127        self.assertTemplateUsed(res, 'generic_views/book_archive_year.html')
128
129    def test_year_view_invalid_pattern(self):
130        res = self.client.get('/dates/books/no_year/')
131        self.assertEqual(res.status_code, 404)
132
133class MonthArchiveViewTests(TestCase):
134    fixtures = ['generic-views-test-data.json']
135    urls = 'regressiontests.generic_views.urls'
136
137    def test_month_view(self):
138        res = self.client.get('/dates/books/2008/oct/')
139        self.assertEqual(res.status_code, 200)
140        self.assertTemplateUsed(res, 'generic_views/book_archive_month.html')
141        self.assertEqual(list(res.context['date_list']), [datetime.datetime(2008, 10, 1)])
142        self.assertEqual(list(res.context['book_list']),
143                         list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1))))
144        self.assertEqual(res.context['month'], datetime.date(2008, 10, 1))
145
146        # Since allow_empty=False, next/prev months must be valid (#7164)
147        self.assertEqual(res.context['next_month'], None)
148        self.assertEqual(res.context['previous_month'], datetime.date(2006, 5, 1))
149
150    def test_month_view_allow_empty(self):
151        # allow_empty = False, empty month
152        res = self.client.get('/dates/books/2000/jan/')
153        self.assertEqual(res.status_code, 404)
154
155        # allow_empty = True, empty month
156        res = self.client.get('/dates/books/2000/jan/allow_empty/')
157        self.assertEqual(res.status_code, 200)
158        self.assertEqual(list(res.context['date_list']), [])
159        self.assertEqual(list(res.context['book_list']), [])
160        self.assertEqual(res.context['month'], datetime.date(2000, 1, 1))
161
162        # Since it's allow empty, next/prev are allowed to be empty months (#7164)
163        self.assertEqual(res.context['next_month'], datetime.date(2000, 2, 1))
164        self.assertEqual(res.context['previous_month'], datetime.date(1999, 12, 1))
165
166        # allow_empty but not allow_future: next_month should be empty (#7164)
167        url = datetime.date.today().strftime('/dates/books/%Y/%b/allow_empty/').lower()
168        res = self.client.get(url)
169        self.assertEqual(res.status_code, 200)
170        self.assertEqual(res.context['next_month'], None)
171
172    def test_month_view_allow_future(self):
173        future = (datetime.date.today() + datetime.timedelta(days=60)).replace(day=1)
174        urlbit = future.strftime('%Y/%b').lower()
175        b = Book.objects.create(name="The New New Testement", pages=600, pubdate=future)
176
177        # allow_future = False, future month
178        res = self.client.get('/dates/books/%s/' % urlbit)
179        self.assertEqual(res.status_code, 404)
180
181        # allow_future = True, valid future month
182        res = self.client.get('/dates/books/%s/allow_future/' % urlbit)
183        self.assertEqual(res.status_code, 200)
184        self.assertEqual(res.context['date_list'][0].date(), b.pubdate)
185        self.assertEqual(list(res.context['book_list']), [b])
186        self.assertEqual(res.context['month'], future)
187
188        # Since it's allow_future but not allow_empty, next/prev are not
189        # allowed to be empty months (#7164)
190        self.assertEqual(res.context['next_month'], None)
191        self.assertEqual(res.context['previous_month'], datetime.date(2008, 10, 1))
192
193        # allow_future, but not allow_empty, with a current month. So next
194        # should be in the future (yup, #7164, again)
195        res = self.client.get('/dates/books/2008/oct/allow_future/')
196        self.assertEqual(res.status_code, 200)
197        self.assertEqual(res.context['next_month'], future)
198        self.assertEqual(res.context['previous_month'], datetime.date(2006, 5, 1))
199
200    def test_month_view_paginated(self):
201        res = self.client.get('/dates/books/2008/oct/paginated/')
202        self.assertEqual(res.status_code, 200)
203        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10)))
204        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10)))
205        self.assertTemplateUsed(res, 'generic_views/book_archive_month.html')
206
207    def test_custom_month_format(self):
208        res = self.client.get('/dates/books/2008/10/')
209        self.assertEqual(res.status_code, 200)
210
211    def test_month_view_invalid_pattern(self):
212        res = self.client.get('/dates/books/2007/no_month/')
213        self.assertEqual(res.status_code, 404)
214
215    def test_previous_month_without_content(self):
216        "Content can exist on any day of the previous month. Refs #14711"
217        self.pubdate_list = [
218            datetime.date(2010, month, day)
219            for month,day in ((9,1), (10,2), (11,3))
220        ]
221        for pubdate in self.pubdate_list:
222            name = str(pubdate)
223            Book.objects.create(name=name, slug=name, pages=100, pubdate=pubdate)
224
225        res = self.client.get('/dates/books/2010/nov/allow_empty/')
226        self.assertEqual(res.status_code, 200)
227        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
228        # The following test demonstrates the bug
229        res = self.client.get('/dates/books/2010/nov/')
230        self.assertEqual(res.status_code, 200)
231        self.assertEqual(res.context['previous_month'], datetime.date(2010,10,1))
232        # The bug does not occur here because a Book with pubdate of Sep 1 exists
233        res = self.client.get('/dates/books/2010/oct/')
234        self.assertEqual(res.status_code, 200)
235        self.assertEqual(res.context['previous_month'], datetime.date(2010,9,1))
236
237
238class WeekArchiveViewTests(TestCase):
239    fixtures = ['generic-views-test-data.json']
240    urls = 'regressiontests.generic_views.urls'
241
242    def test_week_view(self):
243        res = self.client.get('/dates/books/2008/week/39/')
244        self.assertEqual(res.status_code, 200)
245        self.assertTemplateUsed(res, 'generic_views/book_archive_week.html')
246        self.assertEqual(res.context['book_list'][0], Book.objects.get(pubdate=datetime.date(2008, 10, 1)))
247        self.assertEqual(res.context['week'], datetime.date(2008, 9, 28))
248
249    def test_week_view_allow_empty(self):
250        res = self.client.get('/dates/books/2008/week/12/')
251        self.assertEqual(res.status_code, 404)
252
253        res = self.client.get('/dates/books/2008/week/12/allow_empty/')
254        self.assertEqual(res.status_code, 200)
255        self.assertEqual(list(res.context['book_list']), [])
256
257    def test_week_view_allow_future(self):
258        future = datetime.date(datetime.date.today().year + 1, 1, 1)
259        b = Book.objects.create(name="The New New Testement", pages=600, pubdate=future)
260
261        res = self.client.get('/dates/books/%s/week/1/' % future.year)
262        self.assertEqual(res.status_code, 404)
263
264        res = self.client.get('/dates/books/%s/week/1/allow_future/' % future.year)
265        self.assertEqual(res.status_code, 200)
266        self.assertEqual(list(res.context['book_list']), [b])
267
268    def test_week_view_paginated(self):
269        week_start = datetime.date(2008, 9, 28)
270        week_end = week_start + datetime.timedelta(days=7)
271        res = self.client.get('/dates/books/2008/week/39/')
272        self.assertEqual(res.status_code, 200)
273        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end)))
274        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__gte=week_start, pubdate__lt=week_end)))
275        self.assertTemplateUsed(res, 'generic_views/book_archive_week.html')
276
277    def test_week_view_invalid_pattern(self):
278        res = self.client.get('/dates/books/2007/week/no_week/')
279        self.assertEqual(res.status_code, 404)
280
281    def test_week_start_Monday(self):
282        # Regression for #14752
283        res = self.client.get('/dates/books/2008/week/39/')
284        self.assertEqual(res.status_code, 200)
285        self.assertEqual(res.context['week'], datetime.date(2008, 9, 28))
286
287        res = self.client.get('/dates/books/2008/week/39/monday/')
288        self.assertEqual(res.status_code, 200)
289        self.assertEqual(res.context['week'], datetime.date(2008, 9, 29))
290
291class DayArchiveViewTests(TestCase):
292    fixtures = ['generic-views-test-data.json']
293    urls = 'regressiontests.generic_views.urls'
294
295    def test_day_view(self):
296        res = self.client.get('/dates/books/2008/oct/01/')
297        self.assertEqual(res.status_code, 200)
298        self.assertTemplateUsed(res, 'generic_views/book_archive_day.html')
299        self.assertEqual(list(res.context['book_list']),
300                         list(Book.objects.filter(pubdate=datetime.date(2008, 10, 1))))
301        self.assertEqual(res.context['day'], datetime.date(2008, 10, 1))
302
303        # Since allow_empty=False, next/prev days must be valid.
304        self.assertEqual(res.context['next_day'], None)
305        self.assertEqual(res.context['previous_day'], datetime.date(2006, 5, 1))
306
307    def test_day_view_allow_empty(self):
308        # allow_empty = False, empty month
309        res = self.client.get('/dates/books/2000/jan/1/')
310        self.assertEqual(res.status_code, 404)
311
312        # allow_empty = True, empty month
313        res = self.client.get('/dates/books/2000/jan/1/allow_empty/')
314        self.assertEqual(res.status_code, 200)
315        self.assertEqual(list(res.context['book_list']), [])
316        self.assertEqual(res.context['day'], datetime.date(2000, 1, 1))
317
318        # Since it's allow empty, next/prev are allowed to be empty months (#7164)
319        self.assertEqual(res.context['next_day'], datetime.date(2000, 1, 2))
320        self.assertEqual(res.context['previous_day'], datetime.date(1999, 12, 31))
321
322        # allow_empty but not allow_future: next_month should be empty (#7164)
323        url = datetime.date.today().strftime('/dates/books/%Y/%b/%d/allow_empty/').lower()
324        res = self.client.get(url)
325        self.assertEqual(res.status_code, 200)
326        self.assertEqual(res.context['next_day'], None)
327
328    def test_day_view_allow_future(self):
329        future = (datetime.date.today() + datetime.timedelta(days=60))
330        urlbit = future.strftime('%Y/%b/%d').lower()
331        b = Book.objects.create(name="The New New Testement", pages=600, pubdate=future)
332
333        # allow_future = False, future month
334        res = self.client.get('/dates/books/%s/' % urlbit)
335        self.assertEqual(res.status_code, 404)
336
337        # allow_future = True, valid future month
338        res = self.client.get('/dates/books/%s/allow_future/' % urlbit)
339        self.assertEqual(res.status_code, 200)
340        self.assertEqual(list(res.context['book_list']), [b])
341        self.assertEqual(res.context['day'], future)
342
343        # allow_future but not allow_empty, next/prev amust be valid
344        self.assertEqual(res.context['next_day'], None)
345        self.assertEqual(res.context['previous_day'], datetime.date(2008, 10, 1))
346
347        # allow_future, but not allow_empty, with a current month.
348        res = self.client.get('/dates/books/2008/oct/01/allow_future/')
349        self.assertEqual(res.status_code, 200)
350        self.assertEqual(res.context['next_day'], future)
351        self.assertEqual(res.context['previous_day'], datetime.date(2006, 5, 1))
352
353    def test_day_view_paginated(self):
354        res = self.client.get('/dates/books/2008/oct/1/')
355        self.assertEqual(res.status_code, 200)
356        self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1)))
357        self.assertEqual(list(res.context['object_list']), list(Book.objects.filter(pubdate__year=2008, pubdate__month=10, pubdate__day=1)))
358        self.assertTemplateUsed(res, 'generic_views/book_archive_day.html')
359
360    def test_next_prev_context(self):
361        res = self.client.get('/dates/books/2008/oct/01/')
362        self.assertEqual(res.content, "Archive for Oct. 1, 2008. Previous day is May 1, 2006")
363
364    def test_custom_month_format(self):
365        res = self.client.get('/dates/books/2008/10/01/')
366        self.assertEqual(res.status_code, 200)
367
368    def test_day_view_invalid_pattern(self):
369        res = self.client.get('/dates/books/2007/oct/no_day/')
370        self.assertEqual(res.status_code, 404)
371
372    def test_today_view(self):
373        res = self.client.get('/dates/books/today/')
374        self.assertEqual(res.status_code, 404)
375        res = self.client.get('/dates/books/today/allow_empty/')
376        self.assertEqual(res.status_code, 200)
377        self.assertEqual(res.context['day'], datetime.date.today())
378
379class DateDetailViewTests(TestCase):
380    fixtures = ['generic-views-test-data.json']
381    urls = 'regressiontests.generic_views.urls'
382
383    def test_date_detail_by_pk(self):
384        res = self.client.get('/dates/books/2008/oct/01/1/')
385        self.assertEqual(res.status_code, 200)
386        self.assertEqual(res.context['object'], Book.objects.get(pk=1))
387        self.assertEqual(res.context['book'], Book.objects.get(pk=1))
388        self.assertTemplateUsed(res, 'generic_views/book_detail.html')
389
390    def test_date_detail_by_slug(self):
391        res = self.client.get('/dates/books/2006/may/01/byslug/dreaming-in-code/')
392        self.assertEqual(res.status_code, 200)
393        self.assertEqual(res.context['book'], Book.objects.get(slug='dreaming-in-code'))
394
395    def test_date_detail_custom_month_format(self):
396        res = self.client.get('/dates/books/2008/10/01/1/')
397        self.assertEqual(res.status_code, 200)
398        self.assertEqual(res.context['book'], Book.objects.get(pk=1))
399
400    def test_date_detail_allow_future(self):
401        future = (datetime.date.today() + datetime.timedelta(days=60))
402        urlbit = future.strftime('%Y/%b/%d').lower()
403        b = Book.objects.create(name="The New New Testement", slug="new-new", pages=600, pubdate=future)
404
405        res = self.client.get('/dates/books/%s/new-new/' % urlbit)
406        self.assertEqual(res.status_code, 404)
407
408        res = self.client.get('/dates/books/%s/%s/allow_future/' % (urlbit, b.id))
409        self.assertEqual(res.status_code, 200)
410        self.assertEqual(res.context['book'], b)
411        self.assertTemplateUsed(res, 'generic_views/book_detail.html')
412
413    def test_invalid_url(self):
414        self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/")
415