1
0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/codecs_/test_DjangoJSONCodec.py

74 lines
1.7 KiB
Python
Raw Normal View History

2024-01-15 20:20:10 +00:00
# -*- coding: utf-8 -*-
# type: ignore
import decimal
import datetime
from unittest import mock
import uuid
from bthlabs_jsonrpc_django import codecs
from django.core.serializers.json import DjangoJSONEncoder
import pytest
@pytest.fixture
def payload() -> dict:
return {
'str': 'This is a string',
'int': 42,
'float': 3.14,
'decimal': decimal.Decimal('2.71828'),
'datetime': datetime.datetime(2021, 1, 19, 8, 0, 0),
'date': datetime.date(2022, 8, 25),
'uuid': uuid.UUID('{ab3eacec-e205-413d-b900-940e14f61518}'),
}
def test_encode(payload: dict):
# Given
codec = codecs.DjangoJSONCodec()
# When
result = codec.encode(payload)
# Then
expected_result = (
'{'
'"str": "This is a string", '
'"int": 42, '
'"float": 3.14, '
'"decimal": "2.71828", '
'"datetime": "2021-01-19T08:00:00", '
'"date": "2022-08-25", '
'"uuid": "ab3eacec-e205-413d-b900-940e14f61518"'
'}'
)
assert result == expected_result
def test_encode_super_encode_call(payload: dict):
# Given
codec = codecs.DjangoJSONCodec()
with mock.patch.object(codecs.JSONCodec, 'encode') as mock_super_encode:
# When
_ = codec.encode(payload)
# Then
mock_super_encode.assert_called_once_with(
payload, cls=DjangoJSONEncoder,
)
def test_encode_super_encode_call_encoder_kwargs(payload: dict):
# Given
codec = codecs.DjangoJSONCodec()
with mock.patch.object(codecs.JSONCodec, 'encode') as mock_super_encode:
# When
_ = codec.encode(payload, cls=None)
# Then
mock_super_encode.assert_called_once_with(
payload, cls=None,
)