aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tests/test_injector.py
blob: ccd040baa44835ea13761bc1c1ae5985106955b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from infini.handler import Handler
from infini.injector import Injector
from infini.input import Input
from infini.loader import Loader
from infini.output import Output
from infini.router import Startswith


def test_injector():
    def name(name: str):
        return name

    def add(a: int, b: int = 0):
        return a + b

    injector = Injector()
    injector.parameters = {"a": 12, "b": 20, "c": 0, "card_name": name}
    assert injector.inject(add)() == 32
    assert injector.output(add) == 32


def test_handler_injector():
    input = Input("test_message")

    def absolute(input: Input, plain_text: str) -> Output:
        return input.output(
            "text",
            plain_text,
            status=0,
            block=False,
        )

    handler = Handler()
    handler.handlers = [
        {
            "priority": 2,
            "router": Startswith(".add"),
            "handler": absolute,
        },
    ]

    core = Loader().into_core()
    core.handler = handler

    for output in core.input(input):
        assert output == "test_message"