forked from p15670423/monkey
Common: Add generic events to PydanticEventSerializer
This commit is contained in:
parent
88d65f40ae
commit
62ab6e5a77
|
@ -1,21 +1,22 @@
|
||||||
import logging
|
import logging
|
||||||
from typing import Type
|
from typing import Type, TypeVar
|
||||||
|
|
||||||
from common.base_models import InfectionMonkeyBaseModel
|
|
||||||
from common.events import AbstractAgentEvent
|
from common.events import AbstractAgentEvent
|
||||||
|
|
||||||
from . import IEventSerializer, JSONSerializable
|
from . import IEventSerializer, JSONSerializable
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
T = TypeVar("T", bound=AbstractAgentEvent)
|
||||||
|
|
||||||
|
|
||||||
class PydanticEventSerializer(IEventSerializer):
|
class PydanticEventSerializer(IEventSerializer):
|
||||||
def __init__(self, event_class: Type[AbstractAgentEvent]):
|
def __init__(self, event_class: Type[T]):
|
||||||
self._event_class = event_class
|
self._event_class = event_class
|
||||||
|
|
||||||
def serialize(self, event: AbstractAgentEvent) -> JSONSerializable:
|
def serialize(self, event: T) -> JSONSerializable:
|
||||||
if not issubclass(event.__class__, self._event_class):
|
if not issubclass(event.__class__, self._event_class):
|
||||||
raise TypeError(f"Event object must be of type: {InfectionMonkeyBaseModel.__name__}")
|
raise TypeError(f"Event object must be of type: {self._event_class.__name__}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return event.dict()
|
return event.dict()
|
||||||
|
@ -24,5 +25,5 @@ class PydanticEventSerializer(IEventSerializer):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def deserialize(self, serialized_event: JSONSerializable) -> AbstractAgentEvent:
|
def deserialize(self, serialized_event: JSONSerializable) -> T:
|
||||||
return self._event_class.parse_obj(serialized_event)
|
return self._event_class(**serialized_event)
|
||||||
|
|
|
@ -2,10 +2,9 @@ from abc import ABC
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pydantic import ValidationError
|
|
||||||
|
|
||||||
from common.base_models import InfectionMonkeyBaseModel
|
from common.base_models import InfectionMonkeyBaseModel
|
||||||
from common.event_serializers import PydanticEventSerializer
|
from common.event_serializers import IEventSerializer, PydanticEventSerializer
|
||||||
from common.events import AbstractAgentEvent
|
from common.events import AbstractAgentEvent
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +24,7 @@ class PydanticEvent(InfectionMonkeyBaseModel):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def pydantic_event_serializer():
|
def pydantic_event_serializer() -> IEventSerializer:
|
||||||
return PydanticEventSerializer(PydanticEvent)
|
return PydanticEventSerializer(PydanticEvent)
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,7 +35,7 @@ def test_pydantic_event_serializer__serialize_wrong_type(pydantic_event_serializ
|
||||||
|
|
||||||
|
|
||||||
def test_pydantic_event_serializer__deserialize_wrong_type(pydantic_event_serializer):
|
def test_pydantic_event_serializer__deserialize_wrong_type(pydantic_event_serializer):
|
||||||
with pytest.raises(ValidationError):
|
with pytest.raises(TypeError):
|
||||||
pydantic_event_serializer.deserialize("bla")
|
pydantic_event_serializer.deserialize("bla")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue