This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathtests.py
More file actions
202 lines (161 loc) · 6.11 KB
/
tests.py
File metadata and controls
202 lines (161 loc) · 6.11 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
import pytest
import uuid
from raven.utils import compat
from raven.utils import json
from raven.utils.testutils import TestCase
from raven.utils.serializer import transform
class TransformTest(TestCase):
@pytest.mark.skipif('compat.PY3')
def test_incorrect_unicode(self):
x = compat.b('רונית מגן')
result = transform(x)
assert result == compat.b("'רונית מגן'")
@pytest.mark.skipif('compat.PY3')
def test_truncating_unicode(self):
# 'רונית מגן'
x = compat.u('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df')
result = transform(x, string_max_length=5)
assert result == compat.u("u'\u05e8\u05d5\u05e0\u05d9\u05ea'")
@pytest.mark.skipif('not compat.PY3')
def test_unicode_in_python3(self):
# 'רונית מגן'
x = compat.u('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df')
result = transform(x)
assert result == compat.u("'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'")
@pytest.mark.skipif('compat.PY3')
def test_unicode_in_python2(self):
# 'רונית מגן'
x = compat.u('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df')
result = transform(x)
assert result == compat.u("u'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'")
@pytest.mark.skipif('not compat.PY3')
def test_string_in_python3(self):
# 'רונית מגן'
x = compat.b('hello world')
result = transform(x)
assert result == "b'hello world'"
@pytest.mark.skipif('compat.PY3')
def test_string_in_python2(self):
# 'רונית מגן'
x = compat.b('hello world')
result = transform(x)
assert result == "'hello world'"
@pytest.mark.skipif('compat.PY3')
def test_bad_string(self):
x = compat.b('The following character causes problems: \xd4')
result = transform(x)
assert result == compat.b("'The following character causes problems: \\xd4'")
def test_float(self):
result = transform(13.0)
self.assertEqual(type(result), float)
self.assertEqual(result, 13.0)
def test_bool(self):
result = transform(True)
self.assertEqual(type(result), bool)
self.assertEqual(result, True)
def test_int_subclass(self):
class X(int):
pass
result = transform(X())
self.assertEqual(type(result), int)
self.assertEqual(result, 0)
def test_dict_keys(self):
x = {'foo': 'bar'}
result = transform(x)
self.assertEqual(type(result), dict)
keys = list(result.keys())
self.assertEqual(len(keys), 1)
self.assertTrue(type(keys[0]), str)
self.assertEqual(keys[0], "'foo'")
@pytest.mark.skipif('compat.PY3')
def test_dict_keys_utf8_as_str(self):
x = {'רונית מגן': 'bar'}
result = transform(x)
self.assertEqual(type(result), dict)
keys = list(result.keys())
self.assertEqual(len(keys), 1)
assert keys[0] == "'רונית מגן'"
def test_dict_keys_utf8_as_unicode(self):
x = {
compat.text_type('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'): 'bar'
}
result = transform(x)
assert type(result) is dict
keys = list(result.keys())
assert len(keys) == 1
if compat.PY3:
expected = "'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'"
else:
expected = "u'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'"
assert keys[0] == expected
def test_uuid(self):
x = uuid.uuid4()
result = transform(x)
assert result == repr(x)
@pytest.mark.skipif('compat.PY3')
def test_recurse_exception(self):
class NonAsciiRepr(object):
def __repr__(self):
return compat.b('中文')
x = [NonAsciiRepr()]
result = transform(x, max_depth=1)
self.assertEqual(json.dumps(result), compat.b('["<class \'tests.utils.encoding.tests.NonAsciiRepr\'>"]'))
def test_recursive(self):
x = []
x.append(x)
result = transform(x)
self.assertEqual(result, ('<...>',))
def test_custom_repr(self):
class Foo(object):
def __sentry__(self):
return compat.u('example')
x = Foo()
result = transform(x)
if compat.PY3:
expected = "'example'"
else:
expected = "u'example'"
self.assertEqual(result, expected)
def test_broken_repr(self, caplog):
class Foo(object):
def __repr__(self):
raise ValueError
obj = Foo()
result = transform(obj)
import sys
if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
expected = "<class 'tests.utils.encoding.tests.TransformTest.test_broken_repr.<locals>.Foo'>"
else:
expected = "<class 'tests.utils.encoding.tests.Foo'>"
expected += ' (%s)' % (id(obj),)
assert result == expected
def test_recursion_max_depth(self):
x = [[[[1]]]]
result = transform(x, max_depth=3)
if compat.PY3:
expected = ((("'[1]'",),),)
else:
expected = ((("u'[1]'",),),)
self.assertEqual(result, expected)
def test_list_max_length(self):
x = list(range(10))
result = transform(x, list_max_length=3)
self.assertEqual(result, (0, 1, 2))
def test_dict_max_length(self):
x = dict((x, x) for x in range(10))
result = transform(x, list_max_length=3)
self.assertEqual(type(x), dict)
self.assertEqual(len(result), 3)
def test_string_max_length(self):
x = compat.u('1234')
result = transform(x, string_max_length=3)
expected = "'123'" if compat.PY3 else "u'123'"
self.assertEqual(result, expected)
def test_bytes_max_length(self):
x = compat.b('\xd7\xd7\xd7\xd7\xd7\xd7')
result = transform(x, string_max_length=1)
if compat.PY3:
assert result == "b'\\xd7'"
else:
assert result == "'\\xd7'"