Source code for edri.testing
"""Testing helpers for EDRI application tests.
This module provides lightweight helpers for writing unit tests against
Manager handlers without launching child processes. It wraps internal setup
steps so application tests can use public API only.
``ManagerTestHarness`` is **Experimental** — it may be extended in minor
releases but breaking changes will follow the project's compatibility policy.
"""
from typing import cast
from edri.abstract import ManagerBase
from edri.dataclass.directive import ResponseDirective
from edri.dataclass.event import Event
from edri.dataclass.response import Response, ResponseStatus
__all__ = ["ManagerTestHarness"]
[docs]
class ManagerTestHarness:
"""Wraps a Manager instance for unit testing.
Usage::
harness = ManagerTestHarness(MyManager(router_queue=mock_queue))
harness.prepare()
event = MyEvent(...)
harness.resolve(event)
assert harness.get_status(event) == ResponseStatus.OK
assert harness.get_directives(event) == []
All assertions use public API so tests survive internal refactoring.
"""
def __init__(self, manager: ManagerBase) -> None:
self._manager = manager
[docs]
def prepare(self) -> None:
"""Set up resolver maps so that ``resolve()`` dispatches correctly.
Must be called before the first ``resolve()`` call (equivalent to
what ``_prepare_resolvers()`` does internally).
"""
self._manager._prepare_resolvers()
[docs]
def resolve(self, event: Event) -> None:
"""Dispatch *event* through the manager's handler.
The manager's ``router_queue`` should be a mock for unit tests;
real queues are only needed for integration tests.
"""
self._manager.resolve(event)
[docs]
@staticmethod
def get_status(event: Event) -> ResponseStatus:
"""Return the response status after resolving *event*."""
return cast(Response, event.response).get_status()
[docs]
@staticmethod
def get_directives(event: Event) -> list[ResponseDirective]:
"""Return the directives attached to *event*'s response."""
return cast(Response, event.response).get_directives()
@property
def manager(self) -> ManagerBase:
return self._manager