async def test_handler_factory_cascade_replace(): cascade = HandlerFactoryCascade(policies=[CallableHandlerPolicy()]) obj = _A() with pytest.raises(IncompatibleHandlerFactoryCascadeError): cascade(obj) cascade = cascade.replace(policies=[MethodHandlerPolicy(name="method")]) handler = cascade(obj) await _check_handler_works(handler)
def test_method_handler_policy(): policy = MethodHandlerPolicy(name="test", subtype_of=[str]) assert policy.precises_type policy = MethodHandlerPolicy(name="test") assert not policy.precises_type callable_policy = CallableHandlerPolicy() new_policy = policy.replace_map(callable_policy) assert isinstance(new_policy, MethodHandlerPolicy) assert new_policy.policy is callable_policy assert new_policy.name == policy.name assert list(new_policy.subtype_of) == list(policy.subtype_of) second_policy = MethodHandlerPolicy(name="other") assert policy.replace_map(second_policy) is second_policy assert policy.replace_map(None) is policy
async def test_handler_factory_cascade_success(obj): cascade = HandlerFactoryCascade( policies=[CallableHandlerPolicy(), MethodHandlerPolicy(name="method")]) handler = cascade(obj) await _check_handler_works(handler)
assert isinstance(error, IncompatibleHandlerFactoryCascadeError) factory_errors = error.factory_errors assert len(factory_errors) == 2 for (factory, factory_error), factory_type in zip( factory_errors, [MethodHandlerFactory, CallableHandlerFactory]): assert isinstance(factory, factory_type) assert isinstance(factory_error, IncompatibleHandlerFactoryError) @pytest.mark.parametrize( "obj, policies, error_check", [ (None, [], _check_config_error), (_A(), [MethodHandlerPolicy(name="method_no_type") ], _check_inspect_error), ( _A(), [ MethodHandlerPolicy(name="non_existing"), CallableHandlerPolicy() ], _check_incompatible_error, ), ], ) def test_handler_factory_cascade_error( obj, policies: Sequence[PolicyType], error_check: Callable[[HandlerFactoryCascadeError], Any],
from dataclasses import dataclass import pytest from mediator.common.factory import CallableHandlerPolicy, MethodHandlerPolicy from mediator.request import LocalRequestBus, RequestHandlerRegistry # Create local request registry with given policies. # Single policy tells the handler inspector where to find callable # which handles particular action. # - add policy that suggest to get object attribute named "execute" # and try to analyze as request handler # - add policy that suggest given object is request handler callable registry = RequestHandlerRegistry( policies=[MethodHandlerPolicy(name="execute"), CallableHandlerPolicy()], ) @dataclass class PrintCommand: message: str class PrintCommandHandler: # noinspection PyMethodMayBeStatic async def execute(self, command: PrintCommand): print(f"print: {command.message}") return command.message # ... MethodHandlerPolicy(...) gives ability to inspect handler object
def _registry_policies_factory(): return HandlerRegistry( store=CollectionHandlerStore(), policies=[CallableHandlerPolicy()], ) @pytest.mark.parametrize( "registry_factory", [_registry_cascade_factory, _registry_policies_factory], ) @pytest.mark.parametrize( "policies, incompatible, compatible", [ (None, _A(), _handle), ([MethodHandlerPolicy(name="handle")], _handle, _A()), ], ) @pytest.mark.asyncio async def test_handler_registry_policy( registry_factory: Callable[[], HandlerRegistry], incompatible, compatible, policies: Sequence[PolicyType], mockup_action_factory: Callable[[], ActionSubject], ): registry = registry_factory() with pytest.raises(IncompatibleHandlerFactoryCascadeError): registry.register(incompatible, policies=policies)
from dataclasses import dataclass import pytest from mediator.common.factory import CallableHandlerPolicy, MethodHandlerPolicy from mediator.event import EventHandlerRegistry, LocalEventBus # Create local event registry with given policies. # Single policy tells the handler inspector where to find callable # which handles particular action. # - add policy that suggest to get object attribute named "handle" # and try to analyze as event handler # - add policy that suggest given object is event handler callable registry = EventHandlerRegistry( policies=[MethodHandlerPolicy(name="handle"), CallableHandlerPolicy()], ) @dataclass class MessageEvent: message: str class MessageEventHandler: async def handle(self, event: MessageEvent): print(f"from object handler: {event.message}") # ... MethodHandlerPolicy(...) gives ability to inspect handler object handler = MessageEventHandler()
"y": "b" }, arg_strict=True)), (_A().c, CallableHandlerPolicy(arg_map={"x": "a"})), ], ) @pytest.mark.asyncio async def test_callable_handler_factory(obj, policy: CallableHandlerPolicy): factory = CallableHandlerFactory(policy) handler = factory.create(obj) await _check_handler(handler) @pytest.mark.parametrize( "obj, policy", [ (_A(), MethodHandlerPolicy(name="a")), ( _A(), MethodHandlerPolicy( name="b", subtype_of=[_Base], policy=CallableHandlerPolicy(arg_map={ "x": "a", "y": "b" }), ), ), ], ) @pytest.mark.asyncio async def test_method_handler_factory(obj, policy: MethodHandlerPolicy):