|
1 | | -import asyncio |
2 | | -import enum |
3 | | - |
4 | 1 | import numpy as np |
5 | 2 | import pytest |
6 | 3 | from pvi.device import SignalR |
7 | 4 | from pydantic import ValidationError |
8 | 5 |
|
9 | | -from fastcs.attributes import Attribute, AttrR, AttrRW |
10 | | -from fastcs.controller import Controller |
11 | | -from fastcs.datatypes import Bool, Enum, Float, Int, String |
12 | | -from fastcs.launch import FastCS |
13 | | -from fastcs.util import ( |
14 | | - numpy_to_fastcs_datatype, |
15 | | - snake_to_pascal, |
16 | | - validate_hinted_attributes, |
17 | | -) |
| 6 | +from fastcs.datatypes import Bool, Float, Int, String |
| 7 | +from fastcs.util import numpy_to_fastcs_datatype, snake_to_pascal |
18 | 8 |
|
19 | 9 |
|
20 | 10 | def test_snake_to_pascal(): |
@@ -66,129 +56,3 @@ def test_pvi_validation_error(): |
66 | 56 | ) |
67 | 57 | def test_numpy_to_fastcs_datatype(numpy_type, fastcs_datatype): |
68 | 58 | assert fastcs_datatype == numpy_to_fastcs_datatype(numpy_type) |
69 | | - |
70 | | - |
71 | | -@pytest.mark.asyncio |
72 | | -async def test_hinted_attributes_verified(): |
73 | | - class ControllerWithWrongType(Controller): |
74 | | - hinted_wrong_type: AttrR[int] |
75 | | - |
76 | | - async def initialise(self): |
77 | | - self.hinted_wrong_type = AttrR(Float()) # type: ignore |
78 | | - |
79 | | - controller = ControllerWithWrongType() |
80 | | - await controller.initialise() |
81 | | - with pytest.raises(RuntimeError) as excinfo: |
82 | | - validate_hinted_attributes(controller) |
83 | | - |
84 | | - assert str(excinfo.value) == ( |
85 | | - "Controller 'ControllerWithWrongType' introspection of hinted attribute " |
86 | | - "'hinted_wrong_type' does not match defined datatype. " |
87 | | - "Expected 'int', got 'float'." |
88 | | - ) |
89 | | - |
90 | | - class ControllerWithMissingAttr(Controller): |
91 | | - hinted_int_missing: AttrR[int] |
92 | | - |
93 | | - controller = ControllerWithMissingAttr() |
94 | | - await controller.initialise() |
95 | | - with pytest.raises(RuntimeError) as excinfo: |
96 | | - validate_hinted_attributes(controller) |
97 | | - |
98 | | - assert str(excinfo.value) == ( |
99 | | - "Controller `ControllerWithMissingAttr` failed to introspect hinted attribute " |
100 | | - "`hinted_int_missing` during initialisation" |
101 | | - ) |
102 | | - |
103 | | - class ControllerAttrWrongAccessMode(Controller): |
104 | | - hinted: AttrR[int] |
105 | | - |
106 | | - async def initialise(self): |
107 | | - self.hinted = AttrRW(Int()) |
108 | | - self.attributes["hinted"] = self.hinted |
109 | | - |
110 | | - controller = ControllerAttrWrongAccessMode() |
111 | | - await controller.initialise() |
112 | | - with pytest.raises(RuntimeError) as excinfo: |
113 | | - validate_hinted_attributes(controller) |
114 | | - |
115 | | - assert str(excinfo.value) == ( |
116 | | - "Controller 'ControllerAttrWrongAccessMode' introspection of hinted attribute " |
117 | | - "'hinted' does not match defined access mode. Expected 'AttrR', got 'AttrRW'." |
118 | | - ) |
119 | | - |
120 | | - class MyEnum(enum.Enum): |
121 | | - A = 0 |
122 | | - B = 1 |
123 | | - |
124 | | - class MyEnum2(enum.Enum): |
125 | | - A = 2 |
126 | | - B = 3 |
127 | | - |
128 | | - class ControllerWrongEnumClass(Controller): |
129 | | - hinted_enum: AttrRW[MyEnum] = AttrRW(Enum(MyEnum2)) |
130 | | - |
131 | | - controller = ControllerWrongEnumClass() |
132 | | - await controller.initialise() |
133 | | - with pytest.raises(RuntimeError) as excinfo: |
134 | | - validate_hinted_attributes(controller) |
135 | | - |
136 | | - assert str(excinfo.value) == ( |
137 | | - "Controller 'ControllerWrongEnumClass' introspection of hinted attribute " |
138 | | - "'hinted_enum' does not match defined datatype. " |
139 | | - "Expected 'MyEnum', got 'MyEnum2'." |
140 | | - ) |
141 | | - |
142 | | - |
143 | | -def test_hinted_attributes_verified_on_subcontrollers(): |
144 | | - loop = asyncio.get_event_loop() |
145 | | - |
146 | | - class ControllerWithWrongType(Controller): |
147 | | - hinted_missing: AttrR[int] |
148 | | - |
149 | | - async def connect(self): |
150 | | - return |
151 | | - |
152 | | - class TopController(Controller): |
153 | | - async def initialise(self): # why does this not get called? |
154 | | - subcontroller = ControllerWithWrongType() |
155 | | - self.add_sub_controller("MySubController", subcontroller) |
156 | | - |
157 | | - fastcs = FastCS(TopController(), [], loop) |
158 | | - with pytest.raises(RuntimeError, match="failed to introspect hinted attribute"): |
159 | | - fastcs.run() |
160 | | - |
161 | | - |
162 | | -def test_hinted_attribute_access_mode_verified(): |
163 | | - # test verification works with non-GenericAlias type hints |
164 | | - loop = asyncio.get_event_loop() |
165 | | - |
166 | | - class ControllerAttrWrongAccessMode(Controller): |
167 | | - read_attr: AttrR |
168 | | - |
169 | | - async def initialise(self): |
170 | | - self.read_attr = AttrRW(Int()) |
171 | | - |
172 | | - fastcs = FastCS(ControllerAttrWrongAccessMode(), [], loop) |
173 | | - with pytest.raises(RuntimeError, match="does not match defined access mode"): |
174 | | - fastcs.run() |
175 | | - |
176 | | - |
177 | | -@pytest.mark.asyncio |
178 | | -async def test_hinted_attributes_with_unspecified_access_mode(): |
179 | | - class ControllerUnspecifiedAccessMode(Controller): |
180 | | - unspecified_access_mode: Attribute |
181 | | - |
182 | | - async def initialise(self): |
183 | | - self.unspecified_access_mode = AttrRW(Int()) |
184 | | - |
185 | | - controller = ControllerUnspecifiedAccessMode() |
186 | | - await controller.initialise() |
187 | | - # no assertion thrown |
188 | | - with pytest.raises( |
189 | | - RuntimeError, |
190 | | - match=( |
191 | | - "does not match defined access mode. Expected 'Attribute', got 'AttrRW'" |
192 | | - ), |
193 | | - ): |
194 | | - validate_hinted_attributes(controller) |
0 commit comments