Skip to content

Commit

Permalink
Annotated fixtures and use builtin tmp_path
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondJoseph committed Nov 11, 2024
1 parent 342ab74 commit dcaa11b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.fixture(scope="function")
def RE(request):
def RE(request: pytest.FixtureRequest) -> RunEngine:
loop = asyncio.new_event_loop()
loop.set_debug(True)
RE = RunEngine({}, call_returns_result=True, loop=loop)
Expand Down
53 changes: 23 additions & 30 deletions tests/unit_tests/cli/test_scratch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import stat
import uuid
from collections.abc import Generator
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import Mock, call, patch

import pytest
Expand All @@ -12,38 +12,31 @@


@pytest.fixture
def directory_path() -> Path: # type: ignore
temporary_directory = TemporaryDirectory()
yield Path(temporary_directory.name)
temporary_directory.cleanup()


@pytest.fixture
def file_path(directory_path: Path) -> Path: # type: ignore
file_path = directory_path / str(uuid.uuid4())
def file_path(tmp_path: Path) -> Generator[Path]:
file_path = tmp_path / str(uuid.uuid4())
with file_path.open("w") as stream:
stream.write("foo")
yield file_path
os.remove(file_path)


@pytest.fixture
def nonexistant_path(directory_path: Path) -> Path:
file_path = directory_path / str(uuid.uuid4())
def nonexistant_path(tmp_path: Path) -> Path:
file_path = tmp_path / str(uuid.uuid4())
assert not file_path.exists()
return file_path


@patch("blueapi.cli.scratch.Popen")
def test_scratch_install_installs_path(
mock_popen: Mock,
directory_path: Path,
tmp_path: Path,
):
mock_process = Mock()
mock_process.returncode = 0
mock_popen.return_value = mock_process

scratch_install(directory_path, timeout=1.0)
scratch_install(tmp_path, timeout=1.0)

mock_popen.assert_called_once_with(
[
Expand All @@ -53,7 +46,7 @@ def test_scratch_install_installs_path(
"install",
"--no-deps",
"-e",
str(directory_path),
str(tmp_path),
]
)

Expand All @@ -72,24 +65,24 @@ def test_scratch_install_fails_on_nonexistant_path(nonexistant_path: Path):
@pytest.mark.parametrize("code", [1, 2, 65536])
def test_scratch_install_fails_on_non_zero_exit_code(
mock_popen: Mock,
directory_path: Path,
tmp_path: Path,
code: int,
):
mock_process = Mock()
mock_process.returncode = code
mock_popen.return_value = mock_process

with pytest.raises(RuntimeError):
scratch_install(directory_path, timeout=1.0)
scratch_install(tmp_path, timeout=1.0)


@patch("blueapi.cli.scratch.Repo")
def test_repo_not_cloned_and_validated_if_found_locally(
mock_repo: Mock,
directory_path: Path,
tmp_path: Path,
):
ensure_repo("http://example.com/foo.git", directory_path)
mock_repo.assert_called_once_with(directory_path)
ensure_repo("http://example.com/foo.git", tmp_path)
mock_repo.assert_called_once_with(tmp_path)
mock_repo.clone_from.assert_not_called()


Expand All @@ -108,9 +101,9 @@ def test_repo_cloned_if_not_found_locally(
@patch("blueapi.cli.scratch.Repo")
def test_repo_cloned_with_correct_umask(
mock_repo: Mock,
directory_path: Path,
tmp_path: Path,
):
repo_root = directory_path / "foo"
repo_root = tmp_path / "foo"
file_path = repo_root / "a"

def write_repo_files():
Expand Down Expand Up @@ -153,10 +146,10 @@ def test_setup_scratch_fails_on_non_directory_root(
def test_setup_scratch_iterates_repos(
mock_scratch_install: Mock,
mock_ensure_repo: Mock,
directory_path: Path,
tmp_path: Path,
):
config = ScratchConfig(
root=directory_path,
root=tmp_path,
repositories=[
ScratchRepository(
name="foo",
Expand All @@ -172,15 +165,15 @@ def test_setup_scratch_iterates_repos(

mock_ensure_repo.assert_has_calls(
[
call("http://example.com/foo.git", directory_path / "foo"),
call("http://example.com/bar.git", directory_path / "bar"),
call("http://example.com/foo.git", tmp_path / "foo"),
call("http://example.com/bar.git", tmp_path / "bar"),
]
)

mock_scratch_install.assert_has_calls(
[
call(directory_path / "foo", timeout=120.0),
call(directory_path / "bar", timeout=120.0),
call(tmp_path / "foo", timeout=120.0),
call(tmp_path / "bar", timeout=120.0),
]
)

Expand All @@ -190,10 +183,10 @@ def test_setup_scratch_iterates_repos(
def test_setup_scratch_continues_after_failure(
mock_scratch_install: Mock,
mock_ensure_repo: Mock,
directory_path: Path,
tmp_path: Path,
):
config = ScratchConfig(
root=directory_path,
root=tmp_path,
repositories=[
ScratchRepository(
name="foo",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

@pytest.fixture
def mock_rest() -> BlueapiRestClient:
mock = Mock(spec=BlueapiRestClient)
mock = MagicMock(spec=BlueapiRestClient)

mock.get_plans.return_value = PLANS
mock.get_plan.return_value = PLAN
Expand All @@ -89,12 +89,12 @@ def mock_events() -> EventBusClient:


@pytest.fixture
def client(mock_rest: Mock) -> BlueapiClient:
def client(mock_rest: BlueapiRestClient) -> BlueapiClient:
return BlueapiClient(rest=mock_rest)


@pytest.fixture
def client_with_events(mock_rest: Mock, mock_events: MagicMock):
def client_with_events(mock_rest: BlueapiRestClient, mock_events: EventBusClient):
return BlueapiClient(rest=mock_rest, events=mock_events)


Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/service/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.fixture
def mock_connection() -> Mock:
def mock_connection() -> Connection:
return Mock(spec=Connection)


Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/service/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Generator
from typing import Any, Generic, TypeVar
from unittest import mock
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -30,7 +31,7 @@ def runner() -> WorkerDispatcher:


@pytest.fixture
def started_runner(runner: WorkerDispatcher):
def started_runner(runner: WorkerDispatcher) -> Generator[WorkerDispatcher]:
runner.start()
yield runner
runner.stop()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@


@pytest.fixture
def mock_connection() -> Mock:
def mock_connection() -> Connection:
return Mock(spec=Connection)


@pytest.fixture
def mock_stomp_client(mock_connection: Mock) -> StompClient:
def mock_stomp_client(mock_connection: Connection) -> StompClient:
return StompClient(conn=mock_connection)


Expand Down

0 comments on commit dcaa11b

Please sign in to comment.