-
Notifications
You must be signed in to change notification settings - Fork 464
/
test_serializer.py
88 lines (65 loc) · 2.51 KB
/
test_serializer.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
84
85
86
87
88
import sys
import pytest
from sentry_sdk.serializer import serialize
try:
from hypothesis import given
import hypothesis.strategies as st
except ImportError:
pass
else:
def test_bytes_serialization_decode_many(message_normalizer):
@given(binary=st.binary(min_size=1))
def inner(binary):
result = message_normalizer(binary, should_repr_strings=False)
assert result == binary.decode("utf-8", "replace")
inner()
def test_bytes_serialization_repr_many(message_normalizer):
@given(binary=st.binary(min_size=1))
def inner(binary):
result = message_normalizer(binary, should_repr_strings=True)
assert result == repr(binary)
inner()
@pytest.fixture
def message_normalizer(validate_event_schema):
def inner(message, **kwargs):
event = serialize({"logentry": {"message": message}}, **kwargs)
validate_event_schema(event)
return event["logentry"]["message"]
return inner
@pytest.fixture
def extra_normalizer(validate_event_schema):
def inner(message, **kwargs):
event = serialize({"extra": {"foo": message}}, **kwargs)
validate_event_schema(event)
return event["extra"]["foo"]
return inner
def test_bytes_serialization_decode(message_normalizer):
binary = b"abc123\x80\xf0\x9f\x8d\x95"
result = message_normalizer(binary, should_repr_strings=False)
# fmt: off
assert result == u"abc123\ufffd\U0001f355"
# fmt: on
@pytest.mark.xfail(sys.version_info < (3,), reason="Known safe_repr bugs in Py2.7")
def test_bytes_serialization_repr(message_normalizer):
binary = b"abc123\x80\xf0\x9f\x8d\x95"
result = message_normalizer(binary, should_repr_strings=True)
assert result == r"b'abc123\x80\xf0\x9f\x8d\x95'"
def test_serialize_sets(extra_normalizer):
result = extra_normalizer({1, 2, 3})
assert result == [1, 2, 3]
def test_serialize_custom_mapping(extra_normalizer):
class CustomReprDict(dict):
def __sentry_repr__(self):
return "custom!"
result = extra_normalizer(CustomReprDict(one=1, two=2))
assert result == "custom!"
def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer):
"""
Adding the __sentry_repr__ magic method check in the serializer
shouldn't mess with how mock works. This broke some stuff when we added
sentry_repr without the dunders.
"""
mock = pytest.importorskip("unittest.mock")
m = mock.Mock()
extra_normalizer(m)
assert len(m.mock_calls) == 0