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