summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChuck Short <chuck.short@canonical.com>2013-05-01 13:07:44 -0500
committerChuck Short <chuck.short@canonical.com>2013-05-07 11:55:56 -0500
commit63f6487e9a321dc9f89d6f71a922fcdd03b25589 (patch)
treefc9c91bd3162525867aeff7ba3a1b88ca8c55e82 /tests
parent4c69cbd8857947906e88791823cf3e9e9e5fa86a (diff)
downloadoslo-63f6487e9a321dc9f89d6f71a922fcdd03b25589.tar.gz
oslo-63f6487e9a321dc9f89d6f71a922fcdd03b25589.tar.xz
oslo-63f6487e9a321dc9f89d6f71a922fcdd03b25589.zip
Convert unicode strings for python3 portability
From http://docs.python.org/3.1/whatsnew/3.0.html You can no longer use u"..." literals for Unicode text. However, you must use b"..." literals for binary data. Use python-six to make this migration easier. Change-Id: I95166a07f4edf33be55a4bdf2674c2f238a06f19 Signed-off-by: Chuck Short <chuck.short@canonical.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/rpc/test_common.py8
-rw-r--r--tests/unit/test_strutils.py82
-rw-r--r--tests/unit/test_wsgi.py3
3 files changed, 41 insertions, 52 deletions
diff --git a/tests/unit/rpc/test_common.py b/tests/unit/rpc/test_common.py
index 2248a8f..bec8546 100644
--- a/tests/unit/rpc/test_common.py
+++ b/tests/unit/rpc/test_common.py
@@ -150,15 +150,15 @@ class RpcCommonTestCase(test_utils.BaseTestCase):
'class': 'FakeUserDefinedException',
'module': self.__class__.__module__,
'tb': ['raise FakeUserDefinedException'],
- 'args': (u'fakearg',),
- 'kwargs': {u'fakekwarg': u'fake'},
+ 'args': ('fakearg',),
+ 'kwargs': {'fakekwarg': 'fake'},
}
serialized = jsonutils.dumps(failure)
after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized)
self.assertTrue(isinstance(after_exc, FakeUserDefinedException))
- self.assertEqual(after_exc.args, (u'fakearg',))
- self.assertEqual(after_exc.kwargs, {u'fakekwarg': u'fake'})
+ self.assertEqual(after_exc.args, ('fakearg',))
+ self.assertEqual(after_exc.kwargs, {'fakekwarg': 'fake'})
def test_deserialize_remote_exception_cannot_recreate(self):
"""Ensure a RemoteError is returned on initialization failure.
diff --git a/tests/unit/test_strutils.py b/tests/unit/test_strutils.py
index e2c4622..1863c40 100644
--- a/tests/unit/test_strutils.py
+++ b/tests/unit/test_strutils.py
@@ -16,6 +16,7 @@
# under the License.
import mock
+import six
from openstack.common import strutils
from tests import utils
@@ -27,44 +28,31 @@ class StrUtilsTest(utils.BaseTestCase):
self.assertTrue(strutils.bool_from_string(True))
self.assertFalse(strutils.bool_from_string(False))
- def test_str_bool_from_string(self):
- self.assertTrue(strutils.bool_from_string('true'))
- self.assertTrue(strutils.bool_from_string('TRUE'))
- self.assertTrue(strutils.bool_from_string('on'))
- self.assertTrue(strutils.bool_from_string('On'))
- self.assertTrue(strutils.bool_from_string('yes'))
- self.assertTrue(strutils.bool_from_string('YES'))
- self.assertTrue(strutils.bool_from_string('yEs'))
- self.assertTrue(strutils.bool_from_string('1'))
-
- self.assertFalse(strutils.bool_from_string('false'))
- self.assertFalse(strutils.bool_from_string('FALSE'))
- self.assertFalse(strutils.bool_from_string('off'))
- self.assertFalse(strutils.bool_from_string('OFF'))
- self.assertFalse(strutils.bool_from_string('no'))
- self.assertFalse(strutils.bool_from_string('0'))
- self.assertFalse(strutils.bool_from_string('42'))
- self.assertFalse(strutils.bool_from_string('This should not be True'))
+ def _test_bool_from_string(self, c):
+ self.assertTrue(strutils.bool_from_string(c('true')))
+ self.assertTrue(strutils.bool_from_string(c('TRUE')))
+ self.assertTrue(strutils.bool_from_string(c('on')))
+ self.assertTrue(strutils.bool_from_string(c('On')))
+ self.assertTrue(strutils.bool_from_string(c('yes')))
+ self.assertTrue(strutils.bool_from_string(c('YES')))
+ self.assertTrue(strutils.bool_from_string(c('yEs')))
+ self.assertTrue(strutils.bool_from_string(c('1')))
+
+ self.assertFalse(strutils.bool_from_string(c('false')))
+ self.assertFalse(strutils.bool_from_string(c('FALSE')))
+ self.assertFalse(strutils.bool_from_string(c('off')))
+ self.assertFalse(strutils.bool_from_string(c('OFF')))
+ self.assertFalse(strutils.bool_from_string(c('no')))
+ self.assertFalse(strutils.bool_from_string(c('0')))
+ self.assertFalse(strutils.bool_from_string(c('42')))
+ self.assertFalse(strutils.bool_from_string(c(
+ 'This should not be True')))
+
+ def test_bool_from_string(self):
+ self._test_bool_from_string(lambda s: s)
def test_unicode_bool_from_string(self):
- self.assertTrue(strutils.bool_from_string(u'true'))
- self.assertTrue(strutils.bool_from_string(u'TRUE'))
- self.assertTrue(strutils.bool_from_string(u'on'))
- self.assertTrue(strutils.bool_from_string(u'On'))
- self.assertTrue(strutils.bool_from_string(u'yes'))
- self.assertTrue(strutils.bool_from_string(u'YES'))
- self.assertTrue(strutils.bool_from_string(u'yEs'))
- self.assertTrue(strutils.bool_from_string(u'1'))
-
- self.assertFalse(strutils.bool_from_string(u'false'))
- self.assertFalse(strutils.bool_from_string(u'FALSE'))
- self.assertFalse(strutils.bool_from_string(u'off'))
- self.assertFalse(strutils.bool_from_string(u'OFF'))
- self.assertFalse(strutils.bool_from_string(u'no'))
- self.assertFalse(strutils.bool_from_string(u'NO'))
- self.assertFalse(strutils.bool_from_string(u'0'))
- self.assertFalse(strutils.bool_from_string(u'42'))
- self.assertFalse(strutils.bool_from_string(u'This should not be True'))
+ self._test_bool_from_string(six.text_type)
def test_other_bool_from_string(self):
self.assertFalse(strutils.bool_from_string(mock.Mock()))
@@ -76,25 +64,25 @@ class StrUtilsTest(utils.BaseTestCase):
def test_safe_decode(self):
safe_decode = strutils.safe_decode
self.assertRaises(TypeError, safe_decode, True)
- self.assertEqual(u'ni\xf1o', safe_decode("ni\xc3\xb1o",
- incoming="utf-8"))
- self.assertEqual(u"test", safe_decode("dGVzdA==",
- incoming='base64'))
+ self.assertEqual(six.u('ni\xf1o'), safe_decode("ni\xc3\xb1o",
+ incoming="utf-8"))
+ self.assertEqual(six.u("test"), safe_decode("dGVzdA==",
+ incoming='base64'))
- self.assertEqual(u"strange", safe_decode('\x80strange',
- errors='ignore'))
+ self.assertEqual(six.u("strange"), safe_decode('\x80strange',
+ errors='ignore'))
- self.assertEqual(u'\xc0', safe_decode('\xc0',
- incoming='iso-8859-1'))
+ self.assertEqual(six.u('\xc0'), safe_decode('\xc0',
+ incoming='iso-8859-1'))
# Forcing incoming to ascii so it falls back to utf-8
- self.assertEqual(u'ni\xf1o', safe_decode('ni\xc3\xb1o',
- incoming='ascii'))
+ self.assertEqual(six.u('ni\xf1o'), safe_decode('ni\xc3\xb1o',
+ incoming='ascii'))
def test_safe_encode(self):
safe_encode = strutils.safe_encode
self.assertRaises(TypeError, safe_encode, True)
- self.assertEqual("ni\xc3\xb1o", safe_encode(u'ni\xf1o',
+ self.assertEqual("ni\xc3\xb1o", safe_encode(six.u('ni\xf1o'),
encoding="utf-8"))
self.assertEqual("dGVzdA==\n", safe_encode("test",
encoding='base64'))
diff --git a/tests/unit/test_wsgi.py b/tests/unit/test_wsgi.py
index 94d8092..a3a4d32 100644
--- a/tests/unit/test_wsgi.py
+++ b/tests/unit/test_wsgi.py
@@ -21,6 +21,7 @@ import urllib2
import mock
import routes
+import six
import webob
from openstack.common import exception
@@ -189,7 +190,7 @@ class JSONDictSerializerTest(utils.BaseTestCase):
def test_object_unicode(self):
class TestUnicode:
def __unicode__(self):
- return u'TestUnicode'
+ return six.u('TestUnicode')
input_dict = dict(cls=TestUnicode())
expected_str = '{"cls":"TestUnicode"}'
serializer = wsgi.JSONDictSerializer()