diff --git a/tests/regressiontests/views/fixtures/testdata.json b/tests/regressiontests/views/fixtures/testdata.json index 2e52fa0989..449e91913a 100644 --- a/tests/regressiontests/views/fixtures/testdata.json +++ b/tests/regressiontests/views/fixtures/testdata.json @@ -4,9 +4,32 @@ "model": "views.article", "fields": { "author": 1, - "title": "An Article" + "title": "Old Article", + "slug": "old_article", + "date_created": "2001-01-01 21:22:23" } }, + { + "pk": 2, + "model": "views.article", + "fields": { + "author": 1, + "title": "Current Article", + "slug": "current_article", + "date_created": "2007-09-17 21:22:23" + } + }, + { + "pk": 3, + "model": "views.article", + "fields": { + "author": 1, + "title": "Future Article", + "slug": "future_article", + "date_created": "3000-01-01 21:22:23" + } + }, + { "pk": 1, "model": "views.author", diff --git a/tests/regressiontests/views/models.py b/tests/regressiontests/views/models.py index 613050fb16..4bed1f3bde 100644 --- a/tests/regressiontests/views/models.py +++ b/tests/regressiontests/views/models.py @@ -17,8 +17,10 @@ class Author(models.Model): class Article(models.Model): title = models.CharField(max_length=100) + slug = models.SlugField() author = models.ForeignKey(Author) - + date_created = models.DateTimeField() + def __unicode__(self): return self.title diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py index fa766ddd7c..2c8c5b4a92 100644 --- a/tests/regressiontests/views/tests/__init__.py +++ b/tests/regressiontests/views/tests/__init__.py @@ -1,3 +1,4 @@ from defaults import * from i18n import * -from static import * \ No newline at end of file +from static import * +from generic.date_based import * \ No newline at end of file diff --git a/tests/regressiontests/views/tests/generic/__init__.py b/tests/regressiontests/views/tests/generic/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/views/tests/generic/date_based.py b/tests/regressiontests/views/tests/generic/date_based.py new file mode 100644 index 0000000000..aca93d4579 --- /dev/null +++ b/tests/regressiontests/views/tests/generic/date_based.py @@ -0,0 +1,71 @@ +# coding: utf-8 +from django.test import TestCase +from datetime import datetime +from datetime import timedelta +from regressiontests.views.models import Article, Author + +class ObjectDetailTest(TestCase): + fixtures = ['testdata.json'] + def setUp(self): + # Correct the date for the current article + current_article = Article.objects.get(title="Current Article") + current_article.date_created = datetime.now() + current_article.save() + + def test_finds_past(self): + "date_based.object_detail can view a page in the past" + response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['object'].title, "Old Article") + + def test_object_detail_finds_today(self): + "date_based.object_detail can view a page from today" + today_url = datetime.now().strftime('%Y/%m/%d') + response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['object'].title, "Current Article") + + def test_object_detail_ignores_future(self): + "date_based.object_detail can view a page from the future, but only if allowed." + response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/') + self.assertEqual(response.status_code, 404) + + def test_object_detail_allowed_future_if_enabled(self): + "date_based.object_detail can view a page from the future if explicitly allowed." + response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/allow_future/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['object'].title, "Future Article") + +class MonthArchiveTest(TestCase): + def test_archive_month_includes_only_month(self): + "Regression for #3031: Archives around Feburary include only one month" + author = Author(name="John Smith") + author.save() + + # 2004 was a leap year, so it should be weird enough to not cheat + first_second_of_feb = datetime(2004, 2, 1, 0, 0, 0) + first_second_of_mar = datetime(2004, 3, 1, 0, 0, 0) + one_microsecond = timedelta(0, 0, 1) + article = Article(title="example", author=author) + + article.date_created = first_second_of_feb + article.save() + response = self.client.get('/views/date_based/archive_month/2004/02/') + self.assertEqual(response.status_code, 200) + + article.date_created = first_second_of_feb-one_microsecond + article.save() + response = self.client.get('/views/date_based/archive_month/2004/02/') + self.assertEqual(response.status_code, 404) + + article.date_created = first_second_of_mar-one_microsecond + article.save() + response = self.client.get('/views/date_based/archive_month/2004/02/') + self.assertEqual(response.status_code, 200) + + article.date_created = first_second_of_mar + article.save() + response = self.client.get('/views/date_based/archive_month/2004/02/') + self.assertEqual(response.status_code, 404) + + \ No newline at end of file diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index 67d918a018..5ef0c5129d 100644 --- a/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -1,6 +1,8 @@ from os import path from django.conf.urls.defaults import * + +from models import * import views base_dir = path.dirname(path.abspath(__file__)) @@ -12,15 +14,35 @@ js_info_dict = { 'packages': ('regressiontests.views',), } +date_based_info_dict = { + 'queryset': Article.objects.all(), + 'date_field': 'date_created', + 'month_format': '%m', +} + urlpatterns = patterns('', (r'^$', views.index_page), + + # Default views (r'^shortcut/(\d+)/(.*)/$', 'django.views.defaults.shortcut'), (r'^non_existing_url/', 'django.views.defaults.page_not_found'), (r'^server_error/', 'django.views.defaults.server_error'), + # i18n views (r'^i18n/', include('django.conf.urls.i18n')), (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict), - (r'^jsi18n_test/$', views.jsi18n_test), + # Static views (r'^site_media/(?P.*)$', 'django.views.static.serve', {'document_root': media_dir}), + + # Date-based generic views + (r'^date_based/object_detail/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/$', + 'django.views.generic.date_based.object_detail', + dict(slug_field='slug', **date_based_info_dict)), + (r'^date_based/object_detail/(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P[-\w]+)/allow_future/$', + 'django.views.generic.date_based.object_detail', + dict(allow_future=True, slug_field='slug', **date_based_info_dict)), + (r'^date_based/archive_month/(?P\d{4})/(?P\d{1,2})/$', + 'django.views.generic.date_based.archive_month', + date_based_info_dict), ) diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py index 720e43e0ac..9e0bbb2d66 100644 --- a/tests/regressiontests/views/views.py +++ b/tests/regressiontests/views/views.py @@ -1,12 +1,7 @@ from django.http import HttpResponse from django.template import RequestContext -from django.shortcuts import render_to_response def index_page(request): - """ Dummy index page """ + """Dummy index page""" return HttpResponse('Dummy page') - -def jsi18n_test(request): - """ View for testing javascript message files """ - return render_to_response('js_i18n.html', {}) diff --git a/tests/templates/views/article_archive_month.html b/tests/templates/views/article_archive_month.html new file mode 100644 index 0000000000..3f8ff55da6 --- /dev/null +++ b/tests/templates/views/article_archive_month.html @@ -0,0 +1 @@ +This template intentionally left blank \ No newline at end of file diff --git a/tests/templates/views/article_detail.html b/tests/templates/views/article_detail.html new file mode 100644 index 0000000000..3f8ff55da6 --- /dev/null +++ b/tests/templates/views/article_detail.html @@ -0,0 +1 @@ +This template intentionally left blank \ No newline at end of file