Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #5506 -- Added some basic tests for date-based generic views. T…

…hanks to robh for the original patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 466871ec1b40f4e68bbe693cfdeacef4eec4370c 1 parent 1571e9c
Russell Keith-Magee freakboy3742 authored
25 tests/regressiontests/views/fixtures/testdata.json
View
@@ -4,10 +4,33 @@
"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",
"fields": {
4 tests/regressiontests/views/models.py
View
@@ -17,8 +17,10 @@ def get_absolute_url(self):
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
3  tests/regressiontests/views/tests/__init__.py
View
@@ -1,3 +1,4 @@
from defaults import *
from i18n import *
-from static import *
+from static import *
+from generic.date_based import *
0  tests/regressiontests/views/tests/generic/__init__.py
View
No changes.
71 tests/regressiontests/views/tests/generic/date_based.py
View
@@ -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)
+
+
24 tests/regressiontests/views/urls.py
View
@@ -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 @@
'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<path>.*)$', 'django.views.static.serve', {'document_root': media_dir}),
+
+ # Date-based generic views
+ (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
+ 'django.views.generic.date_based.object_detail',
+ dict(slug_field='slug', **date_based_info_dict)),
+ (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\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<year>\d{4})/(?P<month>\d{1,2})/$',
+ 'django.views.generic.date_based.archive_month',
+ date_based_info_dict),
)
7 tests/regressiontests/views/views.py
View
@@ -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('<html><body>Dummy page</body></html>')
-
-def jsi18n_test(request):
- """ View for testing javascript message files """
- return render_to_response('js_i18n.html', {})
1  tests/templates/views/article_archive_month.html
View
@@ -0,0 +1 @@
+This template intentionally left blank
1  tests/templates/views/article_detail.html
View
@@ -0,0 +1 @@
+This template intentionally left blank
Please sign in to comment.
Something went wrong with that request. Please try again.