Skip to content

Commit

Permalink
Fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Aug 24, 2021
1 parent 9f3a089 commit 6056b44
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dev =
mock
mypy
types-pyyaml
types-mock
types-mock >=4.0
isort >5.0
flake8
flake8-isort
Expand Down
124 changes: 64 additions & 60 deletions tests/test_odinprocserv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from mock import AsyncMock, Mock, call
from mock import Mock, call, patch
from pytest_mock import MockerFixture

from odinprocservcontrol import OdinProcServConfig, OdinProcServControl
Expand All @@ -23,9 +23,6 @@ def control():
return OdinProcServControl(config, log_level="DEBUG")


record_mock = None


@pytest.fixture(autouse=True)
def _patch_builder(mocker):
mocker.patch.object(builder, "longOut")
Expand All @@ -38,60 +35,60 @@ def _patch_builder(mocker):
async def test_start_processes_press(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_start_processes", new_callable=AsyncMock)
await control.start_processes(1)
control._start_processes.assert_awaited_once_with()
control.start.set.assert_called_once_with(0)
with patch.object(control, "_start_processes") as start_mock:
await control.start_processes(1)
start_mock.assert_awaited_once_with()
control.start.set.assert_called_once_with(0)


@pytest.mark.asyncio
async def test_start_processes_release(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_start_processes", new_callable=AsyncMock)
await control.start_processes(0)
control._start_processes.assert_not_called()
control.start.set.assert_not_called()
with patch.object(control, "_start_processes") as start_mock:
await control.start_processes(0)
start_mock.assert_not_called()
control.start.set.assert_not_called()


@pytest.mark.asyncio
async def test_stop_processes_press(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_stop_processes", new_callable=AsyncMock)
await control.stop_processes(1)
control._stop_processes.assert_called_once_with()
control.stop.set.assert_called_once_with(0)
with patch.object(control, "_stop_processes") as stop_mock:
await control.stop_processes(1)
stop_mock.assert_called_once_with()
control.stop.set.assert_called_once_with(0)


@pytest.mark.asyncio
async def test_stop_processes_release(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_stop_processes", new_callable=AsyncMock)
await control.stop_processes(0)
control._stop_processes.assert_not_called()
control.stop.set.assert_not_called()
with patch.object(control, "_stop_processes") as stop_mock:
await control.stop_processes(0)
stop_mock.assert_not_called()
control.stop.set.assert_not_called()


@pytest.mark.asyncio
async def test_restart_processes_press(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_restart_processes", new_callable=AsyncMock)
await control.restart_processes(1)
control._restart_processes.assert_called_once_with()
control.restart.set.assert_called_once_with(0)
with patch.object(control, "_restart_processes") as restart_mock:
await control.restart_processes(1)
restart_mock.assert_called_once_with()
control.restart.set.assert_called_once_with(0)


@pytest.mark.asyncio
async def test_restart_processes_release(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_restart_processes", new_callable=AsyncMock)
await control.restart_processes(0)
control._restart_processes.assert_not_called()
control.restart.set.assert_not_called()
with patch.object(control, "_restart_processes") as restart_mock:
await control.restart_processes(0)
restart_mock.assert_not_called()
control.restart.set.assert_not_called()


# Test _[start, stop, restart]_processes
Expand All @@ -101,8 +98,9 @@ async def test_restart_processes_release(
async def test__start_processes(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
press_mock = mocker.patch.object(control, "_press_buttons", new_callable=AsyncMock)
sleep_mock = mocker.patch(ASYNCIO_SLEEP_PATCH, new_callable=AsyncMock)
press_mock = patch.object(control, "_press_buttons").start()
sleep_mock = patch(ASYNCIO_SLEEP_PATCH).start()

manager = Mock()
manager.attach_mock(press_mock, "press_mock")
manager.attach_mock(sleep_mock, "sleep_mock")
Expand Down Expand Up @@ -132,44 +130,46 @@ async def test__start_processes(

assert expected_calls == manager.method_calls

await press_mock.stop()
await sleep_mock.stop()


@pytest.mark.asyncio
async def test__stop_processes(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
mocker.patch.object(control, "_press_buttons", new_callable=AsyncMock)
expected_call = call(
[
"BLXXY-EA-ODN-02",
"BLXXY-EA-ODN-03",
"BLXXY-EA-ODN-04",
"BLXXY-EA-ODN-05",
"BLXXY-EA-ODN-06",
"BLXXY-EA-ODN-07",
"BLXXY-EA-ODN-08",
"BLXXY-EA-ODN-09",
"BLXXY-EA-ODN-10",
"BLXXY-EA-ODN-11",
"BLXXY-EA-ODN-01",
"BLXXY-EA-IOC-01",
],
"STOP",
)
with patch.object(control, "_press_buttons") as press_mock:
expected_call = call(
[
"BLXXY-EA-ODN-02",
"BLXXY-EA-ODN-03",
"BLXXY-EA-ODN-04",
"BLXXY-EA-ODN-05",
"BLXXY-EA-ODN-06",
"BLXXY-EA-ODN-07",
"BLXXY-EA-ODN-08",
"BLXXY-EA-ODN-09",
"BLXXY-EA-ODN-10",
"BLXXY-EA-ODN-11",
"BLXXY-EA-ODN-01",
"BLXXY-EA-IOC-01",
],
"STOP",
)

await control._stop_processes()
await control._stop_processes()

assert expected_call in control._press_buttons.await_args_list
assert expected_call in press_mock.call_args_list


@pytest.mark.asyncio
async def test__restart_processes(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
stop_mock = mocker.patch.object(control, "_stop_processes", new_callable=AsyncMock)
start_mock = mocker.patch.object(
control, "_start_processes", new_callable=AsyncMock
)
sleep_mock = mocker.patch(ASYNCIO_SLEEP_PATCH, new_callable=AsyncMock)
stop_mock = patch.object(control, "_stop_processes").start()
start_mock = patch.object(control, "_start_processes").start()
sleep_mock = patch(ASYNCIO_SLEEP_PATCH).start()

manager = Mock()
manager.attach_mock(stop_mock, "stop_mock")
manager.attach_mock(start_mock, "start_mock")
Expand All @@ -184,6 +184,10 @@ async def test__restart_processes(

assert expected_calls == manager.method_calls

await stop_mock.stop()
await start_mock.stop()
await sleep_mock.stop()


# Test [start, stop, restart]_processes

Expand All @@ -192,13 +196,13 @@ async def test__restart_processes(
async def test__press_buttons(
control: OdinProcServControl, mocker: MockerFixture
) -> None:
caput_mock = mocker.patch(ODINPROCSERV_PATCH + ".caput", new_callable=AsyncMock)
prefixes = ["A", "B"]
suffix = "START"
with patch(ODINPROCSERV_PATCH + ".caput") as caput_mock:
prefixes = ["A", "B"]
suffix = "START"

await control._press_buttons(prefixes, suffix)
await control._press_buttons(prefixes, suffix)

caput_mock.assert_called_once_with(["A:START", "B:START"], 1)
caput_mock.assert_called_once_with(["A:START", "B:START"], 1)


def test_format_process_name():
Expand Down

0 comments on commit 6056b44

Please sign in to comment.