def test_command_handler_to_status_with_exclude_from_status():
    ch = CommandHandler(
        name="handler",
        command="/cmd",
        description="command handler",
        func=lambda x: x,
        use_as_default_handler=True,
    )
    assert not ch.to_status_command()
def test_command_handler_to_status_with_simple_handler():
    ch = CommandHandler(name="handler",
                        command="/cmd",
                        description="command handler",
                        func=lambda x: x)
    assert ch.to_status_command() == StatusCommand(
        body="/cmd",
        name="handler",
        description="command handler",
        options=[],
        elements=[],
    )
예제 #3
0
def custom_handler():
    return CommandHandler(
        name="handler",
        command="/cmd",
        description="command handler",
        func=lambda x, b:...,
    )
async def test_sync_dispatcher_message_creation(command_with_text_and_file):
    d = AsyncDispatcher(None)
    await d.start()
    result_array = []

    async def handler_function(message, bot):
        result_array.append(message.body)

    d.add_handler(
        CommandHandler(
            command="/cmd",
            name="handler",
            description="description",
            func=handler_function,
        ))

    for _ in range(3):
        await d.parse_request(command_with_text_and_file,
                              request_type="command")

    await d.shutdown()

    assert len(result_array) == 3
    for r in result_array:
        assert r == "/cmd arg"
async def test_sync_dispatcher_default_handler_processing(
        command_with_text_and_file):
    d = AsyncDispatcher(None)
    await d.start()

    result_array = []

    async def handler_function(message, bot):
        result_array.append("default")

    d.add_handler(
        CommandHandler(
            command="/cmd",
            name="handler",
            description="description",
            func=handler_function,
            use_as_default_handler=True,
        ))

    await d.parse_request(command_with_text_and_file, request_type="command")

    await d.shutdown()

    assert len(result_array) == 1
    assert result_array[0] == "default"
예제 #6
0
def custom_default_handler():
    return CommandHandler(
        name="default handler",
        command="/defaultcmd",
        description="default command handler",
        func=lambda x, y:...,
        use_as_default_handler=True,
    )
예제 #7
0
def custom_async_handler_with_sync_command_body():
    async def f(m, b):
        pass

    return CommandHandler(name="async handler",
                          command="/cmd",
                          description="command handler",
                          func=f)
예제 #8
0
def custom_async_handler():
    async def f(m, b):
        ...

    return CommandHandler(name="async handler",
                          command="/acmd",
                          description="command handler",
                          func=f)
def test_sync_dispatcher_not_accepting_coroutine_as_handler():
    d = SyncDispatcher(workers=4, bot=None)

    async def f(m):
        pass

    with pytest.raises(BotXException):
        d.add_handler(
            CommandHandler(name="a", command="a", description="a", func=f))
예제 #10
0
def test_router_command_decorator_with_docstring(custom_router):
    @custom_router.command(name="handler")
    def func(m):
        """func command"""

    assert len(custom_router._handlers) == 1
    assert "/handler" in custom_router._handlers
    assert custom_router._handlers["/handler"] == CommandHandler(
        command="/handler", func=func, name="Handler", description="func command"
    )
예제 #11
0
def test_router_command_decorator_call_simple(custom_router):
    @custom_router.command()
    def func(m):
        pass

    assert len(custom_router._handlers) == 1
    assert "/func" in custom_router._handlers
    assert custom_router._handlers["/func"] == CommandHandler(
        command="/func", func=func, name="Func", description="func command"
    )
예제 #12
0
def custom_default_async_handler():
    async def f(m, b):
        pass

    return CommandHandler(
        name="default handler",
        command="/defaultcmd",
        description="default command handler",
        func=f,
        use_as_default_handler=True,
    )
def test_dispatcher_command_handler_signature_check(custom_dispatcher):
    def f():
        pass

    with pytest.raises(BotXException):
        custom_dispatcher.add_handler(
            CommandHandler(name="a", command="a", description="a", func=f))

    def f(m):
        pass

    with pytest.raises(BotXException):
        custom_dispatcher.add_handler(
            CommandHandler(name="a", command="a", description="a", func=f))

    def f(m, b):
        pass

    custom_dispatcher.add_handler(
        CommandHandler(name="a", command="a", description="a", func=f))
async def test_sync_dispatcher_not_accepting_coroutine_as_handler():
    d = AsyncDispatcher(None)
    await d.start()

    def f(m):
        pass

    with pytest.raises(BotXException):
        d.add_handler(
            CommandHandler(name="a", command="a", description="a", func=f))

    await d.shutdown()
예제 #15
0
def test_router_command_decorator_with_full_args(custom_router):
    @custom_router.command(
        name="handler", body="/function", description="command description"
    )
    def func(m):
        """func command"""

    assert custom_router._handlers["/function"] == CommandHandler(
        command="/function",
        func=func,
        name="Handler",
        description="command description",
    )
def test_exception_returned_if_occurred_in_handler():
    d = SyncDispatcher(workers=1, bot=None)

    error = Exception("exception from handler")

    def f(m, b):
        raise error

    d.add_handler(
        CommandHandler(name="a", command="a", description="a", func=f))
    assert d._handlers["a"].func(None, None) == error

    d.shutdown()