Skip to content

Commit

Permalink
Registry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GitRon committed Jan 24, 2025
1 parent 238ce03 commit a7bed16
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 46 deletions.
16 changes: 4 additions & 12 deletions queuebie/registry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import contextlib
import importlib
import os
from functools import wraps
from pathlib import Path
from typing import Type

from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -33,7 +33,7 @@ def __new__(cls, *args, **kwargs):
cls._instance = super().__new__(cls)
return cls._instance

def register_command(self, *, command: Command):
def register_command(self, *, command: Type[Command]):
def decorator(decoratee):
# Ensure that registered message is of correct type
if not (issubclass(command, Command)):
Expand All @@ -50,7 +50,7 @@ def decorator(decoratee):

return decorator

def register_event(self, *, event: Event):
def register_event(self, *, event: Type[Event]):
def decorator(decoratee):
# Ensure that registered message is of correct type
if not (issubclass(event, Event)):
Expand All @@ -67,15 +67,6 @@ def decorator(decoratee):

return decorator

def inject(self, func):
# TODO: do we need this?
@wraps(func)
def decorated(*args, **kwargs):
new_args = (*args, self.event_dict)
return func(*new_args, **kwargs)

return decorated

def autodiscover(self) -> None:
"""
Detects message registries which have been registered via the "register_*" decorator.
Expand All @@ -102,6 +93,7 @@ def autodiscover(self) -> None:
if project_path not in app_path.parents:
continue

# TODO: registering only via one file might be a plus
for message_type in ("commands", "events"):
try:
for module in os.listdir(app_path / "handlers" / message_type):
Expand Down
1 change: 1 addition & 0 deletions queuebie/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def _handle_command(command: Command, queue: list[Message]):
"""
Handler to process messages of type "Command"
"""
# TODO: refactor that this gets the handler list and a message so we can remove one of those two functions
handler_list = message_registry.command_dict.get(command.__class__, []) # TODO: ive replace "list()" here
for handler in handler_list:
try:
Expand Down
10 changes: 5 additions & 5 deletions testapp/handlers/commands/testapp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from queuebie import message_registry
from queuebie.messages import Event
from testapp.messages.commands.my_commands import MyCommand
from testapp.messages.events.my_events import MyEvent
from testapp.messages.commands.my_commands import DoSomething
from testapp.messages.events.my_events import SomethingHappened


@message_registry.register_command(command=MyCommand)
def handle_my_command(*, context: MyCommand.Context) -> list[Event] | Event:
return MyEvent(context=MyEvent.Context(other_var=context.my_var + 1))
@message_registry.register_command(command=DoSomething)
def handle_my_command(*, context: DoSomething.Context) -> list[Event] | Event:
return SomethingHappened(context=SomethingHappened.Context(other_var=context.my_var + 1))
6 changes: 3 additions & 3 deletions testapp/handlers/events/testapp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from queuebie import message_registry
from queuebie.messages import Command
from testapp.messages.events.my_events import MyEvent
from testapp.messages.events.my_events import SomethingHappened


@message_registry.register_event(event=MyEvent)
def handle_my_event(*, context: MyEvent.Context) -> list[Command] | Command:
@message_registry.register_event(event=SomethingHappened)
def handle_my_event(*, context: SomethingHappened.Context) -> list[Command] | Command:
return []
2 changes: 1 addition & 1 deletion testapp/messages/commands/my_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from queuebie.messages import Command


class MyCommand(Command):
class DoSomething(Command):
@dataclass(kw_only=True)
class Context:
my_var: int
2 changes: 1 addition & 1 deletion testapp/messages/events/my_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from queuebie.messages import Event


class MyEvent(Event):
class SomethingHappened(Event):
@dataclass(kw_only=True)
class Context:
other_var: int
106 changes: 98 additions & 8 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

from queuebie import MessageRegistry
from testapp.messages.commands.my_commands import MyCommand
from testapp.messages.events.my_events import MyEvent
from testapp.messages.commands.my_commands import DoSomething
from testapp.messages.events.my_events import SomethingHappened


def test_message_registry_init_regular():
Expand All @@ -17,23 +19,111 @@ def test_message_registry_singleton_works():
assert message_registry_1 is message_registry_2


def test_message_registry_register_command_regular():
def dummy_function(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_command(command=DoSomething)
decorator(dummy_function)

assert len(message_registry.event_dict) == 0
assert len(message_registry.command_dict) == 1
assert "dummy_function" in str(message_registry.command_dict[DoSomething][0])


def test_message_registry_register_command_second_function():
def dummy_function(*args):
return None

def dummy_function_2(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_command(command=DoSomething)
decorator(dummy_function)
decorator(dummy_function_2)

assert len(message_registry.event_dict) == 0
assert len(message_registry.command_dict) == 1
assert "dummy_function" in str(message_registry.command_dict[DoSomething][0])
assert "dummy_function_2" in str(message_registry.command_dict[DoSomething][1])


def test_message_registry_register_command_wrong_type():
def dummy_function(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_command(command=SomethingHappened)

with pytest.raises(
TypeError, match='Trying to register message function of wrong type: "MyEvent" on handler "dummy_function".'
):
decorator(dummy_function)


def test_message_registry_register_event_regular():
def dummy_function(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_event(event=SomethingHappened)
decorator(dummy_function)

assert len(message_registry.command_dict) == 0
assert len(message_registry.event_dict) == 1
assert "dummy_function" in str(message_registry.event_dict[SomethingHappened][0])


def test_message_registry_register_event_second_function():
def dummy_function(*args):
return None

def dummy_function_2(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_event(event=SomethingHappened)
decorator(dummy_function)
decorator(dummy_function_2)

assert len(message_registry.command_dict) == 0
assert len(message_registry.event_dict) == 1
assert "dummy_function" in str(message_registry.event_dict[SomethingHappened][0])
assert "dummy_function_2" in str(message_registry.event_dict[SomethingHappened][1])


def test_message_registry_register_event_wrong_type():
def dummy_function(*args):
return None

message_registry = MessageRegistry()
decorator = message_registry.register_event(event=DoSomething)

with pytest.raises(
TypeError, match='Trying to register message function of wrong type: "MyCommand" on handler "dummy_function".'
):
decorator(dummy_function)


def test_message_autodiscover_regular():
message_registry = MessageRegistry()

message_registry.autodiscover()

# Assert one command registered
assert len(message_registry.command_dict) == 1
assert MyCommand in message_registry.command_dict.keys()
assert DoSomething in message_registry.command_dict.keys()

# Assert one handler registered
assert len(message_registry.command_dict[MyCommand]) == 1
assert "<function handle_my_command at" in str(message_registry.command_dict[MyCommand][0])
assert len(message_registry.command_dict[DoSomething]) == 1
assert "<function handle_my_command at" in str(message_registry.command_dict[DoSomething][0])

# Assert one event registered
assert len(message_registry.event_dict) == 1
assert MyEvent in message_registry.event_dict.keys()
assert SomethingHappened in message_registry.event_dict.keys()

# Assert one handler registered
assert len(message_registry.event_dict[MyEvent]) == 1
assert "<function handle_my_event at" in str(message_registry.event_dict[MyEvent][0])
assert len(message_registry.event_dict[SomethingHappened]) == 1
assert "<function handle_my_event at" in str(message_registry.event_dict[SomethingHappened][0])
18 changes: 2 additions & 16 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import dataclasses

from queuebie.messages import Command, Event
from queuebie.runner import handle_message


class TestCommand(Command):
@dataclasses.dataclass
class Context:
my_var: int


class CommandTested(Event):
@dataclasses.dataclass
class Context:
my_var: int
from testapp.messages.commands.my_commands import DoSomething


def test_handle_message_pass_single_message():
# TODO: finish me
handle_message(message_list=TestCommand(context=TestCommand.Context(my_var=1)))
handle_message(message_list=DoSomething(context=DoSomething.Context(my_var=1)))
assert 1 == 0 # noqa: PLR0133

0 comments on commit a7bed16

Please sign in to comment.