diff options
| author | 2024-01-26 13:56:24 +0800 | |
|---|---|---|
| committer | 2024-01-26 13:56:24 +0800 | |
| commit | 0e8be9de58454de079d772dec6c0ef9c1774a775 (patch) | |
| tree | 9e0d4863d87524c15c232bf5cbd555d98a90aa70 /tests/test_handlers.py | |
| parent | 796455df18b87515a4f8a4933dd92d79bea58adf (diff) | |
| download | infini-0e8be9de58454de079d772dec6c0ef9c1774a775.tar.gz infini-0e8be9de58454de079d772dec6c0ef9c1774a775.zip | |
:recycle: refactor(infini): refacted infini as version 2.0.0-alpha.1
Diffstat (limited to 'tests/test_handlers.py')
| -rw-r--r-- | tests/test_handlers.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/test_handlers.py b/tests/test_handlers.py new file mode 100644 index 00000000..8d2d2894 --- /dev/null +++ b/tests/test_handlers.py @@ -0,0 +1,84 @@ +from infini.handler import Handler +from infini.input import Input +from infini.output import Output +from infini.router import StartswithRouter + + +def test_handler(): + input = Input(".add 1 2") + + def add(input: Input) -> Output: + a, b = map(int, input.get_plain_text().lstrip(".add").split()) + output = Output() + output.block = False + output.status = 0 + output.type = "text" + output.name = str(a + b) + return output + + def cmd(_: Input) -> Output: + output = Output() + output.block = False + output.status = 0 + output.type = "text" + output.name = "cmd" + return output + + handler = Handler() + handler.handlers = [ + { + "priority": 2, + "router": StartswithRouter(".add"), + "handler": add, + }, + { + "priority": 1, + "router": StartswithRouter("."), + "handler": cmd, + }, + ] + + names = [] + for output in handler.input(input): + names.append(output.name) + assert names == ["cmd", "3"] + + +def test_handler_block(): + input = Input(".add 1 2") + + def add(input: Input) -> Output: + a, b = map(int, input.get_plain_text().lstrip(".add").split()) + output = Output() + output.block = False + output.status = 0 + output.type = "text" + output.name = str(a + b) + return output + + def cmd(_: Input) -> Output: + output = Output() + output.block = True + output.status = 0 + output.type = "text" + output.name = "cmd" + return output + + handler = Handler() + handler.handlers = [ + { + "priority": 2, + "router": StartswithRouter(".add"), + "handler": add, + }, + { + "priority": 1, + "router": StartswithRouter("."), + "handler": cmd, + }, + ] + + names = [] + for output in handler.input(input): + names.append(output.name) + assert names == ["cmd"] |
