Skip to content

Commit

Permalink
Made tests work with server running (#189)
Browse files Browse the repository at this point in the history
* fixed bug in test

* made quick fixture to handle tearing down the handler for cli tests

* renamed test function
  • Loading branch information
rosesyrett authored May 12, 2023
1 parent 5cfd0ff commit a0e0ccc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion tests/service/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
20 changes: 13 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
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
from blueapi.core.bluesky_types import Plan
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()
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -127,16 +138,14 @@ 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...
result = runner.invoke(main, ["-c", "non_existent.yaml"])
assert result.exit_code == 1


@pytest.mark.handler
@patch("blueapi.service.handler.Handler")
@patch("requests.put")
def test_config_passed_down_to_command_children(
Expand All @@ -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()

0 comments on commit a0e0ccc

Please sign in to comment.