diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/m2o_recursive/models.py | 2 | ||||
| -rw-r--r-- | tests/modeltests/model_forms/models.py | 155 | ||||
| -rw-r--r-- | tests/modeltests/model_forms/test.png | bin | 0 -> 482 bytes | |||
| -rw-r--r-- | tests/regressiontests/backends/models.py | 28 | ||||
| -rw-r--r-- | tests/regressiontests/forms/fields.py | 33 | ||||
| -rw-r--r-- | tests/regressiontests/middleware/models.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/middleware/tests.py | 28 | ||||
| -rw-r--r-- | tests/regressiontests/templates/filters.py | 3 | ||||
| -rw-r--r-- | tests/regressiontests/templates/tests.py | 10 |
9 files changed, 231 insertions, 29 deletions
diff --git a/tests/modeltests/m2o_recursive/models.py b/tests/modeltests/m2o_recursive/models.py index c38200a25b..f58f7fe0f5 100644 --- a/tests/modeltests/m2o_recursive/models.py +++ b/tests/modeltests/m2o_recursive/models.py @@ -14,7 +14,7 @@ from django.db import models class Category(models.Model): name = models.CharField(max_length=20) - parent = models.ForeignKey('self', null=True, related_name='child_set') + parent = models.ForeignKey('self', blank=True, null=True, related_name='child_set') def __unicode__(self): return self.name diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 17c3b3551c..f1fed8f1e1 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -7,6 +7,9 @@ examples are probably a poor fit for the ModelForm syntax. In other words, most of these tests should be rewritten. """ +import os +import tempfile + from django.db import models ARTICLE_STATUS = ( @@ -55,6 +58,20 @@ class PhoneNumber(models.Model): def __unicode__(self): return self.phone +class TextFile(models.Model): + description = models.CharField(max_length=20) + file = models.FileField(upload_to=tempfile.gettempdir()) + + def __unicode__(self): + return self.description + +class ImageFile(models.Model): + description = models.CharField(max_length=20) + image = models.FileField(upload_to=tempfile.gettempdir()) + + def __unicode__(self): + return self.description + __test__ = {'API_TESTS': """ >>> from django import newforms as forms >>> from django.newforms.models import ModelForm @@ -701,4 +718,142 @@ ValidationError: [u'Select a valid choice. 4 is not one of the available choices True >>> f.cleaned_data {'phone': u'312-555-1212', 'description': u'Assistance'} + +# FileField ################################################################### + +>>> class TextFileForm(ModelForm): +... class Meta: +... model = TextFile + +# Test conditions when files is either not given or empty. + +>>> f = TextFileForm(data={'description': u'Assistance'}) +>>> f.is_valid() +False +>>> f = TextFileForm(data={'description': u'Assistance'}, files={}) +>>> f.is_valid() +False + +# Upload a file and ensure it all works as expected. + +>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}}) +>>> f.is_valid() +True +>>> type(f.cleaned_data['file']) +<class 'django.newforms.fields.UploadedFile'> +>>> instance = f.save() +>>> instance.file +u'.../test1.txt' + +# Edit an instance that already has the file defined in the model. This will not +# save the file again, but leave it exactly as it is. + +>>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance) +>>> f.is_valid() +True +>>> f.cleaned_data['file'] +u'.../test1.txt' +>>> instance = f.save() +>>> instance.file +u'.../test1.txt' + +# Delete the current file since this is not done by Django. + +>>> os.unlink(instance.get_file_filename()) + +# Override the file by uploading a new one. + +>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.file +u'.../test2.txt' + +>>> instance.delete() + +# Test the non-required FileField + +>>> f = TextFileForm(data={'description': u'Assistance'}) +>>> f.fields['file'].required = False +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.file +'' + +>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.file +u'.../test3.txt' +>>> instance.delete() + +# ImageField ################################################################### + +# ImageField and FileField are nearly identical, but they differ slighty when +# it comes to validation. This specifically tests that #6302 is fixed for +# both file fields and image fields. + +>>> class ImageFileForm(ModelForm): +... class Meta: +... model = ImageFile + +>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png")).read() + +>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': {'filename': 'test.png', 'content': image_data}}) +>>> f.is_valid() +True +>>> type(f.cleaned_data['image']) +<class 'django.newforms.fields.UploadedFile'> +>>> instance = f.save() +>>> instance.image +u'.../test.png' + +# Edit an instance that already has the image defined in the model. This will not +# save the image again, but leave it exactly as it is. + +>>> f = ImageFileForm(data={'description': u'Look, it changed'}, instance=instance) +>>> f.is_valid() +True +>>> f.cleaned_data['image'] +u'.../test.png' +>>> instance = f.save() +>>> instance.image +u'.../test.png' + +# Delete the current image since this is not done by Django. + +>>> os.unlink(instance.get_image_filename()) + +# Override the file by uploading a new one. + +>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': {'filename': 'test2.png', 'content': image_data}}, instance=instance) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.image +u'.../test2.png' + +>>> instance.delete() + +# Test the non-required ImageField + +>>> f = ImageFileForm(data={'description': u'Test'}) +>>> f.fields['image'].required = False +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.image +'' + +>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance) +>>> f.is_valid() +True +>>> instance = f.save() +>>> instance.image +u'.../test3.png' +>>> instance.delete() + """} diff --git a/tests/modeltests/model_forms/test.png b/tests/modeltests/model_forms/test.png Binary files differnew file mode 100644 index 0000000000..4f17cd075d --- /dev/null +++ b/tests/modeltests/model_forms/test.png diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py index db16d2c198..a041ab6b12 100644 --- a/tests/regressiontests/backends/models.py +++ b/tests/regressiontests/backends/models.py @@ -8,6 +8,13 @@ class Square(models.Model): def __unicode__(self): return "%s ** 2 == %s" % (self.root, self.square) +class Person(models.Model): + first_name = models.CharField(max_length=20) + last_name = models.CharField(max_length=20) + + def __unicode__(self): + return u'%s %s' % (self.first_name, self.last_name) + if connection.features.uses_case_insensitive_names: t_convert = lambda x: x.upper() else: @@ -32,4 +39,25 @@ __test__ = {'API_TESTS': """ >>> Square.objects.count() 11 +#6254: fetchone, fetchmany, fetchall return strings as unicode objects +>>> Person(first_name="John", last_name="Doe").save() +>>> Person(first_name="Jane", last_name="Doe").save() +>>> Person(first_name="Mary", last_name="Agnelline").save() +>>> Person(first_name="Peter", last_name="Parker").save() +>>> Person(first_name="Clark", last_name="Kent").save() +>>> opts2 = Person._meta +>>> f3, f4 = opts2.get_field('first_name'), opts2.get_field('last_name') +>>> query2 = ('SELECT %s, %s FROM %s ORDER BY %s' +... % (qn(f3.column), qn(f4.column), t_convert(opts2.db_table), +... qn(f3.column))) +>>> cursor.execute(query2) and None or None +>>> cursor.fetchone() +(u'Clark', u'Kent') + +>>> list(cursor.fetchmany(2)) +[(u'Jane', u'Doe'), (u'John', u'Doe')] + +>>> list(cursor.fetchall()) +[(u'Mary', u'Agnelline'), (u'Peter', u'Parker')] + """} diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index cff5db6fca..9216210e09 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -749,32 +749,59 @@ Traceback (most recent call last): ... ValidationError: [u'This field is required.'] +>>> f.clean('', '') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] + +>>> f.clean('', 'files/test1.pdf') +'files/test1.pdf' + >>> f.clean(None) Traceback (most recent call last): ... ValidationError: [u'This field is required.'] +>>> f.clean(None, '') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] + +>>> f.clean(None, 'files/test2.pdf') +'files/test2.pdf' + >>> f.clean({}) Traceback (most recent call last): ... ValidationError: [u'No file was submitted.'] +>>> f.clean({}, '') +Traceback (most recent call last): +... +ValidationError: [u'No file was submitted.'] + +>>> f.clean({}, 'files/test3.pdf') +'files/test3.pdf' + >>> f.clean('some content that is not a file') Traceback (most recent call last): ... ValidationError: [u'No file was submitted. Check the encoding type on the form.'] ->>> f.clean({'filename': 'name', 'content':None}) +>>> f.clean({'filename': 'name', 'content': None}) Traceback (most recent call last): ... ValidationError: [u'The submitted file is empty.'] ->>> f.clean({'filename': 'name', 'content':''}) +>>> f.clean({'filename': 'name', 'content': ''}) Traceback (most recent call last): ... ValidationError: [u'The submitted file is empty.'] ->>> type(f.clean({'filename': 'name', 'content':'Some File Content'})) +>>> type(f.clean({'filename': 'name', 'content': 'Some File Content'})) +<class 'django.newforms.fields.UploadedFile'> + +>>> type(f.clean({'filename': 'name', 'content': 'Some File Content'}, 'files/test4.pdf')) <class 'django.newforms.fields.UploadedFile'> # URLField ################################################################## diff --git a/tests/regressiontests/middleware/models.py b/tests/regressiontests/middleware/models.py new file mode 100644 index 0000000000..71abcc5198 --- /dev/null +++ b/tests/regressiontests/middleware/models.py @@ -0,0 +1 @@ +# models.py file for tests to run. diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py index cb5c29abe1..5e7fd320d6 100644 --- a/tests/regressiontests/middleware/tests.py +++ b/tests/regressiontests/middleware/tests.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- -import unittest - -from django.test import TestCase +from django.test import TestCase from django.http import HttpRequest from django.middleware.common import CommonMiddleware from django.conf import settings @@ -19,7 +17,7 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_have_slash(self): """ - tests that urls with slashes go unmolested + Tests that URLs with slashes go unmolested. """ settings.APPEND_SLASH = True request = self._get_request('slash/') @@ -27,7 +25,7 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_slashless_resource(self): """ - tests that matches to explicit slashless urls go unmolested + Tests that matches to explicit slashless URLs go unmolested. """ settings.APPEND_SLASH = True request = self._get_request('noslash') @@ -35,7 +33,7 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_slashless_unknown(self): """ - tests that APPEND_SLASH doesn't redirect to unknown resources + Tests that APPEND_SLASH doesn't redirect to unknown resources. """ settings.APPEND_SLASH = True request = self._get_request('unknown') @@ -43,7 +41,7 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_redirect(self): """ - tests that APPEND_SLASH redirects slashless urls to a valid pattern + Tests that APPEND_SLASH redirects slashless URLs to a valid pattern. """ settings.APPEND_SLASH = True request = self._get_request('slash') @@ -53,9 +51,9 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_no_redirect_on_POST_in_DEBUG(self): """ - tests that while in debug mode, an exception is raised with a warning - when a failed attempt is made to POST to an url which would normally be - redirected to a slashed version + Tests that while in debug mode, an exception is raised with a warning + when a failed attempt is made to POST to an URL which would normally be + redirected to a slashed version. """ settings.APPEND_SLASH = True settings.DEBUG = True @@ -68,11 +66,12 @@ class CommonMiddlewareTest(TestCase): try: CommonMiddleware().process_request(request) except RuntimeError, e: - self.assertTrue('end in a slash' in str(e)) + self.failUnless('end in a slash' in str(e)) + settings.DEBUG = False def test_append_slash_disabled(self): """ - tests disabling append slash functionality + Tests disabling append slash functionality. """ settings.APPEND_SLASH = False request = self._get_request('slash') @@ -80,8 +79,8 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_quoted(self): """ - tests that urls which require quoting are redirected to their slash - version ok + Tests that URLs which require quoting are redirected to their slash + version ok. """ settings.APPEND_SLASH = True request = self._get_request('needsquoting#') @@ -90,4 +89,3 @@ class CommonMiddlewareTest(TestCase): self.assertEquals( r['Location'], 'http://testserver/middleware/needsquoting%23/') - diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py index f38b2cdef1..fd73dc57ba 100644 --- a/tests/regressiontests/templates/filters.py +++ b/tests/regressiontests/templates/filters.py @@ -179,6 +179,9 @@ def get_filter_tests(): 'filter-first01': ('{{ a|first }} {{ b|first }}', {"a": ["a&b", "x"], "b": [mark_safe("a&b"), "x"]}, "a&b a&b"), 'filter-first02': ('{% autoescape off %}{{ a|first }} {{ b|first }}{% endautoescape %}', {"a": ["a&b", "x"], "b": [mark_safe("a&b"), "x"]}, "a&b a&b"), + 'filter-last01': ('{{ a|last }} {{ b|last }}', {"a": ["x", "a&b"], "b": ["x", mark_safe("a&b")]}, "a&b a&b"), + 'filter-last02': ('{% autoescape off %}{{ a|last }} {{ b|last }}{% endautoescape %}', {"a": ["x", "a&b"], "b": ["x", mark_safe("a&b")]}, "a&b a&b"), + 'filter-random01': ('{{ a|random }} {{ b|random }}', {"a": ["a&b", "a&b"], "b": [mark_safe("a&b"), mark_safe("a&b")]}, "a&b a&b"), 'filter-random02': ('{% autoescape off %}{{ a|random }} {{ b|random }}{% endautoescape %}', {"a": ["a&b", "a&b"], "b": [mark_safe("a&b"), mark_safe("a&b")]}, "a&b a&b"), diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index ef8fd43292..9e9033f975 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -809,16 +809,6 @@ class Templates(unittest.TestCase): '{% endfor %},' + \ '{% endfor %}', {}, ''), - - # Test syntax. - 'regroup03': ('{% regroup data by bar as %}', {}, - template.TemplateSyntaxError), - 'regroup04': ('{% regroup data by bar thisaintright grouped %}', {}, - template.TemplateSyntaxError), - 'regroup05': ('{% regroup data thisaintright bar as grouped %}', {}, - template.TemplateSyntaxError), - 'regroup06': ('{% regroup data by bar as grouped toomanyargs %}', {}, - template.TemplateSyntaxError), ### TEMPLATETAG TAG ####################################################### 'templatetag01': ('{% templatetag openblock %}', {}, '{%'), |
