-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added
RuntimeEnum
class (with metaclass), and added logic to `make_…
…converter`s to support them
- Loading branch information
1 parent
b2b8bba
commit 02522cf
Showing
7 changed files
with
227 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import pytest | ||
from epicscorelibs.ca import dbr | ||
from p4p import Value as P4PValue | ||
from p4p.nt import NTEnum | ||
|
||
from ophyd_async.core import RuntimeEnum | ||
from ophyd_async.epics._backend._aioca import make_converter as aioca_make_converter | ||
from ophyd_async.epics._backend._p4p import make_converter as p4p_make_converter | ||
from ophyd_async.epics.signal.signal import epics_signal_rw | ||
|
||
|
||
async def test_runtime_enum_behaviour(): | ||
rt_enum = RuntimeEnum["A", "B"] | ||
|
||
with pytest.raises(RuntimeError) as exc: | ||
rt_enum() | ||
assert str(exc.value) == "RuntimeEnum cannot be instantiated" | ||
|
||
assert issubclass(rt_enum, RuntimeEnum) | ||
assert issubclass(rt_enum, RuntimeEnum["A", "B"]) | ||
assert issubclass(rt_enum, RuntimeEnum["B", "A"]) | ||
|
||
rt_enum_reversed_args = RuntimeEnum["B", "A"] | ||
assert rt_enum == rt_enum_reversed_args | ||
assert str(rt_enum) == "RuntimeEnum['A', 'B']" | ||
assert str(RuntimeEnum) == "RuntimeEnum" | ||
|
||
# The order of the choices is not important | ||
assert str(rt_enum_reversed_args) == "RuntimeEnum['A', 'B']" | ||
|
||
with pytest.raises(TypeError) as exc: | ||
RuntimeEnum["A", "B", "A"] | ||
assert str(exc.value) == "Duplicate elements in runtime enum choices." | ||
|
||
|
||
async def test_ca_runtime_enum_converter(): | ||
class EpicsValue: | ||
def __init__(self): | ||
self.name = "test" | ||
self.ok = (True,) | ||
self.errorcode = 0 | ||
self.datatype = dbr.DBR_ENUM | ||
self.element_count = 1 | ||
self.severity = 0 | ||
self.status = 0 | ||
self.raw_stamp = (0,) | ||
self.timestamp = 0 | ||
self.datetime = 0 | ||
self.enums = ["A", "B", "C"] # More than the runtime enum | ||
|
||
epics_value = EpicsValue() | ||
rt_enum = RuntimeEnum["A", "B"] | ||
converter = aioca_make_converter( | ||
rt_enum, values={"READ_PV": epics_value, "WRITE_PV": epics_value} | ||
) | ||
assert converter.choices == {"A": "A", "B": "B"} | ||
|
||
|
||
async def test_pva_runtime_enum_converter(): | ||
enum_type = NTEnum.buildType() | ||
epics_value = P4PValue( | ||
enum_type, | ||
{ | ||
"value.choices": ["A", "B", "C"], | ||
}, | ||
) | ||
rt_enum = RuntimeEnum["A", "B"] | ||
converter = p4p_make_converter( | ||
rt_enum, values={"READ_PV": epics_value, "WRITE_PV": epics_value} | ||
) | ||
assert converter.choices == ("A", "B") | ||
|
||
|
||
async def test_runtime_enum_signal(): | ||
signal_rw_pva = epics_signal_rw( | ||
RuntimeEnum["A1", "B1"], "ca://RW_PV", name="signal" | ||
) | ||
signal_rw_ca = epics_signal_rw(RuntimeEnum["A2", "B2"], "ca://RW_PV", name="signal") | ||
await signal_rw_pva.connect(mock=True) | ||
await signal_rw_ca.connect(mock=True) | ||
await signal_rw_pva.get_value() == "A1" | ||
await signal_rw_ca.get_value() == "A2" | ||
await signal_rw_pva.set("B1") | ||
await signal_rw_ca.set("B2") | ||
await signal_rw_pva.get_value() == "B1" | ||
await signal_rw_ca.get_value() == "B2" | ||
|
||
# Will accept string values even if they're not in the runtime enum | ||
await signal_rw_pva.set("C1") | ||
await signal_rw_ca.set("C2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters