diff options
Diffstat (limited to 'tests/regressiontests/utils')
| -rw-r--r-- | tests/regressiontests/utils/baseconv.py | 3 | ||||
| -rw-r--r-- | tests/regressiontests/utils/crypto.py | 28 | ||||
| -rw-r--r-- | tests/regressiontests/utils/decorators.py | 2 | ||||
| -rw-r--r-- | tests/regressiontests/utils/http.py | 26 | ||||
| -rw-r--r-- | tests/regressiontests/utils/simplelazyobject.py | 26 |
5 files changed, 51 insertions, 34 deletions
diff --git a/tests/regressiontests/utils/baseconv.py b/tests/regressiontests/utils/baseconv.py index 75660d8119..cc413b4e8e 100644 --- a/tests/regressiontests/utils/baseconv.py +++ b/tests/regressiontests/utils/baseconv.py @@ -1,10 +1,11 @@ from unittest import TestCase from django.utils.baseconv import base2, base16, base36, base56, base62, base64, BaseConverter +from django.utils.six.moves import xrange class TestBaseConv(TestCase): def test_baseconv(self): - nums = [-10 ** 10, 10 ** 10] + range(-100, 100) + nums = [-10 ** 10, 10 ** 10] + list(xrange(-100, 100)) for converter in [base2, base16, base36, base56, base62, base64]: for i in nums: self.assertEqual(i, converter.decode(converter.encode(i))) diff --git a/tests/regressiontests/utils/crypto.py b/tests/regressiontests/utils/crypto.py index 2bdc5ba530..52a286cb27 100644 --- a/tests/regressiontests/utils/crypto.py +++ b/tests/regressiontests/utils/crypto.py @@ -1,4 +1,6 @@ +from __future__ import unicode_literals +import binascii import math import timeit import hashlib @@ -108,15 +110,15 @@ class TestUtilsCryptoPBKDF2(unittest.TestCase): "c4007d5298f9033c0241d5ab69305e7b64eceeb8d" "834cfec"), }, - # Check leading zeros are not stripped (#17481) + # Check leading zeros are not stripped (#17481) { - "args": { - "password": chr(186), - "salt": "salt", - "iterations": 1, - "dklen": 20, - "digest": hashlib.sha1, - }, + "args": { + "password": b'\xba', + "salt": "salt", + "iterations": 1, + "dklen": 20, + "digest": hashlib.sha1, + }, "result": '0053d3b91a7f1e54effebd6d68771e8a6e0b2c5b', }, ] @@ -124,12 +126,14 @@ class TestUtilsCryptoPBKDF2(unittest.TestCase): def test_public_vectors(self): for vector in self.rfc_vectors: result = pbkdf2(**vector['args']) - self.assertEqual(result.encode('hex'), vector['result']) + self.assertEqual(binascii.hexlify(result).decode('ascii'), + vector['result']) def test_regression_vectors(self): for vector in self.regression_vectors: result = pbkdf2(**vector['args']) - self.assertEqual(result.encode('hex'), vector['result']) + self.assertEqual(binascii.hexlify(result).decode('ascii'), + vector['result']) def test_performance_scalability(self): """ @@ -140,11 +144,11 @@ class TestUtilsCryptoPBKDF2(unittest.TestCase): # to run the test suite and false positives caused by imprecise # measurement. n1, n2 = 200000, 800000 - elapsed = lambda f: timeit.Timer(f, + elapsed = lambda f: timeit.Timer(f, 'from django.utils.crypto import pbkdf2').timeit(number=1) t1 = elapsed('pbkdf2("password", "salt", iterations=%d)' % n1) t2 = elapsed('pbkdf2("password", "salt", iterations=%d)' % n2) measured_scale_exponent = math.log(t2 / t1, n2 / n1) - # This should be less than 1. We allow up to 1.2 so that tests don't + # This should be less than 1. We allow up to 1.2 so that tests don't # fail nondeterministically too often. self.assertLess(measured_scale_exponent, 1.2) diff --git a/tests/regressiontests/utils/decorators.py b/tests/regressiontests/utils/decorators.py index 96f4dd4e7a..4d503df026 100644 --- a/tests/regressiontests/utils/decorators.py +++ b/tests/regressiontests/utils/decorators.py @@ -105,4 +105,4 @@ class DecoratorFromMiddlewareTests(TestCase): response.render() self.assertTrue(getattr(request, 'process_response_reached', False)) # Check that process_response saw the rendered content - self.assertEqual(request.process_response_content, "Hello world") + self.assertEqual(request.process_response_content, b"Hello world") diff --git a/tests/regressiontests/utils/http.py b/tests/regressiontests/utils/http.py index 67dcd7af89..f22e05496d 100644 --- a/tests/regressiontests/utils/http.py +++ b/tests/regressiontests/utils/http.py @@ -1,10 +1,11 @@ import sys -from django.utils import http -from django.utils import unittest -from django.utils.datastructures import MultiValueDict from django.http import HttpResponse, utils from django.test import RequestFactory +from django.utils.datastructures import MultiValueDict +from django.utils import http +from django.utils import six +from django.utils import unittest class TestUtilsHttp(unittest.TestCase): @@ -110,22 +111,23 @@ class TestUtilsHttp(unittest.TestCase): def test_base36(self): # reciprocity works - for n in [0, 1, 1000, 1000000, sys.maxint]: + for n in [0, 1, 1000, 1000000]: self.assertEqual(n, http.base36_to_int(http.int_to_base36(n))) + if not six.PY3: + self.assertEqual(sys.maxint, http.base36_to_int(http.int_to_base36(sys.maxint))) # bad input - for n in [-1, sys.maxint+1, '1', 'foo', {1:2}, (1,2,3)]: - self.assertRaises(ValueError, http.int_to_base36, n) - + self.assertRaises(ValueError, http.int_to_base36, -1) + if not six.PY3: + self.assertRaises(ValueError, http.int_to_base36, sys.maxint + 1) + for n in ['1', 'foo', {1: 2}, (1, 2, 3), 3.141]: + self.assertRaises(TypeError, http.int_to_base36, n) + for n in ['#', ' ']: self.assertRaises(ValueError, http.base36_to_int, n) - - for n in [123, {1:2}, (1,2,3)]: + for n in [123, {1: 2}, (1, 2, 3), 3.141]: self.assertRaises(TypeError, http.base36_to_int, n) - # non-integer input - self.assertRaises(TypeError, http.int_to_base36, 3.141) - # more explicit output testing for n, b36 in [(0, '0'), (1, '1'), (42, '16'), (818469960, 'django')]: self.assertEqual(http.int_to_base36(n), b36) diff --git a/tests/regressiontests/utils/simplelazyobject.py b/tests/regressiontests/utils/simplelazyobject.py index 960a5e3201..3f81e8f608 100644 --- a/tests/regressiontests/utils/simplelazyobject.py +++ b/tests/regressiontests/utils/simplelazyobject.py @@ -19,17 +19,27 @@ class _ComplexObject(object): def __hash__(self): return hash(self.name) - def __str__(self): - return "I am _ComplexObject(%r)" % self.name + if six.PY3: + def __bytes__(self): + return ("I am _ComplexObject(%r)" % self.name).encode("utf-8") - def __unicode__(self): - return six.text_type(self.name) + def __str__(self): + return self.name + + else: + def __str__(self): + return b"I am _ComplexObject(%r)" % str(self.name) + + def __unicode__(self): + return self.name def __repr__(self): return "_ComplexObject(%r)" % self.name + complex_object = lambda: _ComplexObject("joe") + class TestUtilsSimpleLazyObject(TestCase): """ Tests for SimpleLazyObject @@ -54,11 +64,11 @@ class TestUtilsSimpleLazyObject(TestCase): # proxy __repr__ self.assertTrue("SimpleLazyObject" in repr(SimpleLazyObject(complex_object))) - def test_str(self): - self.assertEqual(str_prefix("I am _ComplexObject(%(_)s'joe')"), - str(SimpleLazyObject(complex_object))) + def test_bytes(self): + self.assertEqual(b"I am _ComplexObject('joe')", + bytes(SimpleLazyObject(complex_object))) - def test_unicode(self): + def test_text(self): self.assertEqual("joe", six.text_type(SimpleLazyObject(complex_object))) def test_class(self): |
