Пример #1
0
def make_kutana_no_run(backend_source=None):
    app = Kutana()

    debug = Debug(messages=[])

    if backend_source:
        debug.get_identity = lambda: backend_source

    app.add_backend(debug)

    async def _handle_update(update):
        ctx = await Context.create(
            app=app,
            config={
                "prefixes": (".",),
                "mention_prefix": ("",),
                "ignore_initial_spaces": True
            },
            update=update,
            backend=debug
        )

        return await app._handle_update(update, ctx)

    def handle_update(update):
        return app.get_loop().run_until_complete(_handle_update(update))

    return app, debug, handle_update
Пример #2
0
def test_get_backend():
    app = Kutana()
    app.add_backend(Debug([], name="backend1"))
    app.add_backend(Debug([], name="backend2"))

    assert app.get_backend("backend1").name == "backend1"
    assert app.get_backend("backend2").name == "backend2"
    assert app.get_backend("backend3") is None
Пример #3
0
def test_happy_path():
    app = Kutana()

    # Simple echo plugin
    pl = Plugin("")

    @pl.on_messages()
    async def __(message, ctx):
        await ctx.reply(message.text)

    app.add_plugin(pl)

    # Simple debug backend
    debug = Debug(
        messages=[("message 1", 1), ("message 2", 2)],
        on_complete=app.stop,
    )

    app.add_backend(debug)

    # Run application
    app.run()

    # Check replies
    assert debug.answers[1] == [("message 1", (), {})]
    assert debug.answers[2] == [("message 2", (), {})]
Пример #4
0
def test_not_active_backend():
    app = Kutana()
    app.add_backend(Debug([], name="backend1", active=False))

    async def test():
        with patch('asyncio.ensure_future') as ensure_future:
            await app._on_start(None)
            ensure_future.assert_not_called

    asyncio.get_event_loop().run_until_complete(test())
Пример #5
0
def make_kutana(messages):
    app = Kutana()

    debug = Debug(
        messages=messages,
        on_complete=app.stop,
    )

    app.add_backend(debug)

    return app, debug
Пример #6
0
def test_same_plugins_and_backends():
    app = Kutana()

    plugin = Plugin("")
    backend = Debug([])

    app.add_plugin(plugin)
    with pytest.raises(RuntimeError):
        app.add_plugin(plugin)

    app.add_backend(backend)
    with pytest.raises(RuntimeError):
        app.add_backend(backend)

    assert app.get_backends() == [backend]