/
test_encoding.py
83 lines (58 loc) · 2.25 KB
/
test_encoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import with_statement
import sys
from contextlib import contextmanager
from mock import patch
from nose import SkipTest
from kombu.utils.encoding import safe_str
from kombu.tests.utils import TestCase
@contextmanager
def clean_encoding():
old_encoding = sys.modules.pop('kombu.utils.encoding', None)
import kombu.utils.encoding
yield kombu.utils.encoding
if old_encoding:
sys.modules['kombu.utils.encoding'] = old_encoding
class test_default_encoding(TestCase):
@patch('sys.getfilesystemencoding')
def test_default(self, getfilesystemencoding):
getfilesystemencoding.return_value = 'ascii'
with clean_encoding() as encoding:
enc = encoding.default_encoding()
if sys.platform.startswith('java'):
self.assertEqual(enc, 'utf-8')
else:
self.assertEqual(enc, 'ascii')
getfilesystemencoding.assert_called_with()
class test_encoding_utils(TestCase):
def setUp(self):
if sys.version_info >= (3, 0):
raise SkipTest('not relevant on py3k')
def test_str_to_bytes(self):
with clean_encoding() as e:
self.assertIsInstance(e.str_to_bytes(u'foobar'), str)
self.assertIsInstance(e.str_to_bytes('foobar'), str)
def test_from_utf8(self):
with clean_encoding() as e:
self.assertIsInstance(e.from_utf8(u'foobar'), str)
def test_default_encode(self):
with clean_encoding() as e:
self.assertTrue(e.default_encode('foo'))
class test_safe_str(TestCase):
def test_when_str(self):
self.assertEqual(safe_str('foo'), 'foo')
def test_when_unicode(self):
self.assertIsInstance(safe_str(u'foo'), str)
def test_when_containing_high_chars(self):
s = u'The quiæk fåx jømps øver the lazy dåg'
res = safe_str(s)
self.assertIsInstance(res, str)
def test_when_not_string(self):
o = object()
self.assertEqual(safe_str(o), repr(o))
def test_when_unrepresentable(self):
class O(object):
def __repr__(self):
raise KeyError('foo')
self.assertIn('<Unrepresentable', safe_str(O()))