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 0000000000..7593e1eb48 Binary files /dev/null and b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo differ 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 0000000000..01c002e9e0 Binary files /dev/null and b/tests/regressiontests/views/locale/es/LC_MESSAGES/djangojs.mo differ 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 0000000000..4feca0b44d Binary files /dev/null and b/tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.mo differ diff --git a/tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.po b/tests/regressiontests/views/locale/fr/LC_MESSAGES/djangojs.po new file mode 100644 index 0000000000..9cfaa23232 --- /dev/null +++ b/tests/regressiontests/views/locale/fr/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 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')), )