/usr/lib/python2.7/dist-packages/kombu/tests/utils/test_encoding.py is in python-kombu 3.0.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | # -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import sys
from contextlib import contextmanager
from kombu.five import bytes_t, string_t
from kombu.utils.encoding import safe_str, default_encoding
from kombu.tests.case import Case, SkipTest, patch
@contextmanager
def clean_encoding():
old_encoding = sys.modules.pop('kombu.utils.encoding', None)
import kombu.utils.encoding
try:
yield kombu.utils.encoding
finally:
if old_encoding:
sys.modules['kombu.utils.encoding'] = old_encoding
class test_default_encoding(Case):
@patch('sys.getfilesystemencoding')
def test_default(self, getdefaultencoding):
getdefaultencoding.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')
getdefaultencoding.assert_called_with()
class test_encoding_utils(Case):
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('foobar'), bytes_t)
def test_from_utf8(self):
with clean_encoding() as e:
self.assertIsInstance(e.from_utf8('foobar'), bytes_t)
def test_default_encode(self):
with clean_encoding() as e:
self.assertTrue(e.default_encode(b'foo'))
class test_safe_str(Case):
def setUp(self):
self._cencoding = patch('sys.getfilesystemencoding')
self._encoding = self._cencoding.__enter__()
self._encoding.return_value = 'ascii'
def tearDown(self):
self._cencoding.__exit__()
def test_when_bytes(self):
self.assertEqual(safe_str('foo'), 'foo')
def test_when_unicode(self):
self.assertIsInstance(safe_str('foo'), string_t)
def test_when_encoding_utf8(self):
with patch('sys.getfilesystemencoding') as encoding:
encoding.return_value = 'utf-8'
self.assertEqual(default_encoding(), 'utf-8')
s = 'The quiæk fåx jømps øver the lazy dåg'
res = safe_str(s)
self.assertIsInstance(res, str)
def test_when_containing_high_chars(self):
with patch('sys.getfilesystemencoding') as encoding:
encoding.return_value = 'ascii'
s = 'The quiæk fåx jømps øver the lazy dåg'
res = safe_str(s)
self.assertIsInstance(res, str)
self.assertEqual(len(s), len(res))
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()))
|