From 1571e9c32b9f944e273e06bd458acd081c9f0a14 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 17 Sep 2007 14:48:33 +0000 Subject: [PATCH] Fixed #5496 -- Added test cases for some of the built-in Django views. Thanks to Manuel Saelices for the original patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6370 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + tests/regressiontests/views/__init__.py | 0 .../views/fixtures/testdata.json | 25 +++++++++++ .../views/locale/en/LC_MESSAGES/djangojs.mo | Bin 0 -> 452 bytes .../views/locale/en/LC_MESSAGES/djangojs.po | 20 +++++++++ .../views/locale/es/LC_MESSAGES/djangojs.mo | Bin 0 -> 445 bytes .../views/locale/es/LC_MESSAGES/djangojs.po | 21 ++++++++++ .../views/locale/fr/LC_MESSAGES/djangojs.mo | Bin 0 -> 436 bytes .../views/locale/fr/LC_MESSAGES/djangojs.po | 20 +++++++++ tests/regressiontests/views/media/file.txt | 1 + tests/regressiontests/views/models.py | 24 +++++++++++ tests/regressiontests/views/tests/__init__.py | 3 ++ tests/regressiontests/views/tests/defaults.py | 39 ++++++++++++++++++ tests/regressiontests/views/tests/i18n.py | 30 ++++++++++++++ tests/regressiontests/views/tests/static.py | 15 +++++++ tests/regressiontests/views/urls.py | 26 ++++++++++++ tests/regressiontests/views/views.py | 12 ++++++ tests/urls.py | 3 ++ 18 files changed, 240 insertions(+) create mode 100644 tests/regressiontests/views/__init__.py create mode 100644 tests/regressiontests/views/fixtures/testdata.json create mode 100644 tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo create mode 100644 tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po create mode 100644 tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo create mode 100644 tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.po create mode 100644 tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo create mode 100644 tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.po create mode 100644 tests/regressiontests/views/media/file.txt create mode 100644 tests/regressiontests/views/models.py create mode 100644 tests/regressiontests/views/tests/__init__.py create mode 100644 tests/regressiontests/views/tests/defaults.py create mode 100644 tests/regressiontests/views/tests/i18n.py create mode 100644 tests/regressiontests/views/tests/static.py create mode 100644 tests/regressiontests/views/urls.py create mode 100644 tests/regressiontests/views/views.py diff --git a/AUTHORS b/AUTHORS index 794d5249d1..ed943fc4ef 100644 --- a/AUTHORS +++ b/AUTHORS @@ -260,6 +260,7 @@ answer newbie questions, and generally made Django that much better: Brian Rosner Oliver Rutherfurd ryankanno + Manuel Saelices Ivan Sagalaev (Maniac) Vinay Sajip David Schein diff --git a/tests/regressiontests/views/__init__.py b/tests/regressiontests/views/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/views/fixtures/testdata.json b/tests/regressiontests/views/fixtures/testdata.json new file mode 100644 index 0000000000..2e52fa0989 --- /dev/null +++ b/tests/regressiontests/views/fixtures/testdata.json @@ -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" + } + } +] diff --git a/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo new file mode 100644 index 0000000000000000000000000000000000000000..7593e1eb4888dceb7e468736f9a91f52ac96df80 GIT binary patch literal 452 zcmZ{gPfx-y7{-Uu%Z{Ercz6?!SR6(aClV$rxVV3gL87<8>L{5l>59=0;@wZ?XYpMi z;pCJ4@;1%$zJ1#7!{c9Ls{<^j$T@O^^pILHa)LY?Mx%pug`DC32WtbVRrhYCm%IX8 zTEd)xR+*^EOtajGm0YofCSFcvOjTS88)9epLetHi0i;(WCvdJ zH43TeXO$-DE}K*~jHbR1p%YN((ZKQif#bRfolN>@6~!hi7)e?7ZiDZH<7pka=lg?_ zx1=iiX5a;M|EPA$GKW%VCh(50HHOvfQ(vshjEg&1EHhOxeLqb{q`UXkS>Bi;R4io9 WMPWmC&b4X$_np86Fj17eS{i>`0D3L} literal 0 HcmV?d00001 diff --git a/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po new file mode 100644 index 0000000000..54ee2c4065 --- /dev/null +++ b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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" \ No newline at end of file diff --git a/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo b/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo new file mode 100644 index 0000000000000000000000000000000000000000..01c002e9e0e3878edd15a03a3d997c0e5124ef33 GIT binary patch literal 445 zcmYL_%TB{E5Jgi2n=DzeU@?0zq=@ne1vRuTMC3)9BEc>+Luyp)Vmk;vggsxzw=fZv zdgY^$tUKfD`QF_Ab^f(MWtZ3|wunB_DkFA?d&lYYsJ;^9>-H$KoJ^}%oLLrD>;bhAO8Srm!@J;y!voJJVVbx(E|s<{HnphObyFKwC?DJ` zE!a?FX{m{gXqpd4o_ET;bJp!a_ryQ$?Rj)VMwa7;Xos(Sk37g(oU%~>x2Ye8drxV? z-KaE{6{~DY`!JqH5hOv(q0i$Wj0VAIl=I2timbA3${JaL<(m&tklak$$o(i9EX0A* z^_3fkF<*bGd*Hc4B`r#N<7-J_Eq>a=<)Rew0%r448?=|xV$9CgzBbDXYR2VERYKN2 coIMNcI%sH-R-i<9Zx9UXcBfS{6P0rQ00q8x+5i9m literal 0 HcmV?d00001 diff --git a/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.po b/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.po new file mode 100644 index 0000000000..03c01b7656 --- /dev/null +++ b/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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" \ No newline at end of file diff --git a/tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo b/tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo new file mode 100644 index 0000000000000000000000000000000000000000..4feca0b44d69463a63957c4869ca210ba7bce725 GIT binary patch literal 436 zcmYL_-%i3X6vl_qn_cRS7haruEDjSQOe9QJaB+Wd3=+L7tc;SmlCCrQAm00AK8t5U z!k7MXn&vyFU;BM}{%h=Yg5?UiM$V8vQfrD_A}@x~=wRI<7r6hy+CXa6y?g16C;^w2 zu;ic>E6SW{o*Gjnw|u3Emy!inrI3XUQ)l$-Oeid9JoBQ^jQLI~O@cBLDS522k|fdw za~dUNq&U;qL|mL^!>wiA5vxbq9ccILcBg6K2^vYvKSVwJ;CrP`OoE8G4m?G6;5FZ( zkeWU#HAxQHq_SZ=_k9SRfI^=Jj^_^@*Ny3HHb85URV?Eq;q2W8-w7x4I&$Clhq-7; zl?}|m3+VAt?UrSZWTAP1cYN(ItmdEkW|uQj+{0?aRLS+jJQ, 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 \n" +"Language-Team: LANGUAGE \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" \ No newline at end of file diff --git a/tests/regressiontests/views/media/file.txt b/tests/regressiontests/views/media/file.txt new file mode 100644 index 0000000000..f1fc82c455 --- /dev/null +++ b/tests/regressiontests/views/media/file.txt @@ -0,0 +1 @@ +An example media file. \ No newline at end of file diff --git a/tests/regressiontests/views/models.py b/tests/regressiontests/views/models.py new file mode 100644 index 0000000000..613050fb16 --- /dev/null +++ b/tests/regressiontests/views/models.py @@ -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 + diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py new file mode 100644 index 0000000000..fa766ddd7c --- /dev/null +++ b/tests/regressiontests/views/tests/__init__.py @@ -0,0 +1,3 @@ +from defaults import * +from i18n import * +from static import * \ No newline at end of file diff --git a/tests/regressiontests/views/tests/defaults.py b/tests/regressiontests/views/tests/defaults.py new file mode 100644 index 0000000000..bf490d7cf0 --- /dev/null +++ b/tests/regressiontests/views/tests/defaults.py @@ -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) diff --git a/tests/regressiontests/views/tests/i18n.py b/tests/regressiontests/views/tests/i18n.py new file mode 100644 index 0000000000..ebe97ab2dc --- /dev/null +++ b/tests/regressiontests/views/tests/i18n.py @@ -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) diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py new file mode 100644 index 0000000000..0a67cf543e --- /dev/null +++ b/tests/regressiontests/views/tests/static.py @@ -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) \ No newline at end of file diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py new file mode 100644 index 0000000000..67d918a018 --- /dev/null +++ b/tests/regressiontests/views/urls.py @@ -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.*)$', 'django.views.static.serve', {'document_root': media_dir}), +) diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py new file mode 100644 index 0000000000..720e43e0ac --- /dev/null +++ b/tests/regressiontests/views/views.py @@ -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('Dummy page') + + +def jsi18n_test(request): + """ View for testing javascript message files """ + return render_to_response('js_i18n.html', {}) diff --git a/tests/urls.py b/tests/urls.py index dd475b0ea7..d7251007c5 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -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')), )