Skip to content

Commit

Permalink
(#325) Expose mock itself rather than the assert_mock_put_called_with
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram authored and abbiemery committed May 28, 2024
1 parent c64b876 commit 331b79a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/ophyd_async/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
walk_rw_signals,
)
from .flyer import HardwareTriggeredFlyable, TriggerLogic
from .mock_signal_backend import (
MockSignalBackend,
)
from .mock_signal_backend import MockSignalBackend
from .mock_signal_utils import (
assert_mock_put_called_with,
callback_on_mock_put,
get_mock_put,
mock_puts_blocked,
reset_mock_put_calls,
set_mock_put_proceeds,
Expand Down Expand Up @@ -70,7 +68,7 @@
)

__all__ = [
"assert_mock_put_called_with",
"get_mock_put",
"callback_on_mock_put",
"mock_puts_blocked",
"set_mock_values",
Expand Down
7 changes: 3 additions & 4 deletions src/ophyd_async/core/mock_signal_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from contextlib import asynccontextmanager, contextmanager
from typing import Any, Callable, Iterable
from unittest.mock import ANY, Mock
from unittest.mock import Mock

from ophyd_async.core.signal import Signal
from ophyd_async.core.utils import T
Expand Down Expand Up @@ -41,9 +41,8 @@ async def mock_puts_blocked(*signals: Signal):
set_mock_put_proceeds(signal, True)


def assert_mock_put_called_with(signal: Signal, value: Any, wait=ANY, timeout=ANY):
backend = _get_mock_signal_backend(signal)
backend.put_mock.assert_called_with(value, wait=wait, timeout=timeout)
def get_mock_put(signal: Signal) -> Mock:
return _get_mock_signal_backend(signal).put_mock


def reset_mock_put_calls(signal: Signal):
Expand Down
27 changes: 10 additions & 17 deletions tests/core/test_mock_signal_backend.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import asyncio
import re
from itertools import repeat
from unittest.mock import MagicMock, call
from unittest.mock import ANY, MagicMock, call

import pytest

from ophyd_async.core import MockSignalBackend, SignalRW
from ophyd_async.core.device import Device, DeviceCollector
from ophyd_async.core.mock_signal_utils import (
assert_mock_put_called_with,
callback_on_mock_put,
get_mock_put,
mock_puts_blocked,
reset_mock_put_calls,
set_mock_put_proceeds,
set_mock_value,
set_mock_values,
)
from ophyd_async.core.signal import (
SignalW,
soft_signal_r_and_setter,
soft_signal_rw,
)
from ophyd_async.core.signal import SignalW, soft_signal_r_and_setter, soft_signal_rw
from ophyd_async.core.soft_signal_backend import SoftSignalBackend
from ophyd_async.epics.signal.signal import epics_signal_r, epics_signal_rw

Expand Down Expand Up @@ -116,7 +112,7 @@ async def test_mock_utils_throw_error_if_backend_isnt_mock_signal_backend():
set_mock_value(signal, 10)
exc_msgs.append(str(exc.value))
with pytest.raises(AssertionError) as exc:
assert_mock_put_called_with(signal, 10)
get_mock_put(signal).assert_called_once_with(10)
exc_msgs.append(str(exc.value))
with pytest.raises(AssertionError) as exc:
async with mock_puts_blocked(signal):
Expand All @@ -141,16 +137,13 @@ async def test_mock_utils_throw_error_if_backend_isnt_mock_signal_backend():
)


async def test_assert_mock_put_called_with():
async def test_get_mock_put():
mock_signal = epics_signal_rw(str, "READ_PV", "WRITE_PV", name="mock_name")
await mock_signal.connect(mock=True)
await mock_signal.set("test_value", wait=True, timeout=100)

# can leave out kwargs
assert_mock_put_called_with(mock_signal, "test_value")
assert_mock_put_called_with(mock_signal, "test_value", wait=True)
assert_mock_put_called_with(mock_signal, "test_value", timeout=100)
assert_mock_put_called_with(mock_signal, "test_value", wait=True, timeout=100)
mock = get_mock_put(mock_signal)
mock.assert_called_once_with("test_value", wait=True, timeout=100)

def err_text(text, wait, timeout):
return (
Expand All @@ -166,7 +159,7 @@ def err_text(text, wait, timeout):
("test_value", False, 0), # wait and timeout wrong
]:
with pytest.raises(AssertionError) as exc:
assert_mock_put_called_with(mock_signal, text, wait=wait, timeout=timeout)
mock.assert_called_once_with(text, wait=wait, timeout=timeout)
for err_substr in err_text(text, wait, timeout):
assert err_substr in str(exc.value)

Expand Down Expand Up @@ -302,10 +295,10 @@ async def test_set_mock_values_exhausted_fails(mock_signals):
async def test_reset_mock_put_calls(mock_signals):
signal1, signal2 = mock_signals
await signal1.set("test_value", wait=True, timeout=1)
assert_mock_put_called_with(signal1, "test_value")
get_mock_put(signal1).assert_called_with("test_value", wait=ANY, timeout=ANY)
reset_mock_put_calls(signal1)
with pytest.raises(AssertionError) as exc:
assert_mock_put_called_with(signal1, "test_value")
get_mock_put(signal1).assert_called_with("test_value", wait=ANY, timeout=ANY)
# Replacing spaces because they change between runners
# (e.g the github actions runner has more)
assert str(exc.value).replace(" ", "").replace("\n", "") == (
Expand Down

0 comments on commit 331b79a

Please sign in to comment.