diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-12-19 05:08:37 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-12-19 05:08:37 +0000 |
| commit | 97091940b1efbc6018133e9f77402c2983fa702f (patch) | |
| tree | e003a3ec00ab4b0bd0cda7799485cedbf787cd31 /tests/regressiontests | |
| parent | 13d3162aaf7fce11c06d447f397e32b7648ae199 (diff) | |
queryset-refactor: Merged from trunk up to [6953].
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6954 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/cache/tests.py | 58 | ||||
| -rw-r--r-- | tests/regressiontests/defaultfilters/tests.py | 12 | ||||
| -rw-r--r-- | tests/regressiontests/forms/localflavor/cl.py | 8 | ||||
| -rw-r--r-- | tests/regressiontests/forms/localflavor/uk.py | 29 | ||||
| -rw-r--r-- | tests/regressiontests/httpwrappers/tests.py | 38 | ||||
| -rw-r--r-- | tests/regressiontests/maxlength/tests.py | 6 | ||||
| -rw-r--r-- | tests/regressiontests/model_regress/models.py | 10 | ||||
| -rw-r--r-- | tests/regressiontests/templates/filters.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/views/media/file.unknown | 1 | ||||
| -rw-r--r-- | tests/regressiontests/views/tests/static.py | 8 |
10 files changed, 147 insertions, 27 deletions
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 9ac2722b0a..f050348c77 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -3,8 +3,8 @@ # Unit tests for cache framework # Uses whatever cache backend is set in the test settings file. -import time, unittest - +import time +import unittest from django.core.cache import cache from django.utils.cache import patch_vary_headers from django.http import HttpResponse @@ -27,7 +27,7 @@ class Cache(unittest.TestCase): cache.add("addkey1", "value") cache.add("addkey1", "newvalue") self.assertEqual(cache.get("addkey1"), "value") - + def test_non_existent(self): # get with non-existent keys self.assertEqual(cache.get("does_not_exist"), None) @@ -76,10 +76,16 @@ class Cache(unittest.TestCase): self.assertEqual(cache.get("stuff"), stuff) def test_expiration(self): - # expiration - cache.set('expire', 'very quickly', 1) - time.sleep(2) - self.assertEqual(cache.get("expire"), None) + cache.set('expire1', 'very quickly', 1) + cache.set('expire2', 'very quickly', 1) + cache.set('expire3', 'very quickly', 1) + + time.sleep(2) + self.assertEqual(cache.get("expire1"), None) + + cache.add("expire2", "newvalue") + self.assertEqual(cache.get("expire2"), "newvalue") + self.assertEqual(cache.has_key("expire3"), False) def test_unicode(self): stuff = { @@ -92,6 +98,44 @@ class Cache(unittest.TestCase): cache.set(key, value) self.assertEqual(cache.get(key), value) +import os +import md5 +import shutil +import tempfile +from django.core.cache.backends.filebased import CacheClass as FileCache + +class FileBasedCacheTests(unittest.TestCase): + """ + Specific test cases for the file-based cache. + """ + def setUp(self): + self.dirname = tempfile.mktemp() + os.mkdir(self.dirname) + self.cache = FileCache(self.dirname, {}) + + def tearDown(self): + shutil.rmtree(self.dirname) + + def test_hashing(self): + """Test that keys are hashed into subdirectories correctly""" + self.cache.set("foo", "bar") + keyhash = md5.new("foo").hexdigest() + keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) + self.assert_(os.path.exists(keypath)) + + def test_subdirectory_removal(self): + """ + Make sure that the created subdirectories are correctly removed when empty. + """ + self.cache.set("foo", "bar") + keyhash = md5.new("foo").hexdigest() + keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) + self.assert_(os.path.exists(keypath)) + + self.cache.delete("foo") + self.assert_(not os.path.exists(keypath)) + self.assert_(not os.path.exists(os.path.dirname(keypath))) + self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath)))) class CacheUtils(unittest.TestCase): """TestCase for django.utils.cache functions.""" diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py index bfa03cd6e1..668ecb9d5a 100644 --- a/tests/regressiontests/defaultfilters/tests.py +++ b/tests/regressiontests/defaultfilters/tests.py @@ -49,6 +49,18 @@ u'\\\\ : backslashes, too' >>> capfirst(u'hello world') u'Hello world' +>>> escapejs(u'"double quotes" and \'single quotes\'') +u'\\"double quotes\\" and \\\'single quotes\\\'' + +>>> escapejs(ur'\ : backslashes, too') +u'\\\\ : backslashes, too' + +>>> escapejs(u'and lots of whitespace: \r\n\t\v\f\b') +u'and lots of whitespace: \\r\\n\\t\\v\\f\\b' + +>>> escapejs(ur'<script>and this</script>') +u'<script>and this<\\/script>' + >>> fix_ampersands(u'Jack & Jill & Jeroboam') u'Jack & Jill & Jeroboam' diff --git a/tests/regressiontests/forms/localflavor/cl.py b/tests/regressiontests/forms/localflavor/cl.py index 407f6610e4..23e7a41902 100644 --- a/tests/regressiontests/forms/localflavor/cl.py +++ b/tests/regressiontests/forms/localflavor/cl.py @@ -41,7 +41,7 @@ Strict RUT usage (does not allow imposible values) >>> rut.clean('11-6') Traceback (most recent call last): ... -ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'] +ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.'] # valid format, bad verifier. >>> rut.clean('11.111.111-0') @@ -53,17 +53,17 @@ ValidationError: [u'The Chilean RUT is not valid.'] >>> rut.clean('767484100') Traceback (most recent call last): ... -ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'] +ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.'] >>> rut.clean('78.412.790-7') u'78.412.790-7' >>> rut.clean('8.334.6043') Traceback (most recent call last): ... -ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'] +ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.'] >>> rut.clean('76793310-K') Traceback (most recent call last): ... -ValidationError: [u'Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'] +ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.'] ## CLRegionSelect ######################################################### >>> from django.contrib.localflavor.cl.forms import CLRegionSelect diff --git a/tests/regressiontests/forms/localflavor/uk.py b/tests/regressiontests/forms/localflavor/uk.py index d7848f70a8..258c22e5a9 100644 --- a/tests/regressiontests/forms/localflavor/uk.py +++ b/tests/regressiontests/forms/localflavor/uk.py @@ -12,13 +12,15 @@ u'BT32 4PX' >>> f.clean('GIR 0AA') u'GIR 0AA' >>> f.clean('BT324PX') +u'BT32 4PX' +>>> f.clean('1NV 4L1D') Traceback (most recent call last): ... -ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] ->>> f.clean('1NV 4L1D') +ValidationError: [u'Enter a valid postcode.'] +>>> f.clean('1NV4L1D') Traceback (most recent call last): ... -ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] +ValidationError: [u'Enter a valid postcode.'] >>> f.clean(None) Traceback (most recent call last): ... @@ -27,7 +29,20 @@ ValidationError: [u'This field is required.'] Traceback (most recent call last): ... ValidationError: [u'This field is required.'] - +>>> f.clean(' so11aa ') +u'SO1 1AA' +>>> f.clean(' so1 1aa ') +u'SO1 1AA' +>>> f.clean('G2 3wt') +u'G2 3WT' +>>> f.clean('EC1A 1BB') +u'EC1A 1BB' +>>> f.clean('Ec1a1BB') +u'EC1A 1BB' +>>> f.clean(' b0gUS') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid postcode.'] >>> f = UKPostcodeField(required=False) >>> f.clean('BT32 4PX') u'BT32 4PX' @@ -36,11 +51,9 @@ u'GIR 0AA' >>> f.clean('1NV 4L1D') Traceback (most recent call last): ... -ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] +ValidationError: [u'Enter a valid postcode.'] >>> f.clean('BT324PX') -Traceback (most recent call last): -... -ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] +u'BT32 4PX' >>> f.clean(None) u'' >>> f.clean('') diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py index 5cfae029bb..31b956a99d 100644 --- a/tests/regressiontests/httpwrappers/tests.py +++ b/tests/regressiontests/httpwrappers/tests.py @@ -391,9 +391,45 @@ u'\ufffd' >>> q.getlist('foo') [u'bar', u'\ufffd'] + +###################################### +# HttpResponse with Unicode headers # +###################################### + +>>> r = HttpResponse() + +If we insert a unicode value it will be converted to an ascii +string. This makes sure we comply with the HTTP specifications. + +>>> r['value'] = u'test value' +>>> isinstance(r['value'], str) +True + +An error is raised When a unicode object with non-ascii is assigned. + +>>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS +Traceback (most recent call last): +... +UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format + +The response also converts unicode keys to strings. + +>>> r[u'test'] = 'testing key' +>>> l = list(r.items()) +>>> l.sort() +>>> l[1] +('test', 'testing key') + +It will also raise errors for keys with non-ascii data. + +>>> r[u't\xebst'] = 'testing key' # doctest:+ELLIPSIS +Traceback (most recent call last): +... +UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format + """ -from django.http import QueryDict +from django.http import QueryDict, HttpResponse if __name__ == "__main__": import doctest diff --git a/tests/regressiontests/maxlength/tests.py b/tests/regressiontests/maxlength/tests.py index 8a5f874c78..c7ed1f91c0 100644 --- a/tests/regressiontests/maxlength/tests.py +++ b/tests/regressiontests/maxlength/tests.py @@ -22,12 +22,12 @@ Don't print out the deprecation warnings during testing. >>> legacy_maxlength(10, 12) Traceback (most recent call last): ... -TypeError: field can not take both the max_length argument and the legacy maxlength argument. +TypeError: Field cannot take both the max_length argument and the legacy maxlength argument. >>> legacy_maxlength(0, 10) Traceback (most recent call last): ... -TypeError: field can not take both the max_length argument and the legacy maxlength argument. +TypeError: Field cannot take both the max_length argument and the legacy maxlength argument. >>> legacy_maxlength(0, None) 0 @@ -48,7 +48,7 @@ TypeError: field can not take both the max_length argument and the legacy maxlen >>> fields.Field(maxlength=10, max_length=15) Traceback (most recent call last): ... -TypeError: field can not take both the max_length argument and the legacy maxlength argument. +TypeError: Field cannot take both the max_length argument and the legacy maxlength argument. # Test max_length >>> new.max_length diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py index 00c3bc96f0..02e73a5aa9 100644 --- a/tests/regressiontests/model_regress/models.py +++ b/tests/regressiontests/model_regress/models.py @@ -11,6 +11,7 @@ class Article(models.Model): pub_date = models.DateTimeField() status = models.IntegerField(blank=True, null=True, choices=CHOICES) misc_data = models.CharField(max_length=100, blank=True) + article_text = models.TextField() class Meta: ordering = ('pub_date','headline') @@ -41,5 +42,14 @@ Empty strings should be returned as Unicode >>> a2 = Article.objects.get(pk=a.id) >>> a2.misc_data u'' + +# TextFields can hold more than 4000 characters (this was broken in Oracle). +>>> a3 = Article(headline="Really, really big", pub_date=datetime.now()) +>>> a3.article_text = "ABCDE" * 1000 +>>> a3.save() +>>> a4 = Article.objects.get(pk=a3.id) +>>> len(a4.article_text) +5000 + """ } diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py index 4175bdbe5f..f38b2cdef1 100644 --- a/tests/regressiontests/templates/filters.py +++ b/tests/regressiontests/templates/filters.py @@ -108,8 +108,8 @@ def get_filter_tests(): 'filter-urlize05': ('{% autoescape off %}{{ a|urlize }}{% endautoescape %}', {"a": "<script>alert('foo')</script>"}, "<script>alert('foo')</script>"), 'filter-urlize06': ('{{ a|urlize }}', {"a": "<script>alert('foo')</script>"}, '<script>alert('foo')</script>'), - 'filter-urlizetrunc01': ('{% autoescape off %}{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}{% endautoescape %}', {"a": "http://example.com/x=&y=", "b": mark_safe("http://example.com?x=&y=")}, u'<a href="http://example.com/x=&y=" rel="nofollow">http:...</a> <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'), - 'filter-urlizetrunc02': ('{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}', {"a": "http://example.com/x=&y=", "b": mark_safe("http://example.com?x=&y=")}, u'<a href="http://example.com/x=&y=" rel="nofollow">http:...</a> <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'), + 'filter-urlizetrunc01': ('{% autoescape off %}{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}{% endautoescape %}', {"a": '"Unsafe" http://example.com/x=&y=', "b": mark_safe('"Safe" http://example.com?x=&y=')}, u'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> "Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'), + 'filter-urlizetrunc02': ('{{ a|urlizetrunc:"8" }} {{ b|urlizetrunc:"8" }}', {"a": '"Unsafe" http://example.com/x=&y=', "b": mark_safe('"Safe" http://example.com?x=&y=')}, u'"Unsafe" <a href="http://example.com/x=&y=" rel="nofollow">http:...</a> "Safe" <a href="http://example.com?x=&y=" rel="nofollow">http:...</a>'), 'filter-wordcount01': ('{% autoescape off %}{{ a|wordcount }} {{ b|wordcount }}{% endautoescape %}', {"a": "a & b", "b": mark_safe("a & b")}, "3 3"), 'filter-wordcount02': ('{{ a|wordcount }} {{ b|wordcount }}', {"a": "a & b", "b": mark_safe("a & b")}, "3 3"), diff --git a/tests/regressiontests/views/media/file.unknown b/tests/regressiontests/views/media/file.unknown new file mode 100644 index 0000000000..77dcda8970 --- /dev/null +++ b/tests/regressiontests/views/media/file.unknown @@ -0,0 +1 @@ +An unknown file extension. diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py index c731b249e8..d7e87d19d2 100644 --- a/tests/regressiontests/views/tests/static.py +++ b/tests/regressiontests/views/tests/static.py @@ -13,11 +13,15 @@ class StaticTests(TestCase): response = self.client.get('/views/site_media/%s' % filename) file = open(path.join(media_dir, filename)) self.assertEquals(file.read(), response.content) + self.assertEquals(len(response.content), int(response['Content-Length'])) + + def test_unknown_mime_type(self): + response = self.client.get('/views/site_media/file.unknown') + self.assertEquals('application/octet-stream', response['Content-Type']) def test_copes_with_empty_path_component(self): file_name = 'file.txt' response = self.client.get('/views/site_media//%s' % file_name) file = open(path.join(media_dir, file_name)) self.assertEquals(file.read(), response.content) - - + |
