|
1 | 1 | from datetime import datetime |
2 | | - |
| 2 | +import sys |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
@@ -30,3 +30,39 @@ def test_datetime_precision(dt, semaphore_normalize): |
30 | 30 | # Float glitches can happen, and more glitches can happen |
31 | 31 | # because we try to work around some float glitches in semaphore |
32 | 32 | assert (dt - dt2).total_seconds() < 1.0 |
| 33 | + |
| 34 | + @given(binary=st.binary(min_size=1)) |
| 35 | + def test_bytes_serialization_decode_many(binary, message_normalizer): |
| 36 | + result = message_normalizer(binary, should_repr_strings=False) |
| 37 | + assert result == binary.decode("utf-8", "replace") |
| 38 | + |
| 39 | + @given(binary=st.binary(min_size=1)) |
| 40 | + def test_bytes_serialization_repr_many(binary, message_normalizer): |
| 41 | + result = message_normalizer(binary, should_repr_strings=True) |
| 42 | + assert result == repr(binary) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def message_normalizer(semaphore_normalize): |
| 47 | + if semaphore_normalize({"test": "test"}) is None: |
| 48 | + pytest.skip("no semaphore available") |
| 49 | + |
| 50 | + def inner(message, **kwargs): |
| 51 | + event = serialize({"logentry": {"message": message}}, **kwargs) |
| 52 | + normalized = semaphore_normalize(event) |
| 53 | + return normalized["logentry"]["message"] |
| 54 | + |
| 55 | + return inner |
| 56 | + |
| 57 | + |
| 58 | +def test_bytes_serialization_decode(message_normalizer): |
| 59 | + binary = b"abc123\x80\xf0\x9f\x8d\x95" |
| 60 | + result = message_normalizer(binary, should_repr_strings=False) |
| 61 | + assert result == u"abc123\ufffd\U0001f355" |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.xfail(sys.version_info < (3,), reason="Known safe_repr bugs in Py2.7") |
| 65 | +def test_bytes_serialization_repr(message_normalizer): |
| 66 | + binary = b"abc123\x80\xf0\x9f\x8d\x95" |
| 67 | + result = message_normalizer(binary, should_repr_strings=True) |
| 68 | + assert result == r"b'abc123\x80\xf0\x9f\x8d\x95'" |
0 commit comments