|
27 | 27 | from labthings_fastapi.base_descriptor import DescriptorAddedToClassTwiceError |
28 | 28 | from labthings_fastapi.exceptions import MissingTypeError, NotConnectedToServerError |
29 | 29 | import labthings_fastapi as lt |
| 30 | +from labthings_fastapi.testing import create_thing_without_server |
30 | 31 | from .utilities import raises_or_is_caused_by |
31 | 32 |
|
32 | 33 |
|
@@ -298,3 +299,49 @@ def prop(self) -> bool: |
298 | 299 | Example.prop.add_to_fastapi(mocker.Mock(), example) |
299 | 300 | with pytest.raises(NotConnectedToServerError): |
300 | 301 | Example.prop.property_affordance(example, None) |
| 302 | + |
| 303 | + |
| 304 | +def test_readonly_metadata(): |
| 305 | + """Check read-only data propagates to the Thing Description.""" |
| 306 | + |
| 307 | + class Example(lt.Thing): |
| 308 | + prop: int = lt.property(default=0) |
| 309 | + ro_property: int = lt.property(default=0, readonly=True) |
| 310 | + |
| 311 | + @lt.property |
| 312 | + def ro_functional_property(self) -> int: |
| 313 | + """This property should be read-only as there's no setter.""" |
| 314 | + return 42 |
| 315 | + |
| 316 | + @lt.property |
| 317 | + def ro_functional_property_with_setter(self) -> int: |
| 318 | + return 42 |
| 319 | + |
| 320 | + @ro_functional_property_with_setter.setter |
| 321 | + def _set_ro_functional_property_with_setter(self, val: int) -> None: |
| 322 | + pass |
| 323 | + |
| 324 | + ro_functional_property_with_setter.readonly = True |
| 325 | + |
| 326 | + @lt.property |
| 327 | + def funcprop(self) -> int: |
| 328 | + return 42 |
| 329 | + |
| 330 | + @funcprop.setter |
| 331 | + def _set_funcprop(self, val: int) -> None: |
| 332 | + pass |
| 333 | + |
| 334 | + example = create_thing_without_server(Example) |
| 335 | + |
| 336 | + td = example.thing_description() |
| 337 | + |
| 338 | + # Check read-write properties are not read-only |
| 339 | + for name in ["prop", "funcprop"]: |
| 340 | + assert td.properties[name].readOnly is False |
| 341 | + |
| 342 | + for name in [ |
| 343 | + "ro_property", |
| 344 | + "ro_functional_property", |
| 345 | + "ro_functional_property_with_setter", |
| 346 | + ]: |
| 347 | + assert td.properties[name].readOnly is True |
0 commit comments