|
| 1 | +import asyncio |
| 2 | +import enum |
| 3 | + |
1 | 4 | import numpy as np |
2 | 5 | import pytest |
3 | 6 | from pvi.device import SignalR |
4 | 7 | from pydantic import ValidationError |
5 | 8 |
|
6 | | -from fastcs.datatypes import Bool, Float, Int, String |
| 9 | +from fastcs.attributes import AttrR, AttrRW |
| 10 | +from fastcs.backend import Backend |
| 11 | +from fastcs.controller import Controller |
| 12 | +from fastcs.datatypes import Bool, Enum, Float, Int, String |
7 | 13 | from fastcs.util import numpy_to_fastcs_datatype, snake_to_pascal |
8 | 14 |
|
9 | 15 |
|
@@ -56,3 +62,65 @@ def test_pvi_validation_error(): |
56 | 62 | ) |
57 | 63 | def test_numpy_to_fastcs_datatype(numpy_type, fastcs_datatype): |
58 | 64 | assert fastcs_datatype == numpy_to_fastcs_datatype(numpy_type) |
| 65 | + |
| 66 | + |
| 67 | +def test_hinted_attributes_verified(): |
| 68 | + loop = asyncio.get_event_loop() |
| 69 | + |
| 70 | + class ControllerWithWrongType(Controller): |
| 71 | + hinted_wrong_type: AttrR[int] |
| 72 | + |
| 73 | + async def initialise(self): |
| 74 | + self.hinted_wrong_type = AttrR(Float()) # type: ignore |
| 75 | + self.attributes["hinted_wrong_type"] = self.hinted_wrong_type |
| 76 | + |
| 77 | + with pytest.raises(RuntimeError) as excinfo: |
| 78 | + Backend(ControllerWithWrongType(), loop) |
| 79 | + assert str(excinfo.value) == ( |
| 80 | + "Controller 'ControllerWithWrongType' introspection of hinted attribute " |
| 81 | + "'hinted_wrong_type' does not match defined datatype. " |
| 82 | + "Expected 'int', got 'float'." |
| 83 | + ) |
| 84 | + |
| 85 | + class ControllerWithMissingAttr(Controller): |
| 86 | + hinted_int_missing: AttrR[int] |
| 87 | + |
| 88 | + with pytest.raises(RuntimeError) as excinfo: |
| 89 | + Backend(ControllerWithMissingAttr(), loop) |
| 90 | + assert str(excinfo.value) == ( |
| 91 | + "Controller `ControllerWithMissingAttr` failed to introspect hinted attribute " |
| 92 | + "`hinted_int_missing` during initialisation" |
| 93 | + ) |
| 94 | + |
| 95 | + class ControllerAttrWrongAccessMode(Controller): |
| 96 | + hinted: AttrR[int] |
| 97 | + |
| 98 | + async def initialise(self): |
| 99 | + self.hinted = AttrRW(Int()) |
| 100 | + self.attributes["hinted"] = self.hinted |
| 101 | + |
| 102 | + with pytest.raises(RuntimeError) as excinfo: |
| 103 | + Backend(ControllerAttrWrongAccessMode(), loop) |
| 104 | + assert str(excinfo.value) == ( |
| 105 | + "Controller 'ControllerAttrWrongAccessMode' introspection of hinted attribute " |
| 106 | + "'hinted' does not match defined access mode. Expected 'AttrR', got 'AttrRW'." |
| 107 | + ) |
| 108 | + |
| 109 | + class MyEnum(enum.Enum): |
| 110 | + A = 0 |
| 111 | + B = 1 |
| 112 | + |
| 113 | + class MyEnum2(enum.Enum): |
| 114 | + A = 2 |
| 115 | + B = 3 |
| 116 | + |
| 117 | + class ControllerWrongEnumClass(Controller): |
| 118 | + hinted_enum: AttrRW[MyEnum] = AttrRW(Enum(MyEnum2)) |
| 119 | + |
| 120 | + with pytest.raises(RuntimeError) as excinfo: |
| 121 | + Backend(ControllerWrongEnumClass(), loop) |
| 122 | + assert str(excinfo.value) == ( |
| 123 | + "Controller 'ControllerWrongEnumClass' introspection of hinted attribute " |
| 124 | + "'hinted_enum' does not match defined datatype. " |
| 125 | + "Expected 'MyEnum', got 'MyEnum2'." |
| 126 | + ) |
0 commit comments