mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
docs
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from pytest import CaptureFixture
|
||||
|
||||
from argenta import App, Orchestrator, Router, Command, Response
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def patched_argv():
|
||||
with patch.object(sys, 'argv', ['program.py']):
|
||||
yield
|
||||
|
||||
def test_input_incorrect_command(capsys: CaptureFixture[str]):
|
||||
router = Router()
|
||||
orchestrator = Orchestrator()
|
||||
|
||||
@router.command(Command('test'))
|
||||
def test(response: Response) -> None:
|
||||
print('test command')
|
||||
|
||||
app = App(override_system_messages=True, print_func=print)
|
||||
app.include_router(router)
|
||||
app.set_unknown_command_handler(
|
||||
lambda command: print(f'Unknown command: {command.trigger}')
|
||||
)
|
||||
|
||||
with patch("builtins.input", side_effect=["help", "q"]):
|
||||
orchestrator.start_polling(app)
|
||||
|
||||
output = capsys.readouterr().out
|
||||
assert "\nUnknown command: help\n" in output
|
||||
@@ -1,25 +1,21 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
|
||||
from argenta import App, Router, Command, Response
|
||||
from argenta.command import InputCommand
|
||||
|
||||
|
||||
class TestAppIntegration(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.app = App(override_system_messages=True, repeat_command_groups=False)
|
||||
self.router = Router(title="App")
|
||||
def test_simple_app() -> None:
|
||||
app = App(override_system_messages=True, repeat_command_groups_printing=False)
|
||||
router = Router(title="App")
|
||||
|
||||
@self.router.command(Command("HELP", description="Show help"))
|
||||
def help_cmd(response: Response):
|
||||
print("Available commands: HELP")
|
||||
@router.command(Command("HELP", description="Show help"))
|
||||
def help_cmd(response: Response):
|
||||
print("Available commands: HELP")
|
||||
|
||||
_ = help_cmd # appease linter: function is registered via decorator
|
||||
app.include_router(router)
|
||||
|
||||
self.app.include_router(self.router)
|
||||
|
||||
def test_help_command(self):
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
self.router.finds_appropriate_handler(InputCommand.parse("HELP"))
|
||||
self.assertIn("Available commands:", stdout.getvalue())
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
router.finds_appropriate_handler(InputCommand.parse("HELP"))
|
||||
|
||||
assert "Available commands:" in stdout.getvalue()
|
||||
|
||||
@@ -1,59 +1,42 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest.mock import Mock
|
||||
|
||||
from dishka import Provider, provide, make_container, Scope # pyright: ignore[reportUnknownVariableType]
|
||||
from argenta.command import InputCommand
|
||||
from dishka import Provider, make_container, Scope
|
||||
|
||||
from argenta import Router, Command
|
||||
from argenta.di.integration import inject, setup_dishka, FromDishka
|
||||
from argenta.response import Response
|
||||
from argenta.response.status import ResponseStatus
|
||||
from argenta import Router, Response
|
||||
from argenta.di.integration import setup_dishka, FromDishka
|
||||
|
||||
|
||||
class Service:
|
||||
def hello(self) -> str:
|
||||
return "world"
|
||||
|
||||
|
||||
def get_service() -> Service:
|
||||
return Service()
|
||||
|
||||
|
||||
router = Router(title="DI")
|
||||
|
||||
|
||||
@router.command(Command("HELLO"))
|
||||
@inject # Auto-inject dependencies from the Response container
|
||||
@router.command("HELLO")
|
||||
def hello(response: Response, service: FromDishka[Service]) -> None:
|
||||
print(f"hello {service.hello()}")
|
||||
|
||||
|
||||
class TestDIHandler(unittest.TestCase):
|
||||
def test_hello_uses_service(self):
|
||||
# Prepare DI container with a stub
|
||||
fake = Mock(spec=Service)
|
||||
fake.hello.return_value = "mocked"
|
||||
|
||||
class TestProvider(Provider):
|
||||
scope = Scope.APP
|
||||
|
||||
@provide(scope=Scope.APP)
|
||||
def service(self) -> Service:
|
||||
return fake
|
||||
|
||||
container = make_container(TestProvider())
|
||||
|
||||
# Bind container to Response via integration
|
||||
setup_dishka(app=_FakeApp(), container=container, auto_inject=False) # type: ignore[arg-type]
|
||||
|
||||
# Create Response bound to the container
|
||||
r = Response(ResponseStatus.ALL_FLAGS_VALID)
|
||||
r._dishka_container = container # direct assignment is acceptable in tests # pyright: ignore[reportPrivateUsage]
|
||||
|
||||
# Call handler directly
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
hello(r)
|
||||
|
||||
self.assertIn("hello mocked", stdout.getvalue())
|
||||
|
||||
|
||||
|
||||
|
||||
class _FakeApp:
|
||||
# Minimal stub for setup_dishka; app object is not used in unit tests
|
||||
registered_routers = []
|
||||
registered_routers = [router]
|
||||
|
||||
|
||||
def test_hello_uses_service():
|
||||
provider = Provider(scope=Scope.APP)
|
||||
provider.provide(get_service)
|
||||
|
||||
container = make_container(provider)
|
||||
setup_dishka(app=_FakeApp(), container=container, auto_inject=True)
|
||||
|
||||
# Call handler
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
router.finds_appropriate_handler(InputCommand.parse('HELLO'))
|
||||
|
||||
assert "hello world" in stdout.getvalue()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import io
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
|
||||
from argenta import Router, Command, Response
|
||||
@@ -8,15 +7,13 @@ from argenta.command import InputCommand
|
||||
|
||||
router = Router(title="Demo")
|
||||
|
||||
|
||||
@router.command(Command("PING", description="Ping command"))
|
||||
def ping(response: Response):
|
||||
print("PONG")
|
||||
|
||||
|
||||
class TestSimpleHandler(unittest.TestCase):
|
||||
def test_ping_prints_pong(self):
|
||||
# Имитация запуска хендлера через роутер
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
router.finds_appropriate_handler(InputCommand.parse("PING"))
|
||||
self.assertIn("PONG", stdout.getvalue())
|
||||
def test_ping_prints_pong():
|
||||
# Call handler
|
||||
with redirect_stdout(io.StringIO()) as stdout:
|
||||
router.finds_appropriate_handler(InputCommand.parse("PING"))
|
||||
assert "PONG" in stdout.getvalue()
|
||||
|
||||
Reference in New Issue
Block a user