93 lines
2.7 KiB
Python
93 lines
2.7 KiB
Python
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from infrasynth.shared.protocols import (
|
|
AuditableProtocol,
|
|
EventProtocol,
|
|
TenantProtocol,
|
|
)
|
|
|
|
|
|
class TestAuditableProtocol:
|
|
def test_matching_class_is_compatible(self):
|
|
class MyModel:
|
|
pk: Any = 1
|
|
usuario_creacion: Any = None
|
|
fecha_creacion: datetime | None = datetime.now()
|
|
usuario_actualizacion: Any = None
|
|
fecha_actualizacion: datetime | None = datetime.now()
|
|
|
|
assert isinstance(MyModel(), AuditableProtocol)
|
|
|
|
def test_non_matching_class_is_not_compatible(self):
|
|
class NotAuditable:
|
|
pass
|
|
|
|
assert not isinstance(NotAuditable(), AuditableProtocol)
|
|
|
|
def test_partial_match_is_not_compatible(self):
|
|
class Partial:
|
|
pk: Any = 1
|
|
|
|
assert not isinstance(Partial(), AuditableProtocol)
|
|
|
|
|
|
class TestEventProtocol:
|
|
def test_matching_class_is_compatible(self):
|
|
class MyEvent:
|
|
event_name: str = "ticket.created"
|
|
payload: dict = {"id": 1}
|
|
timestamp: str = "2026-01-01T00:00:00Z"
|
|
|
|
assert isinstance(MyEvent(), EventProtocol)
|
|
|
|
def test_non_matching_class_is_not_compatible(self):
|
|
class NotEvent:
|
|
pass
|
|
|
|
assert not isinstance(NotEvent(), EventProtocol)
|
|
|
|
def test_partial_match_is_not_compatible(self):
|
|
class PartialEvent:
|
|
event_name: str = "test"
|
|
|
|
assert not isinstance(PartialEvent(), EventProtocol)
|
|
|
|
|
|
class TestTenantProtocol:
|
|
def test_matching_class_is_compatible(self):
|
|
class MyTenant:
|
|
tenant_id: str | None = "tenant-123"
|
|
|
|
assert isinstance(MyTenant(), TenantProtocol)
|
|
|
|
def test_tenant_id_none_is_still_valid(self):
|
|
class NoTenant:
|
|
tenant_id: str | None = None
|
|
|
|
assert isinstance(NoTenant(), TenantProtocol)
|
|
|
|
def test_non_matching_class_is_not_compatible(self):
|
|
class NoTenantModel:
|
|
pass
|
|
|
|
assert not isinstance(NoTenantModel(), TenantProtocol)
|
|
|
|
|
|
class TestCombinedProtocols:
|
|
def test_class_can_satisfy_multiple_protocols(self):
|
|
class FullModel:
|
|
pk: Any = 1
|
|
usuario_creacion: Any = None
|
|
fecha_creacion: datetime | None = datetime.now()
|
|
usuario_actualizacion: Any = None
|
|
fecha_actualizacion: datetime | None = datetime.now()
|
|
event_name: str = "test"
|
|
payload: dict = {}
|
|
timestamp: str = "2026-01-01T00:00:00Z"
|
|
tenant_id: str | None = "t1"
|
|
|
|
obj = FullModel()
|
|
assert isinstance(obj, AuditableProtocol)
|
|
assert isinstance(obj, EventProtocol)
|
|
assert isinstance(obj, TenantProtocol)
|