mirror of
https://github.com/django/django.git
synced 2024-11-24 11:48:10 +01:00
Fixed #5496 -- Added test cases for some of the built-in Django views. Thanks to Manuel Saelices <msaelices@yaco.es> for the original patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6370 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
2e75529990
commit
1571e9c32b
1
AUTHORS
1
AUTHORS
@ -260,6 +260,7 @@ answer newbie questions, and generally made Django that much better:
|
||||
Brian Rosner <brosner@gmail.com>
|
||||
Oliver Rutherfurd <http://rutherfurd.net/>
|
||||
ryankanno
|
||||
Manuel Saelices <msaelices@yaco.es>
|
||||
Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/>
|
||||
Vinay Sajip <vinay_sajip@yahoo.co.uk>
|
||||
David Schein
|
||||
|
0
tests/regressiontests/views/__init__.py
Normal file
0
tests/regressiontests/views/__init__.py
Normal file
25
tests/regressiontests/views/fixtures/testdata.json
Normal file
25
tests/regressiontests/views/fixtures/testdata.json
Normal file
@ -0,0 +1,25 @@
|
||||
[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "views.article",
|
||||
"fields": {
|
||||
"author": 1,
|
||||
"title": "An Article"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "views.author",
|
||||
"fields": {
|
||||
"name": "Boris"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "sites.site",
|
||||
"fields": {
|
||||
"domain": "testserver",
|
||||
"name": "testserver"
|
||||
}
|
||||
}
|
||||
]
|
BIN
tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo
Normal file
BIN
tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo
Normal file
Binary file not shown.
@ -0,0 +1,20 @@
|
||||
# SOME DESCRIPTIVE TITLE.
|
||||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
|
||||
# This file is distributed under the same license as the PACKAGE package.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
||||
#
|
||||
#, fuzzy
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2007-09-15 16:45+0200\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
||||
msgid "this is to be translated"
|
||||
msgstr "this is to be translated in english"
|
BIN
tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo
Normal file
BIN
tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo
Normal file
Binary file not shown.
@ -0,0 +1,21 @@
|
||||
# SOME DESCRIPTIVE TITLE.
|
||||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
|
||||
# This file is distributed under the same license as the PACKAGE package.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
||||
#
|
||||
#, fuzzy
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2007-09-15 16:45+0200\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
||||
#: media/js/translate.js:1
|
||||
msgid "this is to be translated"
|
||||
msgstr "esto tiene que ser traducido"
|
BIN
tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo
Normal file
BIN
tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo
Normal file
Binary file not shown.
@ -0,0 +1,20 @@
|
||||
# SOME DESCRIPTIVE TITLE.
|
||||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
|
||||
# This file is distributed under the same license as the PACKAGE package.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
||||
#
|
||||
#, fuzzy
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2007-09-15 19:15+0200\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
||||
msgid "this is to be translated"
|
||||
msgstr "il faut le traduire"
|
1
tests/regressiontests/views/media/file.txt
Normal file
1
tests/regressiontests/views/media/file.txt
Normal file
@ -0,0 +1 @@
|
||||
An example media file.
|
24
tests/regressiontests/views/models.py
Normal file
24
tests/regressiontests/views/models.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
Regression tests for Django built-in views
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
def get_absolute_url(self):
|
||||
return '/views/authors/%s/' % self.id
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
author = models.ForeignKey(Author)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.title
|
||||
|
3
tests/regressiontests/views/tests/__init__.py
Normal file
3
tests/regressiontests/views/tests/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from defaults import *
|
||||
from i18n import *
|
||||
from static import *
|
39
tests/regressiontests/views/tests/defaults.py
Normal file
39
tests/regressiontests/views/tests/defaults.py
Normal file
@ -0,0 +1,39 @@
|
||||
from os import path
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from regressiontests.views.models import Author, Article
|
||||
|
||||
class DefaultsTests(TestCase):
|
||||
"""Test django views in django/views/defaults.py"""
|
||||
fixtures = ['testdata.json']
|
||||
|
||||
def test_shorcut_with_absolute_url(self):
|
||||
"Can view a shortcut an Author object that has with a get_absolute_url method"
|
||||
for obj in Author.objects.all():
|
||||
short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
|
||||
response = self.client.get(short_url)
|
||||
self.assertRedirects(response, 'http://testserver%s' % obj.get_absolute_url(),
|
||||
status_code=302, target_status_code=404)
|
||||
|
||||
def test_shortcut_no_absolute_url(self):
|
||||
"Shortcuts for an object that has with a get_absolute_url method raises 404"
|
||||
for obj in Article.objects.all():
|
||||
short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk)
|
||||
response = self.client.get(short_url)
|
||||
self.assertEquals(response.status_code, 404)
|
||||
|
||||
def test_page_not_found(self):
|
||||
"A 404 status is returned by the page_not_found view"
|
||||
non_existing_urls = ['/views/non_existing_url/', # this is in urls.py
|
||||
'/views/other_non_existing_url/'] # this NOT in urls.py
|
||||
for url in non_existing_urls:
|
||||
response = self.client.get(url)
|
||||
self.assertEquals(response.status_code, 404)
|
||||
|
||||
def test_server_error(self):
|
||||
"The server_error view raises a 500 status"
|
||||
response = self.client.get('/views/server_error/')
|
||||
self.assertEquals(response.status_code, 500)
|
30
tests/regressiontests/views/tests/i18n.py
Normal file
30
tests/regressiontests/views/tests/i18n.py
Normal file
@ -0,0 +1,30 @@
|
||||
from os import path
|
||||
import gettext
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.utils.translation import activate
|
||||
|
||||
from regressiontests.views.urls import locale_dir
|
||||
|
||||
class I18NTests(TestCase):
|
||||
""" Tests django views in django/views/i18n.py """
|
||||
|
||||
def test_setlang(self):
|
||||
"""The set_language view can be used to change the session language"""
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
post_data = dict(language=lang_code, next='/views/')
|
||||
response = self.client.post('/views/i18n/setlang/', data=post_data)
|
||||
self.assertRedirects(response, 'http://testserver/views/')
|
||||
self.assertEquals(self.client.session['django_language'], lang_code)
|
||||
|
||||
def test_jsi18n(self):
|
||||
"""The javascript_catalog can be deployed with language settings"""
|
||||
for lang_code in ['es', 'fr', 'en']:
|
||||
activate(lang_code)
|
||||
catalog = gettext.translation('djangojs', locale_dir, [lang_code])
|
||||
trans_txt = catalog.ugettext('this is to be translated')
|
||||
response = self.client.get('/views/jsi18n/')
|
||||
# in response content must to be a line like that:
|
||||
# catalog['this is to be translated'] = 'same_that_trans_txt'
|
||||
self.assertContains(response, trans_txt, 1)
|
15
tests/regressiontests/views/tests/static.py
Normal file
15
tests/regressiontests/views/tests/static.py
Normal file
@ -0,0 +1,15 @@
|
||||
from os import path
|
||||
|
||||
from django.test import TestCase
|
||||
from regressiontests.views.urls import media_dir
|
||||
|
||||
class StaticTests(TestCase):
|
||||
"""Tests django views in django/views/static.py"""
|
||||
|
||||
def test_serve(self):
|
||||
"The static view can serve static media"
|
||||
media_files = ['file.txt',]
|
||||
for filename in media_files:
|
||||
response = self.client.get('/views/site_media/%s' % filename)
|
||||
file = open(path.join(media_dir, filename))
|
||||
self.assertEquals(file.read(), response.content)
|
26
tests/regressiontests/views/urls.py
Normal file
26
tests/regressiontests/views/urls.py
Normal file
@ -0,0 +1,26 @@
|
||||
from os import path
|
||||
|
||||
from django.conf.urls.defaults import *
|
||||
import views
|
||||
|
||||
base_dir = path.dirname(path.abspath(__file__))
|
||||
media_dir = path.join(base_dir, 'media')
|
||||
locale_dir = path.join(base_dir, 'locale')
|
||||
|
||||
js_info_dict = {
|
||||
'domain': 'djangojs',
|
||||
'packages': ('regressiontests.views',),
|
||||
}
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^$', views.index_page),
|
||||
(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'),
|
||||
|
||||
(r'^i18n/', include('django.conf.urls.i18n')),
|
||||
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
|
||||
(r'^jsi18n_test/$', views.jsi18n_test),
|
||||
|
||||
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': media_dir}),
|
||||
)
|
12
tests/regressiontests/views/views.py
Normal file
12
tests/regressiontests/views/views.py
Normal file
@ -0,0 +1,12 @@
|
||||
from django.http import HttpResponse
|
||||
from django.template import RequestContext
|
||||
from django.shortcuts import render_to_response
|
||||
|
||||
def index_page(request):
|
||||
""" 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', {})
|
@ -11,4 +11,7 @@ urlpatterns = patterns('',
|
||||
|
||||
# test urlconf for {% url %} template tag
|
||||
(r'^url_tag/', include('regressiontests.templates.urls')),
|
||||
|
||||
# django built-in views
|
||||
(r'^views/', include('regressiontests.views.urls')),
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user