195 lines
4.2 KiB
Python
195 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
import datetime
|
|
import decimal
|
|
import typing
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
from bthlabs_jsonrpc_core import exceptions, serializer
|
|
|
|
|
|
def test_init():
|
|
# When
|
|
result = serializer.JSONRPCSerializer('spam')
|
|
|
|
# Then
|
|
assert result._data == 'spam'
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value,expected',
|
|
[(None, True), (False, True), (0, True), ('spam', True), ([], False)],
|
|
)
|
|
def test_is_simple_value(value: typing.Any, expected: bool):
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.is_simple_value(value)
|
|
|
|
# Then
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value,expected',
|
|
[
|
|
([], True), ((x for x in [0, 1]), True), (set(), True),
|
|
(tuple(), True), ({}, False),
|
|
],
|
|
)
|
|
def test_is_sequence_value(value: typing.Any, expected: bool):
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.is_sequence_value(value)
|
|
|
|
# Then
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value,expected',
|
|
[({}, True), ([], False)],
|
|
)
|
|
def test_is_dict_value(value: typing.Any, expected: bool):
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.is_dict_value(value)
|
|
|
|
# Then
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value,expected',
|
|
[(uuid.uuid4(), True), (decimal.Decimal('42'), True), ([], False)],
|
|
)
|
|
def test_is_string_coercible_value(value: typing.Any, expected: bool):
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.is_string_coercible_value(value)
|
|
|
|
# Then
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value,expected',
|
|
[
|
|
(
|
|
datetime.datetime(1987, 10, 3, 8, 0, 0, 0, tzinfo=datetime.timezone.utc),
|
|
'1987-10-03T08:00:00+00:00',
|
|
),
|
|
(
|
|
datetime.date(1987, 10, 3),
|
|
'1987-10-03',
|
|
),
|
|
],
|
|
)
|
|
def test_serialize_datetime(value: typing.Any, expected: bool):
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.serialize_datetime(value)
|
|
|
|
# Then
|
|
assert result == expected
|
|
|
|
|
|
def test_serialize_sequence():
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.serialize_sequence([(0, 1), 'spam'])
|
|
|
|
# Then
|
|
assert result == [[0, 1], 'spam']
|
|
|
|
|
|
def test_serialize_dict():
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.serialize_dict(
|
|
{'spam': True, 'eggs': {'key': decimal.Decimal('42')}},
|
|
)
|
|
|
|
# Then
|
|
assert result == {'spam': True, 'eggs': {'key': '42'}}
|
|
|
|
|
|
def test_serialize_string_coercible():
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.serialize_string_coercible(decimal.Decimal('42'))
|
|
|
|
# Then
|
|
assert result == '42'
|
|
|
|
|
|
def test_serialize_value():
|
|
# Given
|
|
value = [
|
|
serializer.JSONRPCSerializer('spam'),
|
|
'eggs',
|
|
datetime.datetime(1987, 10, 3, 8, 0, 0, 0, tzinfo=datetime.timezone.utc),
|
|
[0, 1],
|
|
{'spam': True},
|
|
decimal.Decimal('42'),
|
|
exceptions.BaseJSONRPCError(),
|
|
]
|
|
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
result = the_serializer.serialize_value(value)
|
|
|
|
# Then
|
|
expected = [
|
|
'spam',
|
|
'eggs',
|
|
'1987-10-03T08:00:00+00:00',
|
|
[0, 1],
|
|
{'spam': True},
|
|
'42',
|
|
exceptions.BaseJSONRPCError().to_rpc(),
|
|
]
|
|
assert result == expected
|
|
|
|
|
|
def test_serialize_value_no_to_rpc():
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer('spam')
|
|
|
|
# When
|
|
try:
|
|
_ = the_serializer.serialize_value(object())
|
|
except Exception as exception:
|
|
# Then
|
|
assert isinstance(exception, exceptions.JSONRPCSerializerError)
|
|
|
|
|
|
def test_data():
|
|
# Given
|
|
the_serializer = serializer.JSONRPCSerializer(decimal.Decimal('42'))
|
|
|
|
# When
|
|
result = the_serializer.data
|
|
|
|
# Then
|
|
assert result == '42'
|
|
|
|
assert the_serializer._serialized_data == '42'
|