From a0e0ccc697a3ba5d32f13974da32e07c05397bf9 Mon Sep 17 00:00:00 2001 From: Rose Yemelyanova <90774497+RAYemelyanova@users.noreply.github.com> Date: Fri, 12 May 2023 12:23:40 +0100 Subject: [PATCH] Made tests work with server running (#189) * fixed bug in test * made quick fixture to handle tearing down the handler for cli tests * renamed test function --- pyproject.toml | 3 +++ tests/service/test_handler.py | 6 +++++- tests/test_cli.py | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c0c98a345..df52a72d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,6 +96,9 @@ addopts = """ filterwarnings = ["error", "ignore::DeprecationWarning"] # Doctest python code in docs, python code in src docstrings, test functions in tests testpaths = "docs src tests" +markers = [ + "handler: marks tests that interact with the global handler object in handler.py", +] [tool.coverage.run] data_file = "/tmp/blueapi.coverage" diff --git a/tests/service/test_handler.py b/tests/service/test_handler.py index acb51b999..28def3d1c 100644 --- a/tests/service/test_handler.py +++ b/tests/service/test_handler.py @@ -10,7 +10,7 @@ @patch("blueapi.service.handler.Handler") -def test_get_handler_raises_before_setup_hadler_called( +def test_get_handler_raises_before_setup_handler_called( mock_handler: Mock, handler: Handler ): mock_handler.side_effect = Mock(return_value=handler) @@ -23,3 +23,7 @@ def test_get_handler_raises_before_setup_hadler_called( assert handler teardown_handler() + + +def test_teardown_handler_does_nothing_if_setup_handler_not_called(): + assert teardown_handler() is None diff --git a/tests/test_cli.py b/tests/test_cli.py index e4501685f..b4cc50c19 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -5,6 +5,7 @@ from fastapi.testclient import TestClient from mock import Mock, patch from pydantic import BaseModel +from requests.exceptions import ConnectionError from blueapi import __version__ from blueapi.cli.cli import main @@ -12,6 +13,13 @@ from blueapi.service.handler import Handler, teardown_handler +@pytest.fixture(autouse=True) +def ensure_handler_teardown(request): + yield + if "handler" in request.keywords: + teardown_handler() + + @pytest.fixture def runner(): return CliRunner() @@ -39,7 +47,9 @@ def test_main_with_nonexistent_config_file(): type(result.exception) == FileNotFoundError -def test_controller_plans(): +@patch("requests.get") +def test_connection_error_caught_by_wrapper_func(mock_requests: Mock): + mock_requests.side_effect = ConnectionError() runner = CliRunner() result = runner.invoke(main, ["controller", "plans"]) @@ -73,6 +83,7 @@ def test_deprecated_worker_command( ) +@pytest.mark.handler @patch("blueapi.service.handler.Handler") @patch("requests.get") def test_get_plans_and_devices( @@ -127,9 +138,6 @@ def test_get_plans_and_devices( + "\n{'devices': [{'name': 'my-device', 'protocols': ['HasName']}]}\n" ) - # manually teardown handler, as normally uvicorn does this. - teardown_handler() - def test_invalid_config_path_handling(runner: CliRunner): # test what happens if you pass an invalid config file... @@ -137,6 +145,7 @@ def test_invalid_config_path_handling(runner: CliRunner): assert result.exit_code == 1 +@pytest.mark.handler @patch("blueapi.service.handler.Handler") @patch("requests.put") def test_config_passed_down_to_command_children( @@ -161,6 +170,3 @@ def test_config_passed_down_to_command_children( assert mock_requests.call_args[0][0] == "http://a.fake.host:12345/task/sleep" assert mock_requests.call_args[1] == {"json": {"time": 5}} - - # manually teardown handler, as normally uvicorn does this. - teardown_handler()