Esempio n. 1
0
def collector_with_handlers(handler_as_function):
    collector = Collector()
    for index in range(1, 31):
        body = "/{0}".format("a" * index)
        collector.add_handler(handler=handler_as_function, body=body, name=str(index))

    return collector
Esempio n. 2
0
class FSM:
    def __init__(self, states: Type[Enum]) -> None:
        self.transitions: Dict[Enum, Transition] = {}
        self.collector = Collector()
        self.states = states

    def handler(
        self,
        on_state: Enum,
        next_state: Optional[Union[Enum, object]] = _default_transition,
        on_failure: Optional[Union[Enum, object]] = _default_transition,
    ) -> Callable:
        def decorator(handler: Callable) -> Callable:
            self.collector.add_handler(
                handler,
                body=on_state.name,
                name=on_state.name,
                include_in_status=False,
            )
            self.transitions[on_state] = Transition(
                on_success=next_state, on_failure=on_failure,
            )

            return handler

        return decorator
Esempio n. 3
0
async def test_executing_handler_when_partial_match(message, build_handler):
    event = threading.Event()
    message.command.body = "/command with arguments"

    collector = Collector()
    collector.add_handler(build_handler(event), body=message.command.command)

    await collector.handle_message(message)

    assert event.is_set()
Esempio n. 4
0
async def test_execution_when_full_match(message, build_handler):
    event = threading.Event()
    message.command.body = "/command"

    collector = Collector()
    collector.add_handler(build_handler(event), body=message.command.body)

    await collector.handle_message(message)

    assert event.is_set()
Esempio n. 5
0
def test_preserve_length_sort_when_merging_collectors(
    collector_with_handlers,
    handler_as_function,
):
    collector = Collector()
    collector.add_handler(handler=handler_as_function, body="/{0}".format("a" * 1000))

    collector_with_handlers.include_collector(collector)

    added_handlers = collector_with_handlers.sorted_handlers
    assert added_handlers[0] == collector.handlers[0]
Esempio n. 6
0
def test_error_when_merging_handlers_with_equal_bodies(build_handler):
    collector1 = Collector()
    collector1.add_handler(
        handler=build_handler("handler1"),
        body="/body",
        name="handler1",
    )

    collector2 = Collector()
    collector2.add_handler(
        handler=build_handler("handler2"),
        body="/body",
        name="handler2",
    )

    with pytest.raises(AssertionError):
        collector1.include_collector(collector2)
def test_handler_can_not_consist_from_slashes_only(handler_as_function):
    collector = Collector()
    with pytest.raises(ValidationError):
        collector.add_handler(handler_as_function, body="////")
def test_generating_body_from_snake_case(handler_as_normal_method):
    collector = Collector()
    collector.add_handler(handler=handler_as_normal_method)
    handler = collector.handler_for("handler_method_snake_case")
    assert handler.body == "/handler-method-snake-case"
def test_generating_body_from_camel_case(handler_as_camel_case_method):
    collector = Collector()
    collector.add_handler(handler=handler_as_camel_case_method)
    handler = collector.handler_for("handlerMethodCamelCase")
    assert handler.body == "/handler-method-camel-case"
Esempio n. 10
0
def test_generating_body_from_pascal_case(handler_as_pascal_case_method):
    collector = Collector()
    collector.add_handler(handler=handler_as_pascal_case_method)
    handler = collector.handler_for("HandlerMethodPascalCase")
    assert handler.body == "/handler-method-pascal-case"