Fixed #5506 -- Added some basic tests for date-based generic views. Thanks to robh for the original patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2007-09-19 13:26:56 +00:00
parent 1571e9c32b
commit 466871ec1b
9 changed files with 126 additions and 10 deletions

View File

@ -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",

View File

@ -17,7 +17,9 @@ 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

View File

@ -1,3 +1,4 @@
from defaults import *
from i18n import *
from static import *
from generic.date_based import *

View File

@ -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)

View File

@ -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<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),
)

View File

@ -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', {})

View File

@ -0,0 +1 @@
This template intentionally left blank

View File

@ -0,0 +1 @@
This template intentionally left blank