You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Testing utilities to detect which handlers were called #103
There is a TraceableBus for seeing which messages are passed to a bus, but nothing currently to inspect which handlers were called.
Motivation
Some folk might want to write automated tests to ensure that the correct handler was called for a message.
Possible implementation
Something along the lines of the following, where filename, function, lineno, and timestamp function similarly to the MessageInfo in TraceableBus and reference, handler, name, and request relate to the called handler.
@dataclasses.dataclass(frozen=True)classHandlerInfo[T]:
#: handler reference instancereference: HandlerReference[T]
#: handler objecthandler: Handler[T]
#: module path and class or function name of the handlername: str#: request objectrequest: T#: filename where call originatedfilename: str#: function where call originatedfunction: str#: line in file where call originatedlineno: int#: timestamp of calltimestamp: datetime.datetimeclassTraceableHandlerFactory(banshee.HandlerFactory):
def__init__(self, factory: banshee.HandlerFactory) ->None:
self.factory=factoryself._handlers= []
@propertydefhandlers(self):
returntuple(self._handlers)
def__call__(self, reference: banshee.HandlerReference[T], /) ->banshee.Handler[T]:
handler=self.factory(reference)
@functools.wrap(handler)defwrapper(request: T) ->typing.Any:
self._handlers.append(HandlerInfo(...))
returnhandler(request)
returnwrapper
then in a test something like the following can be used:
changed the title [-]feat: testing utilities to detect which handlers were called[/-][+]Testing utilities to detect which handlers were called[/+]on Apr 4, 2025
Activity
[-]feat: testing utilities to detect which handlers were called[/-][+]Testing utilities to detect which handlers were called[/+]