diff options
Diffstat (limited to 't/unit/utils/test_json.py')
| -rw-r--r-- | t/unit/utils/test_json.py | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/t/unit/utils/test_json.py b/t/unit/utils/test_json.py index 6af1c13b..8dcc7e32 100644 --- a/t/unit/utils/test_json.py +++ b/t/unit/utils/test_json.py @@ -1,14 +1,17 @@ +from __future__ import annotations + +import uuid from collections import namedtuple from datetime import datetime from decimal import Decimal -from unittest.mock import MagicMock, Mock -from uuid import uuid4 import pytest import pytz +from hypothesis import given, settings +from hypothesis import strategies as st from kombu.utils.encoding import str_to_bytes -from kombu.utils.json import _DecodeError, dumps, loads +from kombu.utils.json import dumps, loads class Custom: @@ -21,35 +24,54 @@ class Custom: class test_JSONEncoder: - + @pytest.mark.freeze_time("2015-10-21") def test_datetime(self): now = datetime.utcnow() now_utc = now.replace(tzinfo=pytz.utc) - stripped = datetime(*now.timetuple()[:3]) - serialized = loads(dumps({ + + original = { 'datetime': now, 'tz': now_utc, 'date': now.date(), - 'time': now.time()}, - )) + 'time': now.time(), + } + + serialized = loads(dumps(original)) + + assert serialized == original + + @given(message=st.binary()) + @settings(print_blob=True) + def test_binary(self, message): + serialized = loads(dumps({ + 'args': (message,), + })) assert serialized == { - 'datetime': now.isoformat(), - 'tz': '{}Z'.format(now_utc.isoformat().split('+', 1)[0]), - 'time': now.time().isoformat(), - 'date': stripped.isoformat(), + 'args': [message], } def test_Decimal(self): - d = Decimal('3314132.13363235235324234123213213214134') - assert loads(dumps({'d': d})), {'d': str(d)} + original = {'d': Decimal('3314132.13363235235324234123213213214134')} + serialized = loads(dumps(original)) + + assert serialized == original def test_namedtuple(self): Foo = namedtuple('Foo', ['bar']) assert loads(dumps(Foo(123))) == [123] def test_UUID(self): - id = uuid4() - assert loads(dumps({'u': id})), {'u': str(id)} + constructors = [ + uuid.uuid1, + lambda: uuid.uuid3(uuid.NAMESPACE_URL, "https://example.org"), + uuid.uuid4, + lambda: uuid.uuid5(uuid.NAMESPACE_URL, "https://example.org"), + ] + for constructor in constructors: + id = constructor() + loaded_value = loads(dumps({'u': id})) + assert loaded_value == {'u': id} + assert loaded_value["u"].version == id.version def test_default(self): with pytest.raises(TypeError): @@ -81,9 +103,3 @@ class test_dumps_loads: assert loads( str_to_bytes(dumps({'x': 'z'})), decode_bytes=True) == {'x': 'z'} - - def test_loads_DecodeError(self): - _loads = Mock(name='_loads') - _loads.side_effect = _DecodeError( - MagicMock(), MagicMock(), MagicMock()) - assert loads(dumps({'x': 'z'}), _loads=_loads) == {'x': 'z'} |
