forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_serializer.py
More file actions
86 lines (63 loc) · 2.64 KB
/
Copy pathtest_serializer.py
File metadata and controls
86 lines (63 loc) · 2.64 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
from datetime import datetime
import sys
import pytest
from sentry_sdk.serializer import serialize
try:
from hypothesis import given, example
import hypothesis.strategies as st
except ImportError:
pass
else:
@given(
dt=st.datetimes(
min_value=datetime(2000, 1, 1, 0, 0, 0), timezones=st.just(None)
)
)
@example(dt=datetime(2001, 1, 1, 0, 0, 0, 999500))
def test_datetime_precision(dt, relay_normalize):
event = serialize({"timestamp": dt})
normalized = relay_normalize(event)
if normalized is None:
pytest.skip("no relay available")
dt2 = datetime.utcfromtimestamp(normalized["timestamp"])
# Float glitches can happen, and more glitches can happen
# because we try to work around some float glitches in relay
assert (dt - dt2).total_seconds() < 1.0
@given(binary=st.binary(min_size=1))
def test_bytes_serialization_decode_many(binary, message_normalizer):
result = message_normalizer(binary, should_repr_strings=False)
assert result == binary.decode("utf-8", "replace")
@given(binary=st.binary(min_size=1))
def test_bytes_serialization_repr_many(binary, message_normalizer):
result = message_normalizer(binary, should_repr_strings=True)
assert result == repr(binary)
@pytest.fixture
def message_normalizer(relay_normalize):
if relay_normalize({"test": "test"}) is None:
pytest.skip("no relay available")
def inner(message, **kwargs):
event = serialize({"logentry": {"message": message}}, **kwargs)
normalized = relay_normalize(event)
return normalized["logentry"]["message"]
return inner
@pytest.fixture
def extra_normalizer(relay_normalize):
if relay_normalize({"test": "test"}) is None:
pytest.skip("no relay available")
def inner(message, **kwargs):
event = serialize({"extra": {"foo": message}}, **kwargs)
normalized = relay_normalize(event)
return normalized["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)
assert result == u"abc123\ufffd\U0001f355"
@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]