diff --git a/tests/data_service/test_state_manager.py b/tests/data_service/test_state_manager.py index f5674df..ba593c0 100644 --- a/tests/data_service/test_state_manager.py +++ b/tests/data_service/test_state_manager.py @@ -9,8 +9,13 @@ import pydase.units as u from pydase.data_service.state_manager import StateManager +class SubService(pydase.DataService): + name = "SubService" + + class Service(pydase.DataService): def __init__(self, **kwargs: Any) -> None: + self.subservice = SubService() self.some_unit: u.Quantity = 1.2 * u.units.A self.some_float = 1.0 self._name = "Service" @@ -21,37 +26,18 @@ class Service(pydase.DataService): return self._name -CURRENT_STATE = { - "name": { - "type": "str", - "value": "Service", - "readonly": True, - "doc": None, - }, - "some_float": { - "type": "float", - "value": 1.0, - "readonly": False, - "doc": None, - }, - "some_unit": { - "type": "Quantity", - "value": {"magnitude": 1.2, "unit": "A"}, - "readonly": False, - "doc": None, - }, -} +CURRENT_STATE = Service().serialize() LOAD_STATE = { "name": { "type": "str", - "value": "Service", + "value": "Another name", "readonly": True, "doc": None, }, "some_float": { "type": "int", - "value": 1, + "value": 10, "readonly": False, "doc": None, }, @@ -61,6 +47,25 @@ LOAD_STATE = { "readonly": False, "doc": None, }, + "subservice": { + "type": "DataService", + "value": { + "name": { + "type": "str", + "value": "SubService", + "readonly": False, + "doc": None, + } + }, + "readonly": False, + "doc": None, + }, + "removed_attr": { + "type": "str", + "value": "removed", + "readonly": False, + "doc": None, + }, } @@ -76,7 +81,7 @@ def test_save_state(tmp_path: Path): assert file.read_text() == json.dumps(CURRENT_STATE, indent=4) -def test_load_state(tmp_path: Path): +def test_load_state(tmp_path: Path, caplog: LogCaptureFixture): # Create a StateManager instance with a temporary file file = tmp_path / "test_state.json" @@ -87,7 +92,26 @@ def test_load_state(tmp_path: Path): service = Service() manager = StateManager(service=service, filename=str(file)) manager.load_state() - assert service.some_unit == u.Quantity(12, "A") + + assert service.some_unit == u.Quantity(12, "A") # has changed + 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.subservice.name == "SubService" # didn't change + + assert "Service.some_unit changed to 12.0 A!" in caplog.text + assert ( + "Attribute 'name' is read-only. Ignoring value from JSON file..." in caplog.text + ) + assert ( + "Attribute type of 'some_float' changed from 'int' to 'float'. " + "Ignoring value from JSON file..." + ) in caplog.text + assert ( + "Attribute type of 'removed_attr' changed from 'str' to None. " + "Ignoring value from JSON file..." in caplog.text + ) + assert "Value of attribute 'subservice.name' has not changed..." in caplog.text def test_filename_warning(tmp_path: Path, caplog: LogCaptureFixture):