UT: Add type checking tests for EventSerializer Registry

This commit is contained in:
Ilija Lazoroski 2022-08-18 15:22:09 +02:00
parent b541dc465d
commit ff0469690f
1 changed files with 51 additions and 8 deletions

View File

@ -1,6 +1,9 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from unittest.mock import MagicMock
from common.event_serializers import EventSerializerRegistry import pytest
from common.event_serializers import EventSerializerRegistry, IEventSerializer
from common.events import AbstractEvent from common.events import AbstractEvent
@ -14,15 +17,55 @@ class OtherEvent(AbstractEvent):
other_param: float = field(default=123.456) other_param: float = field(default=123.456)
def test_event_serializer_registry(): @dataclass(frozen=True)
class NoneEvent(AbstractEvent):
none_param: float = field(default=1.0)
SOME_SERIALIZER = MagicMock(spec=IEventSerializer)
OTHER_SERIALIZER = MagicMock(spec=IEventSerializer)
@pytest.fixture
def event_serializer_registry():
event_serializer_registry = EventSerializerRegistry() event_serializer_registry = EventSerializerRegistry()
some_event = SomeEvent(some_param=123) event_serializer_registry[SomeEvent] = SOME_SERIALIZER
other_event = OtherEvent() event_serializer_registry[OtherEvent] = OTHER_SERIALIZER
event_serializer_registry[SomeEvent] = some_event return event_serializer_registry
event_serializer_registry[OtherEvent] = other_event
assert event_serializer_registry[some_event.__class__] == some_event
assert event_serializer_registry[other_event.__class__] == other_event def test_event_serializer_registry_event(event_serializer_registry):
assert event_serializer_registry[SomeEvent] == SOME_SERIALIZER
assert event_serializer_registry[OtherEvent] == OTHER_SERIALIZER
def test_event_serializer_registry_string(event_serializer_registry):
assert event_serializer_registry[SomeEvent.__name__] == SOME_SERIALIZER
assert event_serializer_registry[OtherEvent.__name__] == OTHER_SERIALIZER
def test_event_serializer_registry_set_unsupported_type(event_serializer_registry):
with pytest.raises(TypeError):
event_serializer_registry[SomeEvent] = "SomethingBogusVogus"
def test_event_serializer_registry_set_unsupported_type_key(event_serializer_registry):
with pytest.raises(TypeError):
event_serializer_registry["BogusKey"] = MagicMock(spec=IEventSerializer)
def test_event_serializer_registry_get_unsuported_type(event_serializer_registry):
with pytest.raises(TypeError):
event_serializer_registry[1]
def test_event_serializer_registry_get_unexisting_type(event_serializer_registry):
with pytest.raises(KeyError):
event_serializer_registry[NoneEvent]
def test_event_serializer_registry_get_unexisting_string(event_serializer_registry):
with pytest.raises(KeyError):
event_serializer_registry[NoneEvent.__name__]