UT: Fix all unit tests that we creating events to use pydantic

This commit is contained in:
Ilija Lazoroski 2022-09-13 17:35:42 +02:00
parent 1799f1253c
commit 31f3c1a4d6
3 changed files with 10 additions and 14 deletions

View File

@ -1,4 +1,3 @@
from dataclasses import dataclass
from ipaddress import IPv4Address from ipaddress import IPv4Address
from typing import Callable, FrozenSet, Union from typing import Callable, FrozenSet, Union
from uuid import UUID from uuid import UUID
@ -13,7 +12,6 @@ EVENT_TAG_1 = "event tag 1"
EVENT_TAG_2 = "event tag 2" EVENT_TAG_2 = "event tag 2"
@dataclass(frozen=True)
class TestEvent1(AbstractAgentEvent): class TestEvent1(AbstractAgentEvent):
__test__ = False __test__ = False
source: UUID = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5") source: UUID = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5")
@ -22,7 +20,6 @@ class TestEvent1(AbstractAgentEvent):
tags: FrozenSet = frozenset() tags: FrozenSet = frozenset()
@dataclass(frozen=True)
class TestEvent2(AbstractAgentEvent): class TestEvent2(AbstractAgentEvent):
__test__ = False __test__ = False
source: UUID = UUID("e810ad01-6b67-9446-fc58-9b8d717653f7") source: UUID = UUID("e810ad01-6b67-9446-fc58-9b8d717653f7")

View File

@ -1,25 +1,22 @@
from dataclasses import dataclass, field
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from pydantic import Field
from common.event_serializers import EventSerializerRegistry, IEventSerializer from common.event_serializers import EventSerializerRegistry, IEventSerializer
from common.events import AbstractAgentEvent from common.events import AbstractAgentEvent
@dataclass(frozen=True)
class SomeEvent(AbstractAgentEvent): class SomeEvent(AbstractAgentEvent):
some_param: int = field(default=435) some_param: int = Field(default=435)
@dataclass(frozen=True)
class OtherEvent(AbstractAgentEvent): class OtherEvent(AbstractAgentEvent):
other_param: float = field(default=123.456) other_param: float = Field(default=123.456)
@dataclass(frozen=True)
class NoneEvent(AbstractAgentEvent): class NoneEvent(AbstractAgentEvent):
none_param: float = field(default=1.0) none_param: float = Field(default=1.0)
SOME_SERIALIZER = MagicMock(spec=IEventSerializer) SOME_SERIALIZER = MagicMock(spec=IEventSerializer)

View File

@ -1,7 +1,8 @@
from abc import ABC from abc import ABC
from dataclasses import dataclass, field from dataclasses import dataclass
import pytest import pytest
from pydantic import Field
from common.base_models import InfectionMonkeyBaseModel from common.base_models import InfectionMonkeyBaseModel
from common.event_serializers import IEventSerializer, PydanticEventSerializer from common.event_serializers import IEventSerializer, PydanticEventSerializer
@ -14,9 +15,8 @@ class NotAgentEvent(ABC):
other_field: float other_field: float
@dataclass(frozen=True)
class SomeAgentEvent(AbstractAgentEvent): class SomeAgentEvent(AbstractAgentEvent):
bogus: int = field(default_factory=int) bogus: int = Field(default_factory=int)
class PydanticEvent(InfectionMonkeyBaseModel): class PydanticEvent(InfectionMonkeyBaseModel):
@ -28,7 +28,9 @@ def pydantic_event_serializer() -> IEventSerializer:
return PydanticEventSerializer(PydanticEvent) return PydanticEventSerializer(PydanticEvent)
@pytest.mark.parametrize("event", [NotAgentEvent(1, 2.0), SomeAgentEvent(2)]) @pytest.mark.parametrize(
"event", [NotAgentEvent(some_field=1, other_field=2.0), SomeAgentEvent(bogus=2)]
)
def test_pydantic_event_serializer__serialize_wrong_type(pydantic_event_serializer, event): def test_pydantic_event_serializer__serialize_wrong_type(pydantic_event_serializer, event):
with pytest.raises(TypeError): with pytest.raises(TypeError):
pydantic_event_serializer.serialize(event) pydantic_event_serializer.serialize(event)