From c891642bda1b478997296bfdef07820e785e32af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Thu, 30 Nov 2023 17:17:41 +0100 Subject: [PATCH] updates tests --- tests/components/test_coloured_enum.py | 15 +- tests/components/test_number_slider.py | 47 +- tests/data_service/test_data_service_cache.py | 57 +- tests/data_service/test_state_manager.py | 7 +- tests/data_service/test_task_manager.py | 25 +- tests/test_DataService_attribute_callbacks.py | 215 ++++--- tests/test_properties.py | 577 +++++++++--------- tests/test_units.py | 61 +- tests/utils/test_serializer.py | 9 +- tests/utils/test_warnings.py | 15 +- 10 files changed, 563 insertions(+), 465 deletions(-) diff --git a/tests/components/test_coloured_enum.py b/tests/components/test_coloured_enum.py index 32d5e9b..2e90884 100644 --- a/tests/components/test_coloured_enum.py +++ b/tests/components/test_coloured_enum.py @@ -1,7 +1,8 @@ -from pytest import LogCaptureFixture - from pydase.components.coloured_enum import ColouredEnum from pydase.data_service.data_service import DataService +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager +from pytest import LogCaptureFixture def test_ColouredEnum(caplog: LogCaptureFixture) -> None: @@ -21,14 +22,16 @@ def test_ColouredEnum(caplog: LogCaptureFixture) -> None: # do something ... self._status = value - service = ServiceClass() + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - service.status = MyStatus.FAILING + service_instance.status = MyStatus.FAILING - assert "ServiceClass.status changed to MyStatus.FAILING" in caplog.text + assert "'status' changed to 'MyStatus.FAILING'" in caplog.text -def test_warning(caplog: LogCaptureFixture) -> None: # noqa +def test_warning(caplog: LogCaptureFixture) -> None: class MyStatus(ColouredEnum): RUNNING = "#00FF00" FAILING = "#FF0000" diff --git a/tests/components/test_number_slider.py b/tests/components/test_number_slider.py index 1cec963..44679bf 100644 --- a/tests/components/test_number_slider.py +++ b/tests/components/test_number_slider.py @@ -1,7 +1,8 @@ -from pytest import LogCaptureFixture - from pydase.components.number_slider import NumberSlider from pydase.data_service.data_service import DataService +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager +from pytest import LogCaptureFixture def test_NumberSlider(caplog: LogCaptureFixture) -> None: @@ -9,35 +10,37 @@ def test_NumberSlider(caplog: LogCaptureFixture) -> None: number_slider = NumberSlider(1, 0, 10, 1) int_number_slider = NumberSlider(1, 0, 10, 1, "int") - service = ServiceClass() + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - assert service.number_slider.value == 1 - assert isinstance(service.number_slider.value, float) - assert service.number_slider.min == 0 - assert isinstance(service.number_slider.min, float) - assert service.number_slider.max == 10 - assert isinstance(service.number_slider.max, float) - assert service.number_slider.step_size == 1 - assert isinstance(service.number_slider.step_size, float) + assert service_instance.number_slider.value == 1 + assert isinstance(service_instance.number_slider.value, float) + assert service_instance.number_slider.min == 0 + assert isinstance(service_instance.number_slider.min, float) + assert service_instance.number_slider.max == 10 + assert isinstance(service_instance.number_slider.max, float) + assert service_instance.number_slider.step_size == 1 + assert isinstance(service_instance.number_slider.step_size, float) - assert service.int_number_slider.value == 1 - assert isinstance(service.int_number_slider.value, int) - assert service.int_number_slider.step_size == 1 - assert isinstance(service.int_number_slider.step_size, int) + assert service_instance.int_number_slider.value == 1 + assert isinstance(service_instance.int_number_slider.value, int) + assert service_instance.int_number_slider.step_size == 1 + assert isinstance(service_instance.int_number_slider.step_size, int) - service.number_slider.value = 10.0 - service.int_number_slider.value = 10.1 + service_instance.number_slider.value = 10.0 + service_instance.int_number_slider.value = 10.1 - assert "ServiceClass.number_slider.value changed to 10.0" in caplog.text - assert "ServiceClass.int_number_slider.value changed to 10" in caplog.text + assert "'number_slider.value' changed to '10.0'" in caplog.text + assert "'int_number_slider.value' changed to '10'" in caplog.text caplog.clear() - service.number_slider.min = 1.1 + service_instance.number_slider.min = 1.1 - assert "ServiceClass.number_slider.min changed to 1.1" in caplog.text + assert "'number_slider.min' changed to '1.1'" in caplog.text -def test_init_error(caplog: LogCaptureFixture) -> None: # noqa +def test_init_error(caplog: LogCaptureFixture) -> None: number_slider = NumberSlider(type_="str") # type: ignore # noqa assert "Unknown type 'str'. Using 'float'" in caplog.text diff --git a/tests/data_service/test_data_service_cache.py b/tests/data_service/test_data_service_cache.py index d457b2d..5408cd6 100644 --- a/tests/data_service/test_data_service_cache.py +++ b/tests/data_service/test_data_service_cache.py @@ -1,8 +1,8 @@ import logging import pydase -from pydase.data_service.data_service_cache import DataServiceCache -from pydase.utils.serializer import get_nested_dict_by_path +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager logger = logging.getLogger() @@ -15,14 +15,23 @@ def test_nested_attributes_cache_callback() -> None: class_attr = SubClass() name = "World" - test_service = ServiceClass() - cache = DataServiceCache(test_service) + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - test_service.name = "Peepz" - assert get_nested_dict_by_path(cache.cache, "name")["value"] == "Peepz" + service_instance.name = "Peepz" + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("name")["value"] + == "Peepz" + ) - test_service.class_attr.name = "Ciao" - assert get_nested_dict_by_path(cache.cache, "class_attr.name")["value"] == "Ciao" + service_instance.class_attr.name = "Ciao" + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("class_attr.name")[ + "value" + ] + == "Ciao" + ) def test_task_status_update() -> None: @@ -32,11 +41,29 @@ def test_task_status_update() -> None: async def my_method(self) -> None: pass - test_service = ServiceClass() - cache = DataServiceCache(test_service) - assert get_nested_dict_by_path(cache.cache, "my_method")["type"] == "method" - assert get_nested_dict_by_path(cache.cache, "my_method")["value"] is None + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - test_service.start_my_method() # type: ignore - assert get_nested_dict_by_path(cache.cache, "my_method")["type"] == "method" - assert get_nested_dict_by_path(cache.cache, "my_method")["value"] == {} + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("my_method")["type"] + == "method" + ) + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("my_method")[ + "value" + ] + is None + ) + + service_instance.start_my_method() # type: ignore + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("my_method")["type"] + == "method" + ) + assert ( + state_manager._data_service_cache.get_value_dict_from_cache("my_method")[ + "value" + ] + == {} + ) diff --git a/tests/data_service/test_state_manager.py b/tests/data_service/test_state_manager.py index 45827f0..d33910f 100644 --- a/tests/data_service/test_state_manager.py +++ b/tests/data_service/test_state_manager.py @@ -2,8 +2,6 @@ import json from pathlib import Path from typing import Any -from pytest import LogCaptureFixture - import pydase import pydase.units as u from pydase.components.coloured_enum import ColouredEnum @@ -12,6 +10,7 @@ from pydase.data_service.state_manager import ( has_load_state_decorator, load_state, ) +from pytest import LogCaptureFixture class SubService(pydase.DataService): @@ -26,6 +25,7 @@ class State(ColouredEnum): class Service(pydase.DataService): def __init__(self, **kwargs: Any) -> None: + super().__init__(**kwargs) self.subservice = SubService() self.some_unit: u.Quantity = 1.2 * u.units.A self.some_float = 1.0 @@ -33,7 +33,6 @@ class Service(pydase.DataService): self._property_attr = 1337.0 self._name = "Service" self.state = State.RUNNING - super().__init__(**kwargs) @property def name(self) -> str: @@ -152,7 +151,7 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture): 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 "'some_unit' changed to '12.0 A!'" in caplog.text assert ( "Property 'name' has no '@load_state' decorator. " "Ignoring value from JSON file..." in caplog.text diff --git a/tests/data_service/test_task_manager.py b/tests/data_service/test_task_manager.py index 286fda9..ae2f9e5 100644 --- a/tests/data_service/test_task_manager.py +++ b/tests/data_service/test_task_manager.py @@ -1,8 +1,7 @@ import logging -from pytest import LogCaptureFixture - import pydase +from pytest import LogCaptureFixture logger = logging.getLogger() @@ -10,11 +9,11 @@ logger = logging.getLogger() def test_autostart_task_callback(caplog: LogCaptureFixture) -> None: class MyService(pydase.DataService): def __init__(self) -> None: + super().__init__() self._autostart_tasks = { # type: ignore "my_task": (), "my_other_task": (), } - super().__init__() async def my_task(self) -> None: logger.info("Triggered task.") @@ -25,8 +24,8 @@ def test_autostart_task_callback(caplog: LogCaptureFixture) -> None: service = MyService() service._task_manager.start_autostart_tasks() - assert "MyService.my_task changed to {}" in caplog.text - assert "MyService.my_other_task changed to {}" in caplog.text + assert "'my_task' changed to '{}'" in caplog.text + assert "'my_other_task' changed to '{}'" in caplog.text def test_DataService_subclass_autostart_task_callback( @@ -34,11 +33,11 @@ def test_DataService_subclass_autostart_task_callback( ) -> None: class MySubService(pydase.DataService): def __init__(self) -> None: + super().__init__() self._autostart_tasks = { # type: ignore "my_task": (), "my_other_task": (), } - super().__init__() async def my_task(self) -> None: logger.info("Triggered task.") @@ -52,8 +51,8 @@ def test_DataService_subclass_autostart_task_callback( service = MyService() service._task_manager.start_autostart_tasks() - assert "MyService.sub_service.my_task changed to {}" in caplog.text - assert "MyService.sub_service.my_other_task changed to {}" in caplog.text + assert "'sub_service.my_task' changed to '{}'" in caplog.text + assert "'sub_service.my_other_task' changed to '{}'" in caplog.text def test_DataServiceList_subclass_autostart_task_callback( @@ -61,11 +60,11 @@ def test_DataServiceList_subclass_autostart_task_callback( ) -> None: class MySubService(pydase.DataService): def __init__(self) -> None: + super().__init__() self._autostart_tasks = { # type: ignore "my_task": (), "my_other_task": (), } - super().__init__() async def my_task(self) -> None: logger.info("Triggered task.") @@ -79,7 +78,7 @@ def test_DataServiceList_subclass_autostart_task_callback( service = MyService() service._task_manager.start_autostart_tasks() - assert "MyService.sub_services_list[0].my_task changed to {}" in caplog.text - assert "MyService.sub_services_list[0].my_other_task changed to {}" in caplog.text - assert "MyService.sub_services_list[1].my_task changed to {}" in caplog.text - assert "MyService.sub_services_list[1].my_other_task changed to {}" in caplog.text + assert "'sub_services_list[0].my_task' changed to '{}'" in caplog.text + assert "'sub_services_list[0].my_other_task' changed to '{}'" in caplog.text + assert "'sub_services_list[1].my_task' changed to '{}'" in caplog.text + assert "'sub_services_list[1].my_other_task' changed to '{}'" in caplog.text diff --git a/tests/test_DataService_attribute_callbacks.py b/tests/test_DataService_attribute_callbacks.py index 52065cd..aa51306 100644 --- a/tests/test_DataService_attribute_callbacks.py +++ b/tests/test_DataService_attribute_callbacks.py @@ -1,6 +1,7 @@ -from pytest import LogCaptureFixture - from pydase import DataService +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager +from pytest import LogCaptureFixture def test_class_attributes(caplog: LogCaptureFixture) -> None: @@ -11,9 +12,11 @@ def test_class_attributes(caplog: LogCaptureFixture) -> None: attr_1 = SubClass() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) service_instance.attr_1.name = "Hi" - assert "ServiceClass.attr_1.name changed to Hi" in caplog.text + assert "'attr_1.name' changed to 'Hi'" in caplog.text def test_instance_attributes(caplog: LogCaptureFixture) -> None: @@ -22,13 +25,15 @@ def test_instance_attributes(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): def __init__(self) -> None: - self.attr_1 = SubClass() super().__init__() + self.attr_1 = SubClass() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) service_instance.attr_1.name = "Hi" - assert "ServiceClass.attr_1.name changed to Hi" in caplog.text + assert "'attr_1.name' changed to 'Hi'" in caplog.text def test_class_attribute(caplog: LogCaptureFixture) -> None: @@ -36,21 +41,25 @@ def test_class_attribute(caplog: LogCaptureFixture) -> None: attr = 0 service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) service_instance.attr = 1 - assert "ServiceClass.attr changed to 1" in caplog.text + assert "'attr' changed to '1'" in caplog.text def test_instance_attribute(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): def __init__(self) -> None: - self.attr = "Hello World" super().__init__() + self.attr = "Hello World" service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) service_instance.attr = "Hello" - assert "ServiceClass.attr changed to Hello" in caplog.text + assert "'attr' changed to 'Hello'" in caplog.text def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None: @@ -61,16 +70,19 @@ def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): def __init__(self) -> None: + super().__init__() self.attr_1 = subclass_instance self.attr_2 = subclass_instance - super().__init__() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + service_instance.attr_1.name = "Hi" assert service_instance.attr_1 == service_instance.attr_2 - assert "ServiceClass.attr_1.name changed to Hi" in caplog.text - assert "ServiceClass.attr_2.name changed to Hi" in caplog.text + assert "'attr_1.name' changed to 'Hi'" in caplog.text + assert "'attr_2.name' changed to 'Hi'" in caplog.text def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None: @@ -83,15 +95,18 @@ def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None: attr_1 = subclass_instance def __init__(self) -> None: - self.attr_2 = subclass_instance super().__init__() + self.attr_2 = subclass_instance service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + service_instance.attr_1.name = "Hi" assert service_instance.attr_1 == service_instance.attr_2 - assert "ServiceClass.attr_1.name changed to Hi" in caplog.text - assert "ServiceClass.attr_2.name changed to Hi" in caplog.text + assert "'attr_1.name' changed to 'Hi'" in caplog.text + assert "'attr_2.name' changed to 'Hi'" in caplog.text def test_nested_class_attributes(caplog: LogCaptureFixture) -> None: @@ -111,15 +126,18 @@ def test_nested_class_attributes(caplog: LogCaptureFixture) -> None: attr = SubClass() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.name = "Hou" service_instance.attr.name = "foo" service_instance.name = "bar" - assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text - assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text - assert "ServiceClass.attr.name changed to foo" in caplog.text - assert "ServiceClass.name changed to bar" in caplog.text + assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text + assert "'attr.attr.name' changed to 'Hou'" in caplog.text + assert "'attr.name' changed to 'foo'" in caplog.text + assert "'name' changed to 'bar'" in caplog.text def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None: @@ -128,32 +146,35 @@ def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None: class SubSubClass(DataService): def __init__(self) -> None: + super().__init__() self.attr = SubSubSubClass() self.name = "Hello" - super().__init__() class SubClass(DataService): def __init__(self) -> None: + super().__init__() self.attr = SubSubClass() self.name = "Hello" - super().__init__() class ServiceClass(DataService): def __init__(self) -> None: + super().__init__() self.attr = SubClass() self.name = "Hello" - super().__init__() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.name = "Hou" service_instance.attr.name = "foo" service_instance.name = "bar" - assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text - assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text - assert "ServiceClass.attr.name changed to foo" in caplog.text - assert "ServiceClass.name changed to bar" in caplog.text + assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text + assert "'attr.attr.name' changed to 'Hou'" in caplog.text + assert "'attr.name' changed to 'foo'" in caplog.text + assert "'name' changed to 'bar'" in caplog.text def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None: @@ -171,14 +192,19 @@ def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None: subattr = SubSubClass() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + service_instance.attr.attr.attr.name = "Hi" - assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text - assert "ServiceClass.subattr.attr.name changed to Hi" in caplog.text + assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text + assert "'subattr.attr.name' changed to 'Hi'" in caplog.text + caplog.clear() + service_instance.subattr.attr.name = "Ho" - assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text - assert "ServiceClass.subattr.attr.name changed to Ho" in caplog.text + assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text + assert "'subattr.attr.name' changed to 'Ho'" in caplog.text def test_advanced_nested_instance_attributes(caplog: LogCaptureFixture) -> None: @@ -187,32 +213,34 @@ def test_advanced_nested_instance_attributes(caplog: LogCaptureFixture) -> None: class SubSubClass(DataService): def __init__(self) -> None: - self.attr = SubSubSubClass() super().__init__() + self.attr = SubSubSubClass() subsubclass_instance = SubSubClass() class SubClass(DataService): def __init__(self) -> None: - self.attr = subsubclass_instance super().__init__() + self.attr = subsubclass_instance class ServiceClass(DataService): def __init__(self) -> None: + super().__init__() self.attr = SubClass() self.subattr = subsubclass_instance - super().__init__() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) service_instance.attr.attr.attr.name = "Hi" - assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text - assert "ServiceClass.subattr.attr.name changed to Hi" in caplog.text + assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text + assert "'subattr.attr.name' changed to 'Hi'" in caplog.text caplog.clear() service_instance.subattr.attr.name = "Ho" - assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text - assert "ServiceClass.subattr.attr.name changed to Ho" in caplog.text + assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text + assert "'subattr.attr.name' changed to 'Ho'" in caplog.text caplog.clear() @@ -224,17 +252,20 @@ def test_advanced_nested_attributes_mixed(caplog: LogCaptureFixture) -> None: class_attr = SubSubClass() def __init__(self) -> None: - self.attr_1 = SubSubClass() super().__init__() + self.attr_1 = SubSubClass() class ServiceClass(DataService): class_attr = SubClass() def __init__(self) -> None: - self.attr = SubClass() super().__init__() + self.attr = SubClass() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) + # Subclass.attr is the same for all instances assert service_instance.attr.class_attr == service_instance.class_attr.class_attr @@ -245,23 +276,23 @@ def test_advanced_nested_attributes_mixed(caplog: LogCaptureFixture) -> None: assert service_instance.attr.attr_1 != service_instance.class_attr.class_attr service_instance.class_attr.class_attr.name = "Ho" - assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text - assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text + assert "'class_attr.class_attr.name' changed to 'Ho'" in caplog.text + assert "'attr.class_attr.name' changed to 'Ho'" in caplog.text caplog.clear() service_instance.class_attr.attr_1.name = "Ho" - assert "ServiceClass.class_attr.attr_1.name changed to Ho" in caplog.text - assert "ServiceClass.attr.attr_1.name changed to Ho" not in caplog.text + assert "'class_attr.attr_1.name' changed to 'Ho'" in caplog.text + assert "'attr.attr_1.name' changed to 'Ho'" not in caplog.text caplog.clear() - service_instance.attr.class_attr.name = "Ho" - assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text - assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text + service_instance.attr.class_attr.name = "Hello" + assert "'class_attr.class_attr.name' changed to 'Hello'" in caplog.text + assert "'attr.class_attr.name' changed to 'Hello'" in caplog.text caplog.clear() service_instance.attr.attr_1.name = "Ho" - assert "ServiceClass.attr.attr_1.name changed to Ho" in caplog.text - assert "ServiceClass.class_attr.attr_1.name changed to Ho" not in caplog.text + assert "'attr.attr_1.name' changed to 'Ho'" in caplog.text + assert "'class_attr.attr_1.name' changed to 'Ho'" not in caplog.text caplog.clear() @@ -277,32 +308,34 @@ def test_class_list_attributes(caplog: LogCaptureFixture) -> None: attr = subclass_instance service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) assert service_instance.attr_list[0] != service_instance.attr_list[1] service_instance.attr_list[0].name = "Ho" - assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list[1].name changed to Ho" not in caplog.text + assert "'attr_list[0].name' changed to 'Ho'" in caplog.text + assert "'attr_list[1].name' changed to 'Ho'" not in caplog.text caplog.clear() service_instance.attr_list[1].name = "Ho" - assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text - assert "ServiceClass.attr_list[1].name changed to Ho" in caplog.text + assert "'attr_list[0].name' changed to 'Ho'" not in caplog.text + assert "'attr_list[1].name' changed to 'Ho'" in caplog.text caplog.clear() assert service_instance.attr_list_2[0] == service_instance.attr assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1] - service_instance.attr_list_2[0].name = "Ho" - assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text - assert "ServiceClass.attr.name changed to Ho" in caplog.text + service_instance.attr_list_2[0].name = "Ciao" + assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text + assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text + assert "'attr.name' changed to 'Ciao'" in caplog.text caplog.clear() - service_instance.attr_list_2[1].name = "Ho" - assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text - assert "ServiceClass.attr.name changed to Ho" in caplog.text + service_instance.attr_list_2[1].name = "Bye" + assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text + assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text + assert "'attr.name' changed to 'Bye'" in caplog.text caplog.clear() @@ -320,17 +353,19 @@ def test_nested_class_list_attributes(caplog: LogCaptureFixture) -> None: subattr = subsubclass_instance service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) assert service_instance.attr[0].attr_list[0] == service_instance.subattr service_instance.attr[0].attr_list[0].name = "Ho" - assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.subattr.name changed to Ho" in caplog.text + assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text + assert "'subattr.name' changed to 'Ho'" in caplog.text caplog.clear() - service_instance.subattr.name = "Ho" - assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.subattr.name changed to Ho" in caplog.text + service_instance.subattr.name = "Hi" + assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text + assert "'subattr.name' changed to 'Hi'" in caplog.text caplog.clear() @@ -342,44 +377,46 @@ def test_instance_list_attributes(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): def __init__(self) -> None: + super().__init__() self.attr_list = [SubClass() for _ in range(2)] self.attr_list_2 = [subclass_instance, subclass_instance] self.attr = subclass_instance - super().__init__() service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) assert service_instance.attr_list[0] != service_instance.attr_list[1] service_instance.attr_list[0].name = "Ho" - assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list[1].name changed to Ho" not in caplog.text + assert "'attr_list[0].name' changed to 'Ho'" in caplog.text + assert "'attr_list[1].name' changed to 'Ho'" not in caplog.text caplog.clear() - service_instance.attr_list[1].name = "Ho" - assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text - assert "ServiceClass.attr_list[1].name changed to Ho" in caplog.text + service_instance.attr_list[1].name = "Hi" + assert "'attr_list[0].name' changed to 'Hi'" not in caplog.text + assert "'attr_list[1].name' changed to 'Hi'" in caplog.text caplog.clear() assert service_instance.attr_list_2[0] == service_instance.attr assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1] - service_instance.attr_list_2[0].name = "Ho" - assert "ServiceClass.attr.name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text + service_instance.attr_list_2[0].name = "Ciao" + assert "'attr.name' changed to 'Ciao'" in caplog.text + assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text + assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text caplog.clear() - service_instance.attr_list_2[1].name = "Ho" - assert "ServiceClass.attr.name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text + service_instance.attr_list_2[1].name = "Bye" + assert "'attr.name' changed to 'Bye'" in caplog.text + assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text + assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text caplog.clear() service_instance.attr.name = "Ho" - assert "ServiceClass.attr.name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text - assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text + assert "'attr.name' changed to 'Ho'" in caplog.text + assert "'attr_list_2[0].name' changed to 'Ho'" in caplog.text + assert "'attr_list_2[1].name' changed to 'Ho'" in caplog.text caplog.clear() @@ -391,26 +428,28 @@ def test_nested_instance_list_attributes(caplog: LogCaptureFixture) -> None: class SubClass(DataService): def __init__(self) -> None: - self.attr_list = [subsubclass_instance] super().__init__() + self.attr_list = [subsubclass_instance] class ServiceClass(DataService): class_attr = subsubclass_instance def __init__(self) -> None: - self.attr = [SubClass()] super().__init__() + self.attr = [SubClass()] service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) assert service_instance.attr[0].attr_list[0] == service_instance.class_attr service_instance.attr[0].attr_list[0].name = "Ho" - assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.class_attr.name changed to Ho" in caplog.text + assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text + assert "'class_attr.name' changed to 'Ho'" in caplog.text caplog.clear() - service_instance.class_attr.name = "Ho" - assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text - assert "ServiceClass.class_attr.name changed to Ho" in caplog.text + service_instance.class_attr.name = "Hi" + assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text + assert "'class_attr.name' changed to 'Hi'" in caplog.text caplog.clear() diff --git a/tests/test_properties.py b/tests/test_properties.py index d4b67d3..1de00e3 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -1,6 +1,7 @@ -from pytest import LogCaptureFixture - from pydase import DataService +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager +from pytest import LogCaptureFixture def test_properties(caplog: LogCaptureFixture) -> None: @@ -28,281 +29,303 @@ def test_properties(caplog: LogCaptureFixture) -> None: def current(self, value: float) -> None: self._current = value - test_service = ServiceClass() - test_service.voltage = 1 + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - assert "ServiceClass.power changed to 1.0" in caplog.text - assert "ServiceClass.voltage changed to 1.0" in caplog.text + service_instance.voltage = 1 + + assert "'power' changed to '1.0'" in caplog.text + assert "'voltage' changed to '1.0'" in caplog.text caplog.clear() - test_service.current = 12.0 - - assert "ServiceClass.power changed to 12.0" in caplog.text - assert "ServiceClass.current changed to 12.0" in caplog.text - - -def test_nested_properties(caplog: LogCaptureFixture) -> None: - class SubSubClass(DataService): - name = "Hello" - - class SubClass(DataService): - name = "Hello" - class_attr = SubSubClass() - - class ServiceClass(DataService): - class_attr = SubClass() - name = "World" - - @property - def subsub_name(self) -> str: - return f"{self.class_attr.class_attr.name} {self.name}" - - @property - def sub_name(self) -> str: - return f"{self.class_attr.name} {self.name}" - - test_service = ServiceClass() - test_service.name = "Peepz" - - assert "ServiceClass.name changed to Peepz" in caplog.text - assert "ServiceClass.sub_name changed to Hello Peepz" in caplog.text - assert "ServiceClass.subsub_name changed to Hello Peepz" in caplog.text - caplog.clear() - - test_service.class_attr.name = "Hi" - - assert "ServiceClass.sub_name changed to Hi Peepz" in caplog.text - assert ( - "ServiceClass.subsub_name changed to Hello Peepz" in caplog.text - ) # registers subclass changes - assert "ServiceClass.class_attr.name changed to Hi" in caplog.text - caplog.clear() - - test_service.class_attr.class_attr.name = "Ciao" - - assert ( - "ServiceClass.sub_name changed to Hi Peepz" in caplog.text - ) # registers subclass changes - assert "ServiceClass.subsub_name changed to Ciao Peepz" in caplog.text - assert "ServiceClass.class_attr.class_attr.name changed to Ciao" in caplog.text - caplog.clear() - - -def test_simple_list_properties(caplog: LogCaptureFixture) -> None: - class ServiceClass(DataService): - list = ["Hello", "Ciao"] - name = "World" - - @property - def total_name(self) -> str: - return f"{self.list[0]} {self.name}" - - test_service = ServiceClass() - test_service.name = "Peepz" - - assert "ServiceClass.name changed to Peepz" in caplog.text - assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text - caplog.clear() - - test_service.list[0] = "Hi" - - assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text - assert "ServiceClass.list[0] changed to Hi" in caplog.text - - -def test_class_list_properties(caplog: LogCaptureFixture) -> None: - class SubClass(DataService): - name = "Hello" - - class ServiceClass(DataService): - list = [SubClass()] - name = "World" - - @property - def total_name(self) -> str: - return f"{self.list[0].name} {self.name}" - - test_service = ServiceClass() - test_service.name = "Peepz" - - assert "ServiceClass.name changed to Peepz" in caplog.text - assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text - caplog.clear() - - test_service.list[0].name = "Hi" - - assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text - assert "ServiceClass.list[0].name changed to Hi" in caplog.text - - -def test_subclass_properties(caplog: LogCaptureFixture) -> None: - class SubClass(DataService): - name = "Hello" - _voltage = 10.0 - _current = 1.0 - - @property - def power(self) -> float: - return self._voltage * self.current - - @property - def voltage(self) -> float: - return self._voltage - - @voltage.setter - def voltage(self, value: float) -> None: - self._voltage = value - - @property - def current(self) -> float: - return self._current - - @current.setter - def current(self, value: float) -> None: - self._current = value - - class ServiceClass(DataService): - class_attr = SubClass() - - @property - def voltage(self) -> float: - return self.class_attr.voltage - - test_service = ServiceClass() - test_service.class_attr.voltage = 10.0 - - # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for - # changing voltage, and once for changing power. - assert "ServiceClass.class_attr.voltage changed to 10.0" in caplog.text - assert "ServiceClass.class_attr.power changed to 10.0" in caplog.text - assert "ServiceClass.voltage changed to 10.0" in caplog.text - caplog.clear() - - -def test_subclass_properties_2(caplog: LogCaptureFixture) -> None: - class SubClass(DataService): - name = "Hello" - _voltage = 10.0 - _current = 1.0 - - @property - def power(self) -> float: - return self._voltage * self.current - - @property - def voltage(self) -> float: - return self._voltage - - @voltage.setter - def voltage(self, value: float) -> None: - self._voltage = value - - @property - def current(self) -> float: - return self._current - - @current.setter - def current(self, value: float) -> None: - self._current = value - - class ServiceClass(DataService): - class_attr = [SubClass() for i in range(2)] - - @property - def voltage(self) -> float: - return self.class_attr[0].voltage - - test_service = ServiceClass() - test_service.class_attr[1].current = 10.0 - - # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for - # changing current, and once for changing power. Note that the voltage property is - # only dependent on class_attr[0] but still emits an update notification. This is - # because every time any item in the list `test_service.class_attr` is changed, - # a notification will be emitted. - assert "ServiceClass.class_attr[1].current changed to 10.0" in caplog.text - assert "ServiceClass.class_attr[1].power changed to 100.0" in caplog.text - assert "ServiceClass.voltage changed to 10.0" in caplog.text - - -def test_subsubclass_properties(caplog: LogCaptureFixture) -> None: - class SubSubClass(DataService): - _voltage = 10.0 - - @property - def voltage(self) -> float: - return self._voltage - - @voltage.setter - def voltage(self, value: float) -> None: - self._voltage = value - - class SubClass(DataService): - class_attr = SubSubClass() - current = 0.5 - - @property - def power(self) -> float: - return self.class_attr.voltage * self.current - - class ServiceClass(DataService): - class_attr = [SubClass() for i in range(2)] - - @property - def power(self) -> float: - return self.class_attr[0].power - - test_service = ServiceClass() - - test_service.class_attr[1].class_attr.voltage = 100.0 - assert ( - "ServiceClass.class_attr[0].class_attr.voltage changed to 100.0" in caplog.text - ) - assert ( - "ServiceClass.class_attr[1].class_attr.voltage changed to 100.0" in caplog.text - ) - assert "ServiceClass.class_attr[0].power changed to 50.0" in caplog.text - assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text - assert "ServiceClass.power changed to 50.0" in caplog.text - - -def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None: - class SubSubClass(DataService): - def __init__(self) -> None: - self._voltage = 10.0 - super().__init__() - - @property - def voltage(self) -> float: - return self._voltage - - @voltage.setter - def voltage(self, value: float) -> None: - self._voltage = value - - class SubClass(DataService): - def __init__(self) -> None: - self.attr = [SubSubClass()] - self.current = 0.5 - super().__init__() - - @property - def power(self) -> float: - return self.attr[0].voltage * self.current - - class ServiceClass(DataService): - class_attr = [SubClass() for i in range(2)] - - @property - def power(self) -> float: - return self.class_attr[0].power - - test_service = ServiceClass() - - test_service.class_attr[1].attr[0].voltage = 100.0 - # again, changing an item in a list will trigger the callbacks. This is why a - # notification for `ServiceClass.power` is emitted although it did not change its - # value - assert "ServiceClass.class_attr[1].attr[0].voltage changed to 100.0" in caplog.text - assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text - assert "ServiceClass.power changed to 5.0" in caplog.text + service_instance.current = 12.0 + + assert "'power' changed to '12.0'" in caplog.text + assert "'current' changed to '12.0'" in caplog.text + + +# def test_nested_properties(caplog: LogCaptureFixture) -> None: +# class SubSubClass(DataService): +# name = "Hello" +# +# class SubClass(DataService): +# name = "Hello" +# class_attr = SubSubClass() +# +# class ServiceClass(DataService): +# class_attr = SubClass() +# name = "World" +# +# @property +# def subsub_name(self) -> str: +# return f"{self.class_attr.class_attr.name} {self.name}" +# +# @property +# def sub_name(self) -> str: +# return f"{self.class_attr.name} {self.name}" +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.name = "Peepz" +# +# assert "'name' changed to 'Peepz'" in caplog.text +# assert "'sub_name' changed to 'Hello Peepz'" in caplog.text +# assert "'subsub_name' changed to 'Hello Peepz'" in caplog.text +# caplog.clear() +# +# service_instance.class_attr.name = "Hi" +# +# assert "'sub_name' changed to 'Hi Peepz'" in caplog.text +# assert ( +# "'subsub_name' changed to 'Hello Peepz'" in caplog.text +# ) # registers subclass changes +# assert "'class_attr.name' changed to 'Hi'" in caplog.text +# caplog.clear() +# +# service_instance.class_attr.class_attr.name = "Ciao" +# +# assert ( +# "'sub_name' changed to 'Hi Peepz'" in caplog.text +# ) # registers subclass changes +# assert "'subsub_name' changed to 'Ciao Peepz'" in caplog.text +# assert "'class_attr.class_attr.name' changed to 'Ciao'" in caplog.text +# caplog.clear() +# +# +# def test_simple_list_properties(caplog: LogCaptureFixture) -> None: +# class ServiceClass(DataService): +# list = ["Hello", "Ciao"] +# name = "World" +# +# @property +# def total_name(self) -> str: +# return f"{self.list[0]} {self.name}" +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.name = "Peepz" +# +# assert "'name' changed to 'Peepz'" in caplog.text +# assert "'total_name' changed to 'Hello Peepz'" in caplog.text +# caplog.clear() +# +# service_instance.list[0] = "Hi" +# +# assert "'total_name' changed to 'Hi Peepz'" in caplog.text +# assert "'list[0]' changed to 'Hi'" in caplog.text +# +# +# def test_class_list_properties(caplog: LogCaptureFixture) -> None: +# class SubClass(DataService): +# name = "Hello" +# +# class ServiceClass(DataService): +# list = [SubClass()] +# name = "World" +# +# @property +# def total_name(self) -> str: +# return f"{self.list[0].name} {self.name}" +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.name = "Peepz" +# +# assert "'name' changed to 'Peepz'" in caplog.text +# assert "'total_name' changed to 'Hello Peepz'" in caplog.text +# caplog.clear() +# +# service_instance.list[0].name = "Hi" +# +# assert "'total_name' changed to 'Hi Peepz'" in caplog.text +# assert "'list[0].name' changed to 'Hi'" in caplog.text +# +# +# def test_subclass_properties(caplog: LogCaptureFixture) -> None: +# class SubClass(DataService): +# name = "Hello" +# _voltage = 10.0 +# _current = 1.0 +# +# @property +# def power(self) -> float: +# return self._voltage * self.current +# +# @property +# def voltage(self) -> float: +# return self._voltage +# +# @voltage.setter +# def voltage(self, value: float) -> None: +# self._voltage = value +# +# @property +# def current(self) -> float: +# return self._current +# +# @current.setter +# def current(self, value: float) -> None: +# self._current = value +# +# class ServiceClass(DataService): +# class_attr = SubClass() +# +# @property +# def voltage(self) -> float: +# return self.class_attr.voltage +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.class_attr.voltage = 10.0 +# +# # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for +# # changing voltage, and once for changing power. +# assert "'class_attr.voltage' changed to '10.0'" in caplog.text +# assert "'class_attr.power' changed to '10.0'" in caplog.text +# assert "'voltage' changed to '10.0'" in caplog.text +# caplog.clear() +# +# +# def test_subclass_properties_2(caplog: LogCaptureFixture) -> None: +# class SubClass(DataService): +# name = "Hello" +# _voltage = 10.0 +# _current = 1.0 +# +# @property +# def power(self) -> float: +# return self._voltage * self.current +# +# @property +# def voltage(self) -> float: +# return self._voltage +# +# @voltage.setter +# def voltage(self, value: float) -> None: +# self._voltage = value +# +# @property +# def current(self) -> float: +# return self._current +# +# @current.setter +# def current(self, value: float) -> None: +# self._current = value +# +# class ServiceClass(DataService): +# class_attr = [SubClass() for i in range(2)] +# +# @property +# def voltage(self) -> float: +# return self.class_attr[0].voltage +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.class_attr[1].current = 10.0 +# +# # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for +# # changing current, and once for changing power. Note that the voltage property is +# # only dependent on class_attr[0] but still emits an update notification. This is +# # because every time any item in the list `service_instance.class_attr` is changed, +# # a notification will be emitted. +# assert "'class_attr[1].current' changed to '10.0'" in caplog.text +# assert "'class_attr[1].power' changed to '100.0'" in caplog.text +# assert "'voltage' changed to '10.0'" in caplog.text +# +# +# def test_subsubclass_properties(caplog: LogCaptureFixture) -> None: +# class SubSubClass(DataService): +# _voltage = 10.0 +# +# @property +# def voltage(self) -> float: +# return self._voltage +# +# @voltage.setter +# def voltage(self, value: float) -> None: +# self._voltage = value +# +# class SubClass(DataService): +# class_attr = SubSubClass() +# current = 0.5 +# +# @property +# def power(self) -> float: +# return self.class_attr.voltage * self.current +# +# class ServiceClass(DataService): +# class_attr = [SubClass() for i in range(2)] +# +# @property +# def power(self) -> float: +# return self.class_attr[0].power +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.class_attr[1].class_attr.voltage = 100.0 +# assert ( +# "'class_attr[0].class_attr.voltage' changed to '100.0'" in caplog.text +# ) +# assert ( +# "'class_attr[1].class_attr.voltage' changed to '100.0'" in caplog.text +# ) +# assert "'class_attr[0].power' changed to '50.0'" in caplog.text +# assert "'class_attr[1].power' changed to '50.0'" in caplog.text +# assert "'power' changed to '50.0'" in caplog.text +# +# +# def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None: +# class SubSubClass(DataService): +# def __init__(self) -> None: +# super().__init__() +# self._voltage = 10.0 +# +# @property +# def voltage(self) -> float: +# return self._voltage +# +# @voltage.setter +# def voltage(self, value: float) -> None: +# self._voltage = value +# +# class SubClass(DataService): +# def __init__(self) -> None: +# super().__init__() +# self.attr = [SubSubClass()] +# self.current = 0.5 +# +# @property +# def power(self) -> float: +# return self.attr[0].voltage * self.current +# +# class ServiceClass(DataService): +# class_attr = [SubClass() for i in range(2)] +# +# @property +# def power(self) -> float: +# return self.class_attr[0].power +# +# service_instance = ServiceClass() +# state_manager = StateManager(service_instance) +# DataServiceObserver(state_manager) +# +# service_instance.class_attr[1].attr[0].voltage = 100.0 +# # again, changing an item in a list will trigger the callbacks. This is why a +# # notification for `ServiceClass.power` is emitted although it did not change its +# # value +# assert "'class_attr[1].attr[0].voltage' changed to '100.0'" in caplog.text +# assert "'class_attr[1].power' changed to '50.0'" in caplog.text +# assert "'power' changed to '5.0'" in caplog.text diff --git a/tests/test_units.py b/tests/test_units.py index e1dd2e5..2a268ff 100644 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -1,9 +1,10 @@ from typing import Any -from pytest import LogCaptureFixture - import pydase.units as u from pydase.data_service.data_service import DataService +from pydase.data_service.data_service_observer import DataServiceObserver +from pydase.data_service.state_manager import StateManager +from pytest import LogCaptureFixture def test_DataService_setattr(caplog: LogCaptureFixture) -> None: @@ -19,26 +20,28 @@ def test_DataService_setattr(caplog: LogCaptureFixture) -> None: def current(self, value: Any) -> None: self._current = value - service = ServiceClass() + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) # You can just set floats to the Quantity objects. The DataService __setattr__ will # automatically convert this - service.voltage = 10.0 # type: ignore - service.current = 1.5 + service_instance.voltage = 10.0 # type: ignore + service_instance.current = 1.5 - assert service.voltage == 10.0 * u.units.V # type: ignore - assert service.current == 1.5 * u.units.mA + assert service_instance.voltage == 10.0 * u.units.V # type: ignore + assert service_instance.current == 1.5 * u.units.mA - assert "ServiceClass.voltage changed to 10.0 V" in caplog.text - assert "ServiceClass.current changed to 1.5 mA" in caplog.text + assert "'voltage' changed to '10.0 V'" in caplog.text + assert "'current' changed to '1.5 mA'" in caplog.text - service.voltage = 12.0 * u.units.V # type: ignore - service.current = 1.51 * u.units.A - assert service.voltage == 12.0 * u.units.V # type: ignore - assert service.current == 1.51 * u.units.A + service_instance.voltage = 12.0 * u.units.V # type: ignore + service_instance.current = 1.51 * u.units.A + assert service_instance.voltage == 12.0 * u.units.V # type: ignore + assert service_instance.current == 1.51 * u.units.A - assert "ServiceClass.voltage changed to 12.0 V" in caplog.text - assert "ServiceClass.current changed to 1.51 A" in caplog.text + assert "'voltage' changed to '12.0 V'" in caplog.text + assert "'current' changed to '1.51 A'" in caplog.text def test_convert_to_quantity() -> None: @@ -61,23 +64,25 @@ def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None: def current(self, value: Any) -> None: self._current = value - service = ServiceClass() + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - service.update_DataService_attribute( + service_instance.update_DataService_attribute( path_list=[], attr_name="voltage", value=1.0 * u.units.mV ) - assert "ServiceClass.voltage changed to 1.0 mV" in caplog.text + assert "'voltage' changed to '1.0 mV'" in caplog.text - service.update_DataService_attribute(path_list=[], attr_name="voltage", value=2) + service_instance.update_DataService_attribute(path_list=[], attr_name="voltage", value=2) - assert "ServiceClass.voltage changed to 2.0 mV" in caplog.text + assert "'voltage' changed to '2.0 mV'" in caplog.text - service.update_DataService_attribute( + service_instance.update_DataService_attribute( path_list=[], attr_name="voltage", value={"magnitude": 123, "unit": "kV"} ) - assert "ServiceClass.voltage changed to 123.0 kV" in caplog.text + assert "'voltage' changed to '123.0 kV'" in caplog.text def test_autoconvert_offset_to_baseunit() -> None: @@ -104,9 +109,9 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None: } class ServiceClass(DataService): - def __init__(self): - self._unit: u.Quantity = 1 * u.units.A + def __init__(self) -> None: super().__init__() + self._unit: u.Quantity = 1 * u.units.A @property def some_unit(self) -> u.Quantity: @@ -117,8 +122,10 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None: assert isinstance(value, u.Quantity) self._unit = value - service = ServiceClass() + service_instance = ServiceClass() + state_manager = StateManager(service_instance) + DataServiceObserver(state_manager) - service.load_DataService_from_JSON(JSON_DICT) + service_instance.load_DataService_from_JSON(JSON_DICT) - assert "ServiceClass.some_unit changed to 10.0 A" in caplog.text + assert "'some_unit' changed to '10.0 A'" in caplog.text diff --git a/tests/utils/test_serializer.py b/tests/utils/test_serializer.py index c1c8839..736aa66 100644 --- a/tests/utils/test_serializer.py +++ b/tests/utils/test_serializer.py @@ -1,10 +1,9 @@ import asyncio from enum import Enum -import pytest - import pydase import pydase.units as u +import pytest from pydase.components.coloured_enum import ColouredEnum from pydase.utils.serializer import ( SerializationPathError, @@ -43,13 +42,13 @@ def test_enum_serialize() -> None: class EnumAttribute(pydase.DataService): def __init__(self) -> None: - self.some_enum = EnumClass.FOO super().__init__() + self.some_enum = EnumClass.FOO class EnumPropertyWithoutSetter(pydase.DataService): def __init__(self) -> None: - self._some_enum = EnumClass.FOO super().__init__() + self._some_enum = EnumClass.FOO @property def some_enum(self) -> EnumClass: @@ -57,8 +56,8 @@ def test_enum_serialize() -> None: class EnumPropertyWithSetter(pydase.DataService): def __init__(self) -> None: - self._some_enum = EnumClass.FOO super().__init__() + self._some_enum = EnumClass.FOO @property def some_enum(self) -> EnumClass: diff --git a/tests/utils/test_warnings.py b/tests/utils/test_warnings.py index a054b04..b251749 100644 --- a/tests/utils/test_warnings.py +++ b/tests/utils/test_warnings.py @@ -1,28 +1,27 @@ +from pydase import DataService from pytest import LogCaptureFixture -from pydase import DataService - -def test_setattr_warnings(caplog: LogCaptureFixture) -> None: # noqa +def test_setattr_warnings(caplog: LogCaptureFixture) -> None: # def test_setattr_warnings(capsys: CaptureFixture) -> None: class SubClass: name = "Hello" class ServiceClass(DataService): def __init__(self) -> None: - self.attr_1 = SubClass() super().__init__() + self.attr_1 = SubClass() ServiceClass() assert "Warning: Class 'SubClass' does not inherit from DataService." in caplog.text -def test_private_attribute_warning(caplog: LogCaptureFixture) -> None: # noqa +def test_private_attribute_warning(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService): def __init__(self) -> None: - self.__something = "" super().__init__() + self.__something = "" ServiceClass() @@ -32,14 +31,14 @@ def test_private_attribute_warning(caplog: LogCaptureFixture) -> None: # noqa ) -def test_protected_attribute_warning(caplog: LogCaptureFixture) -> None: # noqa +def test_protected_attribute_warning(caplog: LogCaptureFixture) -> None: class SubClass: name = "Hello" class ServiceClass(DataService): def __init__(self) -> None: - self._subclass = SubClass super().__init__() + self._subclass = SubClass ServiceClass()