rename orchestrator method start_polling to run_repl

This commit is contained in:
2026-02-10 14:03:37 +03:00
parent 6ed1d35e8a
commit de6d35205c
23 changed files with 79 additions and 81 deletions
@@ -38,7 +38,7 @@ def test_empty_input_triggers_empty_command_handler(monkeypatch: pytest.MonkeyPa
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_empty_command_handler(lambda: print('Empty input command'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -64,7 +64,7 @@ def test_unknown_command_triggers_unknown_command_handler(monkeypatch: pytest.Mo
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -85,7 +85,7 @@ def test_mixed_valid_and_unknown_commands_handled_correctly(monkeypatch: pytest.
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -110,7 +110,7 @@ def test_multiple_commands_with_unknown_command_in_between(monkeypatch: pytest.M
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -137,7 +137,7 @@ def test_unregistered_flag_without_value_is_accessible(monkeypatch: pytest.Monke
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -161,7 +161,7 @@ def test_unregistered_flag_with_value_is_accessible(monkeypatch: pytest.MonkeyPa
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -184,7 +184,7 @@ def test_registered_and_unregistered_flags_coexist(monkeypatch: pytest.MonkeyPat
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -210,7 +210,7 @@ def test_flag_without_value_triggers_incorrect_syntax_handler(monkeypatch: pytes
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_incorrect_input_syntax_handler(lambda command: print(f'Incorrect flag syntax: "{command}"'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -236,7 +236,7 @@ def test_repeated_flags_trigger_repeated_flags_handler(monkeypatch: pytest.Monke
app = App(override_system_messages=True, printer=print)
app.include_router(router)
app.set_repeated_input_flags_handler(lambda command: print(f'Repeated input flags: "{command}"'))
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -38,7 +38,7 @@ def test_simple_command_executes_successfully(monkeypatch: pytest.MonkeyPatch, c
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -62,7 +62,7 @@ def test_two_commands_execute_sequentially(monkeypatch: pytest.MonkeyPatch, caps
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -90,7 +90,7 @@ def test_three_commands_execute_sequentially(monkeypatch: pytest.MonkeyPatch, ca
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -118,7 +118,7 @@ def test_custom_flag_without_value_is_recognized(monkeypatch: pytest.MonkeyPatch
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -141,7 +141,7 @@ def test_custom_flag_with_regex_validation_accepts_valid_value(monkeypatch: pyte
app = App(override_system_messages=True, repeat_command_groups_printing=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -169,7 +169,7 @@ def test_predefined_short_help_flag_is_recognized(monkeypatch: pytest.MonkeyPatc
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -192,7 +192,7 @@ def test_predefined_info_flag_is_recognized(monkeypatch: pytest.MonkeyPatch, cap
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -215,7 +215,7 @@ def test_predefined_host_flag_with_value_is_recognized(monkeypatch: pytest.Monke
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
@@ -244,7 +244,7 @@ def test_two_predefined_flags_are_recognized_together(monkeypatch: pytest.Monkey
app = App(override_system_messages=True, printer=print)
app.include_router(router)
orchestrator.start_polling(app)
orchestrator.run_repl(app)
output = capsys.readouterr().out
+35 -36
View File
@@ -45,12 +45,11 @@ def sample_router() -> Router:
# ============================================================================
def test_orchestrator_initializes_with_default_argparser(mocker: MockerFixture) -> None:
"""Test Orchestrator initialization with default ArgParser"""
def test_orchestrator_initializes_with_no_argparser(mocker: MockerFixture) -> None:
"""Test Orchestrator initialization with no ArgParser"""
mocker.patch('sys.argv', ['test_program'])
orchestrator = Orchestrator()
assert orchestrator._arg_parser is not None
assert isinstance(orchestrator._arg_parser, ArgParser)
assert orchestrator._arg_parser is None
def test_orchestrator_initializes_with_custom_argparser(mock_argparser: ArgParser) -> None:
@@ -89,80 +88,80 @@ def test_orchestrator_parses_args_on_initialization(mocker: MockerFixture, mock_
# ============================================================================
# Tests for start_polling method
# Tests for run_repl method
# ============================================================================
def test_start_polling_creates_dishka_container(
def test_run_repl_creates_dishka_container(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that start_polling creates a dishka container"""
"""Test that run_repl creates a dishka container"""
mock_make_container = mocker.patch('argenta.orchestrator.entity.make_container')
_mock_setup_dishka = mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
mock_make_container.assert_called_once()
assert mock_make_container.call_args[1]['context'] == {ArgParser: mock_argparser}
def test_start_polling_calls_setup_dishka_with_auto_inject_enabled(
def test_run_repl_calls_setup_dishka_with_auto_inject_enabled(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that start_polling calls setup_dishka with auto_inject=True"""
"""Test that run_repl calls setup_dishka with auto_inject=True"""
mock_container = mocker.MagicMock() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
mocker.patch('argenta.orchestrator.entity.make_container', return_value=mock_container)
mock_setup_dishka = mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser, auto_inject_handlers=True)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
mock_setup_dishka.assert_called_once_with(sample_app, mock_container, auto_inject=True)
def test_start_polling_calls_setup_dishka_with_auto_inject_disabled(
def test_run_repl_calls_setup_dishka_with_auto_inject_disabled(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that start_polling calls setup_dishka with auto_inject=False"""
"""Test that run_repl calls setup_dishka with auto_inject=False"""
mock_container = mocker.MagicMock() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
mocker.patch('argenta.orchestrator.entity.make_container', return_value=mock_container)
mock_setup_dishka = mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser, auto_inject_handlers=False)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
mock_setup_dishka.assert_called_once_with(sample_app, mock_container, auto_inject=False)
def test_start_polling_calls_app_run_polling(
def test_run_repl_calls_app_run_repl(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that start_polling calls app.run_polling()"""
"""Test that run_repl calls app.run_polling()"""
mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mock_run_polling = mocker.patch.object(sample_app, '_run_polling')
mock_run_repl = mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
mock_run_polling.assert_called_once()
mock_run_repl.assert_called_once()
def test_start_polling_includes_custom_providers_in_container(
def test_run_repl_includes_custom_providers_in_container(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that start_polling includes custom providers in container"""
"""Test that run_repl includes custom providers in container"""
custom_provider = Provider()
mock_make_container = mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser, custom_providers=[custom_provider])
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
# Check that custom_provider was passed to make_container
call_args = mock_make_container.call_args[0]
@@ -180,14 +179,14 @@ def test_orchestrator_integrates_with_app_with_router(
"""Test that Orchestrator properly integrates with App that has routers"""
mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mock_run_polling = mocker.patch.object(sample_app, '_run_polling')
mock_run_repl = mocker.patch.object(sample_app, '_run_repl')
sample_app.include_router(sample_router)
orchestrator = Orchestrator(arg_parser=mock_argparser)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
mock_run_polling.assert_called_once()
mock_run_repl.assert_called_once()
assert len(sample_app.registered_routers.registered_routers) == 1
@@ -202,10 +201,10 @@ def test_orchestrator_passes_argparser_to_container_context(
"""Test that Orchestrator passes ArgParser instance to container context"""
mock_make_container = mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(arg_parser=mock_argparser)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
# Verify that ArgParser was passed in context
call_kwargs = mock_make_container.call_args[1]
@@ -219,18 +218,18 @@ def test_orchestrator_passes_argparser_to_container_context(
# ============================================================================
def test_orchestrator_handles_app_run_polling_exception(
def test_orchestrator_handles_app_run_repl_exception(
mocker: MockerFixture, mock_argparser: ArgParser, sample_app: App
) -> None:
"""Test that Orchestrator propagates exceptions from app.run_polling()"""
mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling', side_effect=RuntimeError("Test error"))
mocker.patch.object(sample_app, '_run_repl', side_effect=RuntimeError("Test error"))
orchestrator = Orchestrator(arg_parser=mock_argparser)
with pytest.raises(RuntimeError, match="Test error"):
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
# ============================================================================
@@ -246,13 +245,13 @@ def test_orchestrator_accepts_multiple_custom_providers(
provider2 = Provider()
mock_make_container = mocker.patch('argenta.orchestrator.entity.make_container')
mocker.patch('argenta.orchestrator.entity.setup_dishka')
mocker.patch.object(sample_app, '_run_polling')
mocker.patch.object(sample_app, '_run_repl')
orchestrator = Orchestrator(
arg_parser=mock_argparser,
custom_providers=[provider1, provider2]
)
orchestrator.start_polling(sample_app)
orchestrator.run_repl(sample_app)
call_args = mock_make_container.call_args[0]
assert provider1 in call_args