Skip to content

Commit

Permalink
adding docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Relm-Arrowny committed Apr 17, 2024
1 parent d35c499 commit 94d5016
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
36 changes: 23 additions & 13 deletions src/ophyd_async/core/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Stageable,
Subscribable,
)
from event_model.documents import DocumentType

from .async_status import AsyncStatus
from .device import Device
Expand Down Expand Up @@ -58,7 +59,6 @@ def __init__(
self, backend: SignalBackend[T], timeout: Optional[float] = DEFAULT_TIMEOUT
) -> None:
self._name = ""
self._long_name = None
self._timeout = timeout
self._init_backend = self._backend = backend

Expand Down Expand Up @@ -257,7 +257,7 @@ def set_sim_callback(signal: Signal[T], callback: ReadingValueCallback[T]) -> No
return _sim_backends[signal].set_callback(callback)


async def verify_readable(
async def _verify_readings(
func: Callable[[], dict[str, Any] | Awaitable[dict[str, Any]]],
expectation: Dict[str, Any],
) -> None:
Expand All @@ -269,35 +269,45 @@ async def verify_readable(
result = func()

for signal in expectation:
for field in expectation[signal]:
if field == "timestamp":
assert isinstance(result[signal]["timestamp"], float)
else:
assert result[signal][field] == expectation[signal][field]
assert result[signal] == expectation[signal]


async def assert_value(signal: SignalR[T], value: T) -> None:
"""Assert a signal value"""
"""Assert a signal value
Parameters
----------
signal:
Call get_valueand and compare it with expected value.
value:
the expected value from the signal.
Notes
-----
Example usage::
await assert_reading(signal, value)
"""
assert await signal.get_value() == value


async def assert_reading(
readable: Readable, reading: Dict[str, Reading] | dict[str, dict[str, Any]]
) -> None:
"""Assert readable by calling verify_readable"""
await verify_readable(readable.read, reading)
"""Assert readable by calling_verify_readings"""
await _verify_readings(readable.read, reading)


async def assert_configuration(
configurable: Configurable,
configuration: Dict[str, Reading] | dict[str, dict[str, Any]],
) -> None:
"""Assert readable generated by configurable.read_configuration
using verify_readable"""
await verify_readable(configurable.read_configuration, configuration)
using _verify_readings"""
await _verify_readings(configurable.read_configuration, configuration)


def assert_emitted(docs: Dict[str, list], **numbers: int):
def assert_emitted(docs: Dict[str, list[DocumentType]], **numbers: int):
"""Assert emitted"""
assert list(docs) == list(numbers)
assert {name: len(d) for name, d in docs.items()} == numbers
Expand Down
11 changes: 6 additions & 5 deletions tests/core/test_signal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import re
import time
from unittest.mock import ANY

import pytest

Expand Down Expand Up @@ -143,7 +144,7 @@ async def test_assert_value(sim_signal: SignalRW):
async def test_assert_reaading(sim_signal: SignalRW):
set_sim_value(sim_signal, 888)
dummy_reading = {
"sim_signal": {"alarm_severity": 0, "timestamp": 46709394.28, "value": 888}
"sim_signal": {"alarm_severity": 0, "timestamp": ANY, "value": 888}
}
await assert_reading(sim_signal, dummy_reading)

Expand Down Expand Up @@ -180,12 +181,12 @@ async def test_assert_configuration(sim_readable: DummyReadable):
dummy_config_reading = {
"sim_readable-mode": {
"alarm_severity": 0,
"timestamp": 123.2,
"timestamp": ANY,
"value": "super mode",
},
"sim_readable-mode2": {
"alarm_severity": 0,
"timestamp": 123.2,
"timestamp": ANY,
"value": "slow mode",
},
}
Expand All @@ -195,8 +196,8 @@ async def test_assert_configuration(sim_readable: DummyReadable):

something = DetWithConf(name="det")
dummy_config_reading1 = {
"det_c": {"value": 3, "timestamp": 171},
"det_d": {"value": 4, "timestamp": 1753},
"det_c": {"value": 3, "timestamp": ANY},
"det_d": {"value": 4, "timestamp": ANY},
}

await assert_configuration(something, dummy_config_reading1)

0 comments on commit 94d5016

Please sign in to comment.