This commit is contained in:
2025-11-03 14:53:07 +03:00
parent b37096d790
commit 767d742060
5 changed files with 145 additions and 98 deletions
@@ -0,0 +1,26 @@
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")
@self.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
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())
@@ -0,0 +1,60 @@
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 import Router, Command
from argenta.di.integration import inject, setup_dishka, FromDishka
from argenta.response import Response
from argenta.response.status import ResponseStatus
class Service:
def hello(self) -> str:
return "world"
router = Router(title="DI")
@router.command(Command("HELLO"))
@inject # Auto-inject dependencies from the Response container
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 = []
@@ -0,0 +1,24 @@
import io
import unittest
from contextlib import redirect_stdout
from argenta import Router, Command, Response
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())