Skip to content

Commit

Permalink
Use actual alarm_severity and timestamp in assert_reading if not prov…
Browse files Browse the repository at this point in the history
…ided to improve error diff
  • Loading branch information
jsouter committed Feb 17, 2025
1 parent 775527d commit 9e23079
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 83 deletions.
11 changes: 7 additions & 4 deletions src/ophyd_async/testing/_assert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from contextlib import AbstractContextManager
from typing import Any
from unittest.mock import ANY, Mock, call
from unittest.mock import Mock, call

import pytest
from bluesky.protocols import Reading
Expand Down Expand Up @@ -55,12 +55,15 @@ async def assert_reading(


def _assert_readings_approx_equal(expected, actual):
assert expected.keys() == actual.keys()
approx_expected_reading = {
k: dict(
v,
value=approx_value(expected[k]["value"]),
timestamp=pytest.approx(expected[k].get("timestamp", ANY), rel=0.1),
alarm_severity=pytest.approx(expected[k].get("alarm_severity", ANY)),
value=approx_value(v["value"]),
timestamp=pytest.approx(v["timestamp"], rel=0.1)
if "timestamp" in v
else actual[k]["timestamp"],
alarm_severity=v.get("alarm_severity", actual[k]["alarm_severity"]),
)
for k, v in expected.items()
}
Expand Down
125 changes: 46 additions & 79 deletions tests/core/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,93 +410,57 @@ async def test_assert_configuration_everything(
{
"everything-device-a_int": {
"value": 1,
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-a_float": {
"value": 1.234,
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-a_str": {
"value": "test_string",
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-a_bool": {
"value": True,
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-a_enum": {
"value": "Bbb",
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-boola": {
"value": _array_vals["boola"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-int8a": {
"value": _array_vals["int8a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-uint8a": {
"value": _array_vals["uint8a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-int16a": {
"value": _array_vals["int16a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-uint16a": {
"value": _array_vals["uint16a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-int32a": {
"value": _array_vals["int32a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-uint32a": {
"value": _array_vals["uint32a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-int64a": {
"value": _array_vals["int64a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-uint64a": {
"value": _array_vals["uint64a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-float32a": {
"value": _array_vals["float32a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-float64a": {
"value": _array_vals["float64a"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-stra": {
"value": ["one", "two", "three"],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-enuma": {
"value": [ExampleEnum.A, ExampleEnum.C],
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-table": {
"value": ExampleTable(
Expand All @@ -506,13 +470,9 @@ async def test_assert_configuration_everything(
a_str=["Hello", "World", "Foo", "Bar"],
a_enum=[ExampleEnum.A, ExampleEnum.B, ExampleEnum.A, ExampleEnum.C],
),
"timestamp": ANY,
"alarm_severity": 0,
},
"everything-device-ndarray": {
"value": np.array([[1, 2, 3], [4, 5, 6]]),
"timestamp": ANY,
"alarm_severity": 0,
},
},
)
Expand All @@ -527,8 +487,6 @@ async def test_assert_reading_everything(
{
"everything-device-a_int": {
"value": 1,
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -537,8 +495,6 @@ async def test_assert_reading_everything(
{
"everything-device-a_float": {
"value": 1.234,
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -547,8 +503,6 @@ async def test_assert_reading_everything(
{
"everything-device-a_str": {
"value": "test_string",
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -557,8 +511,6 @@ async def test_assert_reading_everything(
{
"everything-device-a_bool": {
"value": True,
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -567,8 +519,6 @@ async def test_assert_reading_everything(
{
"everything-device-boola": {
"value": _array_vals["boola"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -577,8 +527,6 @@ async def test_assert_reading_everything(
{
"everything-device-a_enum": {
"value": ExampleEnum.B,
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -587,8 +535,6 @@ async def test_assert_reading_everything(
{
"everything-device-int8a": {
"value": _array_vals["int8a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -597,8 +543,6 @@ async def test_assert_reading_everything(
{
"everything-device-uint8a": {
"value": _array_vals["uint8a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -607,8 +551,6 @@ async def test_assert_reading_everything(
{
"everything-device-int16a": {
"value": _array_vals["int16a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -617,8 +559,6 @@ async def test_assert_reading_everything(
{
"everything-device-uint16a": {
"value": _array_vals["uint16a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -627,8 +567,6 @@ async def test_assert_reading_everything(
{
"everything-device-int32a": {
"value": _array_vals["int32a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -637,8 +575,6 @@ async def test_assert_reading_everything(
{
"everything-device-uint32a": {
"value": _array_vals["uint32a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -647,8 +583,6 @@ async def test_assert_reading_everything(
{
"everything-device-int64a": {
"value": _array_vals["int64a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -657,8 +591,6 @@ async def test_assert_reading_everything(
{
"everything-device-uint64a": {
"value": _array_vals["uint64a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -667,8 +599,6 @@ async def test_assert_reading_everything(
{
"everything-device-float32a": {
"value": _array_vals["float32a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -677,8 +607,6 @@ async def test_assert_reading_everything(
{
"everything-device-float64a": {
"value": _array_vals["float64a"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -687,8 +615,6 @@ async def test_assert_reading_everything(
{
"everything-device-stra": {
"value": ["one", "two", "three"],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -697,8 +623,6 @@ async def test_assert_reading_everything(
{
"everything-device-enuma": {
"value": [ExampleEnum.A, ExampleEnum.C],
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -713,8 +637,6 @@ async def test_assert_reading_everything(
a_str=["Hello", "World", "Foo", "Bar"],
a_enum=[ExampleEnum.A, ExampleEnum.B, ExampleEnum.A, ExampleEnum.C],
),
"timestamp": ANY,
"alarm_severity": 0,
}
},
)
Expand All @@ -723,11 +645,56 @@ async def test_assert_reading_everything(
{
"everything-device-ndarray": {
"value": np.array(([1, 2, 3], [4, 5, 6])),
"timestamp": ANY,
}
},
)


async def test_assert_reading_default_metadata(
one_of_everything_device: OneOfEverythingDevice,
):
await assert_reading(
one_of_everything_device.a_int,
{"everything-device-a_int": {"value": 1}},
)
await assert_reading(
one_of_everything_device.a_int,
{"everything-device-a_int": {"value": 1, "timestamp": ANY}},
)
await assert_reading(
one_of_everything_device.a_int,
{"everything-device-a_int": {"value": 1, "timestamp": time.monotonic()}},
)
await assert_reading(
one_of_everything_device.a_int,
{
"everything-device-a_int": {
"value": 1,
"timestamp": time.monotonic(),
"alarm_severity": 0,
}
},
)
with pytest.raises(AssertionError):
await assert_reading(
one_of_everything_device.a_int,
{
"everything-device-a_int": {
"value": 1,
"timestamp": 2 * time.monotonic(),
}
},
)
with pytest.raises(AssertionError):
await assert_reading(
one_of_everything_device.a_int,
{"everything-device-a_int": {"value": 2}},
)
with pytest.raises(AssertionError):
await assert_reading(
one_of_everything_device.a_int,
{"everything-device-a_int": {"value": 1, "alarm_severity": 1}},
)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 9e23079

Please sign in to comment.