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
103
104
|
import sys
from contextlib import contextmanager
from case import patch
from kombu.utils.encoding import (
get_default_encoding_file, safe_str,
set_default_encoding_file, default_encoding,
)
@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:
def test_set_default_file(self):
prev = get_default_encoding_file()
try:
set_default_encoding_file('/foo.txt')
assert get_default_encoding_file() == '/foo.txt'
finally:
set_default_encoding_file(prev)
@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'):
assert enc == 'utf-8'
else:
assert enc == 'ascii'
getdefaultencoding.assert_called_with()
class newbytes(bytes):
"""Mock class to simulate python-future newbytes class"""
def __repr__(self):
return 'b' + super().__repr__()
def __str__(self):
return 'b' + f"'{super().__str__()}'"
class newstr(str):
"""Mock class to simulate python-future newstr class"""
def encode(self, encoding=None, errors=None):
return newbytes(super().encode(encoding, errors))
class test_safe_str:
def setup(self):
self._encoding = self.patching('sys.getfilesystemencoding')
self._encoding.return_value = 'ascii'
def test_when_bytes(self):
assert safe_str('foo') == 'foo'
def test_when_newstr(self):
"""Simulates using python-future package under 2.7"""
assert str(safe_str(newstr('foo'))) == 'foo'
def test_when_unicode(self):
assert isinstance(safe_str('foo'), str)
def test_when_encoding_utf8(self):
self._encoding.return_value = 'utf-8'
assert default_encoding() == 'utf-8'
s = 'The quiæk fåx jømps øver the lazy dåg'
res = safe_str(s)
assert isinstance(res, str)
def test_when_containing_high_chars(self):
self._encoding.return_value = 'ascii'
s = 'The quiæk fåx jømps øver the lazy dåg'
res = safe_str(s)
assert isinstance(res, str)
assert len(s) == len(res)
def test_when_not_string(self):
o = object()
assert safe_str(o) == repr(o)
def test_when_unrepresentable(self):
class UnrepresentableObject:
def __repr__(self):
raise KeyError('foo')
assert '<Unrepresentable' in safe_str(UnrepresentableObject())
|