mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 08:20:02 +02:00
updates StateManager tests
This commit is contained in:
parent
784d49d90c
commit
aed0dd9493
@ -6,6 +6,7 @@ from pytest import LogCaptureFixture
|
|||||||
|
|
||||||
import pydase
|
import pydase
|
||||||
import pydase.units as u
|
import pydase.units as u
|
||||||
|
from pydase.components.coloured_enum import ColouredEnum
|
||||||
from pydase.data_service.state_manager import StateManager
|
from pydase.data_service.state_manager import StateManager
|
||||||
|
|
||||||
|
|
||||||
@ -13,22 +14,56 @@ class SubService(pydase.DataService):
|
|||||||
name = "SubService"
|
name = "SubService"
|
||||||
|
|
||||||
|
|
||||||
|
class State(ColouredEnum):
|
||||||
|
RUNNING = "#0000FF80"
|
||||||
|
COMPLETED = "hsl(120, 100%, 50%)"
|
||||||
|
FAILED = "hsla(0, 100%, 50%, 0.7)"
|
||||||
|
|
||||||
|
|
||||||
class Service(pydase.DataService):
|
class Service(pydase.DataService):
|
||||||
def __init__(self, **kwargs: Any) -> None:
|
def __init__(self, **kwargs: Any) -> None:
|
||||||
self.subservice = SubService()
|
self.subservice = SubService()
|
||||||
self.some_unit: u.Quantity = 1.2 * u.units.A
|
self.some_unit: u.Quantity = 1.2 * u.units.A
|
||||||
self.some_float = 1.0
|
self.some_float = 1.0
|
||||||
|
self.list_attr = [1.0, 2.0]
|
||||||
|
self._property_attr = 1337.0
|
||||||
self._name = "Service"
|
self._name = "Service"
|
||||||
|
self._state = State.RUNNING
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def property_attr(self) -> float:
|
||||||
|
return self._property_attr
|
||||||
|
|
||||||
|
@property_attr.setter
|
||||||
|
def property_attr(self, value: float) -> None:
|
||||||
|
self._property_attr = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self) -> State:
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
@state.setter
|
||||||
|
def state(self, value: State) -> None:
|
||||||
|
self._state = value
|
||||||
|
|
||||||
|
|
||||||
CURRENT_STATE = Service().serialize()
|
CURRENT_STATE = Service().serialize()
|
||||||
|
|
||||||
LOAD_STATE = {
|
LOAD_STATE = {
|
||||||
|
"list_attr": {
|
||||||
|
"type": "list",
|
||||||
|
"value": [
|
||||||
|
{"type": "float", "value": 1.4, "readonly": False, "doc": None},
|
||||||
|
{"type": "float", "value": 2.0, "readonly": False, "doc": None},
|
||||||
|
],
|
||||||
|
"readonly": False,
|
||||||
|
"doc": None,
|
||||||
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "str",
|
"type": "str",
|
||||||
"value": "Another name",
|
"value": "Another name",
|
||||||
@ -41,12 +76,29 @@ LOAD_STATE = {
|
|||||||
"readonly": False,
|
"readonly": False,
|
||||||
"doc": None,
|
"doc": None,
|
||||||
},
|
},
|
||||||
|
"property_attr": {
|
||||||
|
"type": "float",
|
||||||
|
"value": 1337.1,
|
||||||
|
"readonly": False,
|
||||||
|
"doc": None,
|
||||||
|
},
|
||||||
"some_unit": {
|
"some_unit": {
|
||||||
"type": "Quantity",
|
"type": "Quantity",
|
||||||
"value": {"magnitude": 12.0, "unit": "A"},
|
"value": {"magnitude": 12.0, "unit": "A"},
|
||||||
"readonly": False,
|
"readonly": False,
|
||||||
"doc": None,
|
"doc": None,
|
||||||
},
|
},
|
||||||
|
"state": {
|
||||||
|
"type": "ColouredEnum",
|
||||||
|
"value": "FAILED",
|
||||||
|
"readonly": True,
|
||||||
|
"doc": None,
|
||||||
|
"enum": {
|
||||||
|
"RUNNING": "#0000FF80",
|
||||||
|
"COMPLETED": "hsl(120, 100%, 50%)",
|
||||||
|
"FAILED": "hsla(0, 100%, 50%, 0.7)",
|
||||||
|
},
|
||||||
|
},
|
||||||
"subservice": {
|
"subservice": {
|
||||||
"type": "DataService",
|
"type": "DataService",
|
||||||
"value": {
|
"value": {
|
||||||
@ -94,15 +146,16 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
|
|||||||
manager.load_state()
|
manager.load_state()
|
||||||
|
|
||||||
assert service.some_unit == u.Quantity(12, "A") # has changed
|
assert service.some_unit == u.Quantity(12, "A") # has changed
|
||||||
|
assert service.list_attr[0] == 1.4 # has changed
|
||||||
|
assert service.list_attr[1] == 2.0 # has not changed
|
||||||
|
assert service.property_attr == 1337.1 # has changed
|
||||||
|
assert service.state == State.FAILED # has changed
|
||||||
assert service.name == "Service" # has not changed as readonly
|
assert service.name == "Service" # has not changed as readonly
|
||||||
assert service.some_float == 1.0 # has not changed due to different type
|
assert service.some_float == 1.0 # has not changed due to different type
|
||||||
assert service.some_float == 1.0 # has not changed due to different type
|
|
||||||
assert service.subservice.name == "SubService" # didn't change
|
assert service.subservice.name == "SubService" # didn't change
|
||||||
|
|
||||||
assert "Service.some_unit changed to 12.0 A!" in caplog.text
|
assert "Service.some_unit changed to 12.0 A!" in caplog.text
|
||||||
assert (
|
assert "Attribute 'name' is read-only. Ignoring new value..." in caplog.text
|
||||||
"Attribute 'name' is read-only. Ignoring value from JSON file..." in caplog.text
|
|
||||||
)
|
|
||||||
assert (
|
assert (
|
||||||
"Attribute type of 'some_float' changed from 'int' to 'float'. "
|
"Attribute type of 'some_float' changed from 'int' to 'float'. "
|
||||||
"Ignoring value from JSON file..."
|
"Ignoring value from JSON file..."
|
||||||
@ -144,9 +197,7 @@ def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture):
|
|||||||
service = Service()
|
service = Service()
|
||||||
manager = StateManager(service=service, filename=str(file))
|
manager = StateManager(service=service, filename=str(file))
|
||||||
manager.load_state()
|
manager.load_state()
|
||||||
assert (
|
assert "Attribute 'name' is read-only. Ignoring new value..." in caplog.text
|
||||||
"Attribute 'name' is read-only. Ignoring value from JSON file..." in caplog.text
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
|
def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user