updates tests

This commit is contained in:
Mose Müller 2023-11-30 17:17:41 +01:00
parent cc105106ee
commit c891642bda
10 changed files with 563 additions and 465 deletions

View File

@ -1,7 +1,8 @@
from pytest import LogCaptureFixture
from pydase.components.coloured_enum import ColouredEnum from pydase.components.coloured_enum import ColouredEnum
from pydase.data_service.data_service import DataService 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: def test_ColouredEnum(caplog: LogCaptureFixture) -> None:
@ -21,14 +22,16 @@ def test_ColouredEnum(caplog: LogCaptureFixture) -> None:
# do something ... # do something ...
self._status = value 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): class MyStatus(ColouredEnum):
RUNNING = "#00FF00" RUNNING = "#00FF00"
FAILING = "#FF0000" FAILING = "#FF0000"

View File

@ -1,7 +1,8 @@
from pytest import LogCaptureFixture
from pydase.components.number_slider import NumberSlider from pydase.components.number_slider import NumberSlider
from pydase.data_service.data_service import DataService 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: def test_NumberSlider(caplog: LogCaptureFixture) -> None:
@ -9,35 +10,37 @@ def test_NumberSlider(caplog: LogCaptureFixture) -> None:
number_slider = NumberSlider(1, 0, 10, 1) number_slider = NumberSlider(1, 0, 10, 1)
int_number_slider = NumberSlider(1, 0, 10, 1, "int") 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 service_instance.number_slider.value == 1
assert isinstance(service.number_slider.value, float) assert isinstance(service_instance.number_slider.value, float)
assert service.number_slider.min == 0 assert service_instance.number_slider.min == 0
assert isinstance(service.number_slider.min, float) assert isinstance(service_instance.number_slider.min, float)
assert service.number_slider.max == 10 assert service_instance.number_slider.max == 10
assert isinstance(service.number_slider.max, float) assert isinstance(service_instance.number_slider.max, float)
assert service.number_slider.step_size == 1 assert service_instance.number_slider.step_size == 1
assert isinstance(service.number_slider.step_size, float) assert isinstance(service_instance.number_slider.step_size, float)
assert service.int_number_slider.value == 1 assert service_instance.int_number_slider.value == 1
assert isinstance(service.int_number_slider.value, int) assert isinstance(service_instance.int_number_slider.value, int)
assert service.int_number_slider.step_size == 1 assert service_instance.int_number_slider.step_size == 1
assert isinstance(service.int_number_slider.step_size, int) assert isinstance(service_instance.int_number_slider.step_size, int)
service.number_slider.value = 10.0 service_instance.number_slider.value = 10.0
service.int_number_slider.value = 10.1 service_instance.int_number_slider.value = 10.1
assert "ServiceClass.number_slider.value changed to 10.0" in caplog.text assert "'number_slider.value' changed to '10.0'" in caplog.text
assert "ServiceClass.int_number_slider.value changed to 10" in caplog.text assert "'int_number_slider.value' changed to '10'" in caplog.text
caplog.clear() 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 number_slider = NumberSlider(type_="str") # type: ignore # noqa
assert "Unknown type 'str'. Using 'float'" in caplog.text assert "Unknown type 'str'. Using 'float'" in caplog.text

View File

@ -1,8 +1,8 @@
import logging import logging
import pydase import pydase
from pydase.data_service.data_service_cache import DataServiceCache from pydase.data_service.data_service_observer import DataServiceObserver
from pydase.utils.serializer import get_nested_dict_by_path from pydase.data_service.state_manager import StateManager
logger = logging.getLogger() logger = logging.getLogger()
@ -15,14 +15,23 @@ def test_nested_attributes_cache_callback() -> None:
class_attr = SubClass() class_attr = SubClass()
name = "World" name = "World"
test_service = ServiceClass() service_instance = ServiceClass()
cache = DataServiceCache(test_service) state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
test_service.name = "Peepz" service_instance.name = "Peepz"
assert get_nested_dict_by_path(cache.cache, "name")["value"] == "Peepz" assert (
state_manager._data_service_cache.get_value_dict_from_cache("name")["value"]
== "Peepz"
)
test_service.class_attr.name = "Ciao" service_instance.class_attr.name = "Ciao"
assert get_nested_dict_by_path(cache.cache, "class_attr.name")["value"] == "Ciao" assert (
state_manager._data_service_cache.get_value_dict_from_cache("class_attr.name")[
"value"
]
== "Ciao"
)
def test_task_status_update() -> None: def test_task_status_update() -> None:
@ -32,11 +41,29 @@ def test_task_status_update() -> None:
async def my_method(self) -> None: async def my_method(self) -> None:
pass pass
test_service = ServiceClass() service_instance = ServiceClass()
cache = DataServiceCache(test_service) state_manager = StateManager(service_instance)
assert get_nested_dict_by_path(cache.cache, "my_method")["type"] == "method" DataServiceObserver(state_manager)
assert get_nested_dict_by_path(cache.cache, "my_method")["value"] is None
test_service.start_my_method() # type: ignore assert (
assert get_nested_dict_by_path(cache.cache, "my_method")["type"] == "method" state_manager._data_service_cache.get_value_dict_from_cache("my_method")["type"]
assert get_nested_dict_by_path(cache.cache, "my_method")["value"] == {} == "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"
]
== {}
)

View File

@ -2,8 +2,6 @@ import json
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
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.components.coloured_enum import ColouredEnum
@ -12,6 +10,7 @@ from pydase.data_service.state_manager import (
has_load_state_decorator, has_load_state_decorator,
load_state, load_state,
) )
from pytest import LogCaptureFixture
class SubService(pydase.DataService): class SubService(pydase.DataService):
@ -26,6 +25,7 @@ class State(ColouredEnum):
class Service(pydase.DataService): class Service(pydase.DataService):
def __init__(self, **kwargs: Any) -> None: def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
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
@ -33,7 +33,6 @@ class Service(pydase.DataService):
self._property_attr = 1337.0 self._property_attr = 1337.0
self._name = "Service" self._name = "Service"
self.state = State.RUNNING self.state = State.RUNNING
super().__init__(**kwargs)
@property @property
def name(self) -> str: 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.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 "'some_unit' changed to '12.0 A!'" in caplog.text
assert ( assert (
"Property 'name' has no '@load_state' decorator. " "Property 'name' has no '@load_state' decorator. "
"Ignoring value from JSON file..." in caplog.text "Ignoring value from JSON file..." in caplog.text

View File

@ -1,8 +1,7 @@
import logging import logging
from pytest import LogCaptureFixture
import pydase import pydase
from pytest import LogCaptureFixture
logger = logging.getLogger() logger = logging.getLogger()
@ -10,11 +9,11 @@ logger = logging.getLogger()
def test_autostart_task_callback(caplog: LogCaptureFixture) -> None: def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
class MyService(pydase.DataService): class MyService(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self._autostart_tasks = { # type: ignore self._autostart_tasks = { # type: ignore
"my_task": (), "my_task": (),
"my_other_task": (), "my_other_task": (),
} }
super().__init__()
async def my_task(self) -> None: async def my_task(self) -> None:
logger.info("Triggered task.") logger.info("Triggered task.")
@ -25,8 +24,8 @@ def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
service = MyService() service = MyService()
service._task_manager.start_autostart_tasks() service._task_manager.start_autostart_tasks()
assert "MyService.my_task changed to {}" in caplog.text assert "'my_task' changed to '{}'" in caplog.text
assert "MyService.my_other_task changed to {}" in caplog.text assert "'my_other_task' changed to '{}'" in caplog.text
def test_DataService_subclass_autostart_task_callback( def test_DataService_subclass_autostart_task_callback(
@ -34,11 +33,11 @@ def test_DataService_subclass_autostart_task_callback(
) -> None: ) -> None:
class MySubService(pydase.DataService): class MySubService(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self._autostart_tasks = { # type: ignore self._autostart_tasks = { # type: ignore
"my_task": (), "my_task": (),
"my_other_task": (), "my_other_task": (),
} }
super().__init__()
async def my_task(self) -> None: async def my_task(self) -> None:
logger.info("Triggered task.") logger.info("Triggered task.")
@ -52,8 +51,8 @@ def test_DataService_subclass_autostart_task_callback(
service = MyService() service = MyService()
service._task_manager.start_autostart_tasks() service._task_manager.start_autostart_tasks()
assert "MyService.sub_service.my_task changed to {}" in caplog.text assert "'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_other_task' changed to '{}'" in caplog.text
def test_DataServiceList_subclass_autostart_task_callback( def test_DataServiceList_subclass_autostart_task_callback(
@ -61,11 +60,11 @@ def test_DataServiceList_subclass_autostart_task_callback(
) -> None: ) -> None:
class MySubService(pydase.DataService): class MySubService(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self._autostart_tasks = { # type: ignore self._autostart_tasks = { # type: ignore
"my_task": (), "my_task": (),
"my_other_task": (), "my_other_task": (),
} }
super().__init__()
async def my_task(self) -> None: async def my_task(self) -> None:
logger.info("Triggered task.") logger.info("Triggered task.")
@ -79,7 +78,7 @@ def test_DataServiceList_subclass_autostart_task_callback(
service = MyService() service = MyService()
service._task_manager.start_autostart_tasks() service._task_manager.start_autostart_tasks()
assert "MyService.sub_services_list[0].my_task changed to {}" in caplog.text assert "'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 "'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 "'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[1].my_other_task' changed to '{}'" in caplog.text

View File

@ -1,6 +1,7 @@
from pytest import LogCaptureFixture
from pydase import DataService 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: def test_class_attributes(caplog: LogCaptureFixture) -> None:
@ -11,9 +12,11 @@ def test_class_attributes(caplog: LogCaptureFixture) -> None:
attr_1 = SubClass() attr_1 = SubClass()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr_1.name = "Hi" 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: def test_instance_attributes(caplog: LogCaptureFixture) -> None:
@ -22,13 +25,15 @@ def test_instance_attributes(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr_1 = SubClass()
super().__init__() super().__init__()
self.attr_1 = SubClass()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr_1.name = "Hi" 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: def test_class_attribute(caplog: LogCaptureFixture) -> None:
@ -36,21 +41,25 @@ def test_class_attribute(caplog: LogCaptureFixture) -> None:
attr = 0 attr = 0
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr = 1 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: def test_instance_attribute(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr = "Hello World"
super().__init__() super().__init__()
self.attr = "Hello World"
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr = "Hello" 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: def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None:
@ -61,16 +70,19 @@ def test_reused_instance_attributes(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr_1 = subclass_instance self.attr_1 = subclass_instance
self.attr_2 = subclass_instance self.attr_2 = subclass_instance
super().__init__()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr_1.name = "Hi" service_instance.attr_1.name = "Hi"
assert service_instance.attr_1 == service_instance.attr_2 assert service_instance.attr_1 == service_instance.attr_2
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text assert "'attr_1.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.attr_2.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: def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None:
@ -83,15 +95,18 @@ def test_reused_attributes_mixed(caplog: LogCaptureFixture) -> None:
attr_1 = subclass_instance attr_1 = subclass_instance
def __init__(self) -> None: def __init__(self) -> None:
self.attr_2 = subclass_instance
super().__init__() super().__init__()
self.attr_2 = subclass_instance
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr_1.name = "Hi" service_instance.attr_1.name = "Hi"
assert service_instance.attr_1 == service_instance.attr_2 assert service_instance.attr_1 == service_instance.attr_2
assert "ServiceClass.attr_1.name changed to Hi" in caplog.text assert "'attr_1.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.attr_2.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: def test_nested_class_attributes(caplog: LogCaptureFixture) -> None:
@ -111,15 +126,18 @@ def test_nested_class_attributes(caplog: LogCaptureFixture) -> None:
attr = SubClass() attr = SubClass()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.attr.name = "Hi"
service_instance.attr.attr.name = "Hou" service_instance.attr.attr.name = "Hou"
service_instance.attr.name = "foo" service_instance.attr.name = "foo"
service_instance.name = "bar" service_instance.name = "bar"
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text assert "'attr.attr.name' changed to 'Hou'" in caplog.text
assert "ServiceClass.attr.name changed to foo" in caplog.text assert "'attr.name' changed to 'foo'" in caplog.text
assert "ServiceClass.name changed to bar" in caplog.text assert "'name' changed to 'bar'" in caplog.text
def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None: def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
@ -128,32 +146,35 @@ def test_nested_instance_attributes(caplog: LogCaptureFixture) -> None:
class SubSubClass(DataService): class SubSubClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr = SubSubSubClass() self.attr = SubSubSubClass()
self.name = "Hello" self.name = "Hello"
super().__init__()
class SubClass(DataService): class SubClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr = SubSubClass() self.attr = SubSubClass()
self.name = "Hello" self.name = "Hello"
super().__init__()
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr = SubClass() self.attr = SubClass()
self.name = "Hello" self.name = "Hello"
super().__init__()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.attr.name = "Hi"
service_instance.attr.attr.name = "Hou" service_instance.attr.attr.name = "Hou"
service_instance.attr.name = "foo" service_instance.attr.name = "foo"
service_instance.name = "bar" service_instance.name = "bar"
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.attr.attr.name changed to Hou" in caplog.text assert "'attr.attr.name' changed to 'Hou'" in caplog.text
assert "ServiceClass.attr.name changed to foo" in caplog.text assert "'attr.name' changed to 'foo'" in caplog.text
assert "ServiceClass.name changed to bar" in caplog.text assert "'name' changed to 'bar'" in caplog.text
def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None: def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None:
@ -171,14 +192,19 @@ def test_advanced_nested_class_attributes(caplog: LogCaptureFixture) -> None:
subattr = SubSubClass() subattr = SubSubClass()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.attr.name = "Hi"
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.subattr.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" service_instance.subattr.attr.name = "Ho"
assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text
assert "ServiceClass.subattr.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: 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): class SubSubClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr = SubSubSubClass()
super().__init__() super().__init__()
self.attr = SubSubSubClass()
subsubclass_instance = SubSubClass() subsubclass_instance = SubSubClass()
class SubClass(DataService): class SubClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr = subsubclass_instance
super().__init__() super().__init__()
self.attr = subsubclass_instance
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr = SubClass() self.attr = SubClass()
self.subattr = subsubclass_instance self.subattr = subsubclass_instance
super().__init__()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.attr.attr.attr.name = "Hi" service_instance.attr.attr.attr.name = "Hi"
assert "ServiceClass.attr.attr.attr.name changed to Hi" in caplog.text assert "'attr.attr.attr.name' changed to 'Hi'" in caplog.text
assert "ServiceClass.subattr.attr.name changed to Hi" in caplog.text assert "'subattr.attr.name' changed to 'Hi'" in caplog.text
caplog.clear() caplog.clear()
service_instance.subattr.attr.name = "Ho" service_instance.subattr.attr.name = "Ho"
assert "ServiceClass.attr.attr.attr.name changed to Ho" in caplog.text assert "'attr.attr.attr.name' changed to 'Ho'" in caplog.text
assert "ServiceClass.subattr.attr.name changed to Ho" in caplog.text assert "'subattr.attr.name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
@ -224,17 +252,20 @@ def test_advanced_nested_attributes_mixed(caplog: LogCaptureFixture) -> None:
class_attr = SubSubClass() class_attr = SubSubClass()
def __init__(self) -> None: def __init__(self) -> None:
self.attr_1 = SubSubClass()
super().__init__() super().__init__()
self.attr_1 = SubSubClass()
class ServiceClass(DataService): class ServiceClass(DataService):
class_attr = SubClass() class_attr = SubClass()
def __init__(self) -> None: def __init__(self) -> None:
self.attr = SubClass()
super().__init__() super().__init__()
self.attr = SubClass()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
# Subclass.attr is the same for all instances # Subclass.attr is the same for all instances
assert service_instance.attr.class_attr == service_instance.class_attr.class_attr 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 assert service_instance.attr.attr_1 != service_instance.class_attr.class_attr
service_instance.class_attr.class_attr.name = "Ho" service_instance.class_attr.class_attr.name = "Ho"
assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text assert "'class_attr.class_attr.name' changed to 'Ho'" in caplog.text
assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text assert "'attr.class_attr.name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
service_instance.class_attr.attr_1.name = "Ho" service_instance.class_attr.attr_1.name = "Ho"
assert "ServiceClass.class_attr.attr_1.name changed to Ho" in caplog.text assert "'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 "'attr.attr_1.name' changed to 'Ho'" not in caplog.text
caplog.clear() caplog.clear()
service_instance.attr.class_attr.name = "Ho" service_instance.attr.class_attr.name = "Hello"
assert "ServiceClass.class_attr.class_attr.name changed to Ho" in caplog.text assert "'class_attr.class_attr.name' changed to 'Hello'" in caplog.text
assert "ServiceClass.attr.class_attr.name changed to Ho" in caplog.text assert "'attr.class_attr.name' changed to 'Hello'" in caplog.text
caplog.clear() caplog.clear()
service_instance.attr.attr_1.name = "Ho" service_instance.attr.attr_1.name = "Ho"
assert "ServiceClass.attr.attr_1.name changed to Ho" in caplog.text assert "'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 "'class_attr.attr_1.name' changed to 'Ho'" not in caplog.text
caplog.clear() caplog.clear()
@ -277,32 +308,34 @@ def test_class_list_attributes(caplog: LogCaptureFixture) -> None:
attr = subclass_instance attr = subclass_instance
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
assert service_instance.attr_list[0] != service_instance.attr_list[1] assert service_instance.attr_list[0] != service_instance.attr_list[1]
service_instance.attr_list[0].name = "Ho" service_instance.attr_list[0].name = "Ho"
assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text assert "'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[1].name' changed to 'Ho'" not in caplog.text
caplog.clear() caplog.clear()
service_instance.attr_list[1].name = "Ho" service_instance.attr_list[1].name = "Ho"
assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text assert "'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[1].name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
assert service_instance.attr_list_2[0] == service_instance.attr assert service_instance.attr_list_2[0] == service_instance.attr
assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1] assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1]
service_instance.attr_list_2[0].name = "Ho" service_instance.attr_list_2[0].name = "Ciao"
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text
assert "ServiceClass.attr.name changed to Ho" in caplog.text assert "'attr.name' changed to 'Ciao'" in caplog.text
caplog.clear() caplog.clear()
service_instance.attr_list_2[1].name = "Ho" service_instance.attr_list_2[1].name = "Bye"
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text
assert "ServiceClass.attr.name changed to Ho" in caplog.text assert "'attr.name' changed to 'Bye'" in caplog.text
caplog.clear() caplog.clear()
@ -320,17 +353,19 @@ def test_nested_class_list_attributes(caplog: LogCaptureFixture) -> None:
subattr = subsubclass_instance subattr = subsubclass_instance
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
assert service_instance.attr[0].attr_list[0] == service_instance.subattr assert service_instance.attr[0].attr_list[0] == service_instance.subattr
service_instance.attr[0].attr_list[0].name = "Ho" service_instance.attr[0].attr_list[0].name = "Ho"
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text
assert "ServiceClass.subattr.name changed to Ho" in caplog.text assert "'subattr.name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
service_instance.subattr.name = "Ho" service_instance.subattr.name = "Hi"
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text
assert "ServiceClass.subattr.name changed to Ho" in caplog.text assert "'subattr.name' changed to 'Hi'" in caplog.text
caplog.clear() caplog.clear()
@ -342,44 +377,46 @@ def test_instance_list_attributes(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.attr_list = [SubClass() for _ in range(2)] self.attr_list = [SubClass() for _ in range(2)]
self.attr_list_2 = [subclass_instance, subclass_instance] self.attr_list_2 = [subclass_instance, subclass_instance]
self.attr = subclass_instance self.attr = subclass_instance
super().__init__()
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
assert service_instance.attr_list[0] != service_instance.attr_list[1] assert service_instance.attr_list[0] != service_instance.attr_list[1]
service_instance.attr_list[0].name = "Ho" service_instance.attr_list[0].name = "Ho"
assert "ServiceClass.attr_list[0].name changed to Ho" in caplog.text assert "'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[1].name' changed to 'Ho'" not in caplog.text
caplog.clear() caplog.clear()
service_instance.attr_list[1].name = "Ho" service_instance.attr_list[1].name = "Hi"
assert "ServiceClass.attr_list[0].name changed to Ho" not in caplog.text assert "'attr_list[0].name' changed to 'Hi'" not in caplog.text
assert "ServiceClass.attr_list[1].name changed to Ho" in caplog.text assert "'attr_list[1].name' changed to 'Hi'" in caplog.text
caplog.clear() caplog.clear()
assert service_instance.attr_list_2[0] == service_instance.attr assert service_instance.attr_list_2[0] == service_instance.attr
assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1] assert service_instance.attr_list_2[0] == service_instance.attr_list_2[1]
service_instance.attr_list_2[0].name = "Ho" service_instance.attr_list_2[0].name = "Ciao"
assert "ServiceClass.attr.name changed to Ho" in caplog.text assert "'attr.name' changed to 'Ciao'" in caplog.text
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text assert "'attr_list_2[0].name' changed to 'Ciao'" in caplog.text
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text assert "'attr_list_2[1].name' changed to 'Ciao'" in caplog.text
caplog.clear() caplog.clear()
service_instance.attr_list_2[1].name = "Ho" service_instance.attr_list_2[1].name = "Bye"
assert "ServiceClass.attr.name changed to Ho" in caplog.text assert "'attr.name' changed to 'Bye'" in caplog.text
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text assert "'attr_list_2[0].name' changed to 'Bye'" in caplog.text
assert "ServiceClass.attr_list_2[1].name changed to Ho" in caplog.text assert "'attr_list_2[1].name' changed to 'Bye'" in caplog.text
caplog.clear() caplog.clear()
service_instance.attr.name = "Ho" service_instance.attr.name = "Ho"
assert "ServiceClass.attr.name changed to Ho" in caplog.text assert "'attr.name' changed to 'Ho'" in caplog.text
assert "ServiceClass.attr_list_2[0].name changed to Ho" in caplog.text assert "'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_list_2[1].name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
@ -391,26 +428,28 @@ def test_nested_instance_list_attributes(caplog: LogCaptureFixture) -> None:
class SubClass(DataService): class SubClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr_list = [subsubclass_instance]
super().__init__() super().__init__()
self.attr_list = [subsubclass_instance]
class ServiceClass(DataService): class ServiceClass(DataService):
class_attr = subsubclass_instance class_attr = subsubclass_instance
def __init__(self) -> None: def __init__(self) -> None:
self.attr = [SubClass()]
super().__init__() super().__init__()
self.attr = [SubClass()]
service_instance = ServiceClass() service_instance = ServiceClass()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
assert service_instance.attr[0].attr_list[0] == service_instance.class_attr assert service_instance.attr[0].attr_list[0] == service_instance.class_attr
service_instance.attr[0].attr_list[0].name = "Ho" service_instance.attr[0].attr_list[0].name = "Ho"
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text assert "'attr[0].attr_list[0].name' changed to 'Ho'" in caplog.text
assert "ServiceClass.class_attr.name changed to Ho" in caplog.text assert "'class_attr.name' changed to 'Ho'" in caplog.text
caplog.clear() caplog.clear()
service_instance.class_attr.name = "Ho" service_instance.class_attr.name = "Hi"
assert "ServiceClass.attr[0].attr_list[0].name changed to Ho" in caplog.text assert "'attr[0].attr_list[0].name' changed to 'Hi'" in caplog.text
assert "ServiceClass.class_attr.name changed to Ho" in caplog.text assert "'class_attr.name' changed to 'Hi'" in caplog.text
caplog.clear() caplog.clear()

View File

@ -1,6 +1,7 @@
from pytest import LogCaptureFixture
from pydase import DataService 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: def test_properties(caplog: LogCaptureFixture) -> None:
@ -28,281 +29,303 @@ def test_properties(caplog: LogCaptureFixture) -> None:
def current(self, value: float) -> None: def current(self, value: float) -> None:
self._current = value self._current = value
test_service = ServiceClass() service_instance = ServiceClass()
test_service.voltage = 1 state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
assert "ServiceClass.power changed to 1.0" in caplog.text service_instance.voltage = 1
assert "ServiceClass.voltage changed to 1.0" in caplog.text
assert "'power' changed to '1.0'" in caplog.text
assert "'voltage' changed to '1.0'" in caplog.text
caplog.clear() caplog.clear()
test_service.current = 12.0 service_instance.current = 12.0
assert "ServiceClass.power changed to 12.0" in caplog.text assert "'power' changed to '12.0'" in caplog.text
assert "ServiceClass.current changed to 12.0" in caplog.text assert "'current' changed to '12.0'" in caplog.text
def test_nested_properties(caplog: LogCaptureFixture) -> None: # def test_nested_properties(caplog: LogCaptureFixture) -> None:
class SubSubClass(DataService): # class SubSubClass(DataService):
name = "Hello" # name = "Hello"
#
class SubClass(DataService): # class SubClass(DataService):
name = "Hello" # name = "Hello"
class_attr = SubSubClass() # class_attr = SubSubClass()
#
class ServiceClass(DataService): # class ServiceClass(DataService):
class_attr = SubClass() # class_attr = SubClass()
name = "World" # name = "World"
#
@property # @property
def subsub_name(self) -> str: # def subsub_name(self) -> str:
return f"{self.class_attr.class_attr.name} {self.name}" # return f"{self.class_attr.class_attr.name} {self.name}"
#
@property # @property
def sub_name(self) -> str: # def sub_name(self) -> str:
return f"{self.class_attr.name} {self.name}" # return f"{self.class_attr.name} {self.name}"
#
test_service = ServiceClass() # service_instance = ServiceClass()
test_service.name = "Peepz" # state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager)
assert "ServiceClass.name changed to Peepz" in caplog.text #
assert "ServiceClass.sub_name changed to Hello Peepz" in caplog.text # service_instance.name = "Peepz"
assert "ServiceClass.subsub_name changed to Hello Peepz" in caplog.text #
caplog.clear() # assert "'name' changed to 'Peepz'" in caplog.text
# assert "'sub_name' changed to 'Hello Peepz'" in caplog.text
test_service.class_attr.name = "Hi" # assert "'subsub_name' changed to 'Hello Peepz'" in caplog.text
# caplog.clear()
assert "ServiceClass.sub_name changed to Hi Peepz" in caplog.text #
assert ( # service_instance.class_attr.name = "Hi"
"ServiceClass.subsub_name changed to Hello Peepz" in caplog.text #
) # registers subclass changes # assert "'sub_name' changed to 'Hi Peepz'" in caplog.text
assert "ServiceClass.class_attr.name changed to Hi" in caplog.text # assert (
caplog.clear() # "'subsub_name' changed to 'Hello Peepz'" in caplog.text
# ) # registers subclass changes
test_service.class_attr.class_attr.name = "Ciao" # assert "'class_attr.name' changed to 'Hi'" in caplog.text
# caplog.clear()
assert ( #
"ServiceClass.sub_name changed to Hi Peepz" in caplog.text # service_instance.class_attr.class_attr.name = "Ciao"
) # registers subclass changes #
assert "ServiceClass.subsub_name changed to Ciao Peepz" in caplog.text # assert (
assert "ServiceClass.class_attr.class_attr.name changed to Ciao" in caplog.text # "'sub_name' changed to 'Hi Peepz'" in caplog.text
caplog.clear() # ) # 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
def test_simple_list_properties(caplog: LogCaptureFixture) -> None: # caplog.clear()
class ServiceClass(DataService): #
list = ["Hello", "Ciao"] #
name = "World" # def test_simple_list_properties(caplog: LogCaptureFixture) -> None:
# class ServiceClass(DataService):
@property # list = ["Hello", "Ciao"]
def total_name(self) -> str: # name = "World"
return f"{self.list[0]} {self.name}" #
# @property
test_service = ServiceClass() # def total_name(self) -> str:
test_service.name = "Peepz" # return f"{self.list[0]} {self.name}"
#
assert "ServiceClass.name changed to Peepz" in caplog.text # service_instance = ServiceClass()
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text # state_manager = StateManager(service_instance)
caplog.clear() # DataServiceObserver(state_manager)
#
test_service.list[0] = "Hi" # service_instance.name = "Peepz"
#
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text # assert "'name' changed to 'Peepz'" in caplog.text
assert "ServiceClass.list[0] changed to Hi" in caplog.text # assert "'total_name' changed to 'Hello Peepz'" in caplog.text
# caplog.clear()
#
def test_class_list_properties(caplog: LogCaptureFixture) -> None: # service_instance.list[0] = "Hi"
class SubClass(DataService): #
name = "Hello" # assert "'total_name' changed to 'Hi Peepz'" in caplog.text
# assert "'list[0]' changed to 'Hi'" in caplog.text
class ServiceClass(DataService): #
list = [SubClass()] #
name = "World" # def test_class_list_properties(caplog: LogCaptureFixture) -> None:
# class SubClass(DataService):
@property # name = "Hello"
def total_name(self) -> str: #
return f"{self.list[0].name} {self.name}" # class ServiceClass(DataService):
# list = [SubClass()]
test_service = ServiceClass() # name = "World"
test_service.name = "Peepz" #
# @property
assert "ServiceClass.name changed to Peepz" in caplog.text # def total_name(self) -> str:
assert "ServiceClass.total_name changed to Hello Peepz" in caplog.text # return f"{self.list[0].name} {self.name}"
caplog.clear() #
# service_instance = ServiceClass()
test_service.list[0].name = "Hi" # state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager)
assert "ServiceClass.total_name changed to Hi Peepz" in caplog.text #
assert "ServiceClass.list[0].name changed to Hi" in caplog.text # service_instance.name = "Peepz"
#
# assert "'name' changed to 'Peepz'" in caplog.text
def test_subclass_properties(caplog: LogCaptureFixture) -> None: # assert "'total_name' changed to 'Hello Peepz'" in caplog.text
class SubClass(DataService): # caplog.clear()
name = "Hello" #
_voltage = 10.0 # service_instance.list[0].name = "Hi"
_current = 1.0 #
# assert "'total_name' changed to 'Hi Peepz'" in caplog.text
@property # assert "'list[0].name' changed to 'Hi'" in caplog.text
def power(self) -> float: #
return self._voltage * self.current #
# def test_subclass_properties(caplog: LogCaptureFixture) -> None:
@property # class SubClass(DataService):
def voltage(self) -> float: # name = "Hello"
return self._voltage # _voltage = 10.0
# _current = 1.0
@voltage.setter #
def voltage(self, value: float) -> None: # @property
self._voltage = value # def power(self) -> float:
# return self._voltage * self.current
@property #
def current(self) -> float: # @property
return self._current # def voltage(self) -> float:
# return self._voltage
@current.setter #
def current(self, value: float) -> None: # @voltage.setter
self._current = value # def voltage(self, value: float) -> None:
# self._voltage = value
class ServiceClass(DataService): #
class_attr = SubClass() # @property
# def current(self) -> float:
@property # return self._current
def voltage(self) -> float: #
return self.class_attr.voltage # @current.setter
# def current(self, value: float) -> None:
test_service = ServiceClass() # self._current = value
test_service.class_attr.voltage = 10.0 #
# class ServiceClass(DataService):
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for # class_attr = SubClass()
# changing voltage, and once for changing power. #
assert "ServiceClass.class_attr.voltage changed to 10.0" in caplog.text # @property
assert "ServiceClass.class_attr.power changed to 10.0" in caplog.text # def voltage(self) -> float:
assert "ServiceClass.voltage changed to 10.0" in caplog.text # return self.class_attr.voltage
caplog.clear() #
# service_instance = ServiceClass()
# state_manager = StateManager(service_instance)
def test_subclass_properties_2(caplog: LogCaptureFixture) -> None: # DataServiceObserver(state_manager)
class SubClass(DataService): #
name = "Hello" # service_instance.class_attr.voltage = 10.0
_voltage = 10.0 #
_current = 1.0 # # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
# # changing voltage, and once for changing power.
@property # assert "'class_attr.voltage' changed to '10.0'" in caplog.text
def power(self) -> float: # assert "'class_attr.power' changed to '10.0'" in caplog.text
return self._voltage * self.current # assert "'voltage' changed to '10.0'" in caplog.text
# caplog.clear()
@property #
def voltage(self) -> float: #
return self._voltage # def test_subclass_properties_2(caplog: LogCaptureFixture) -> None:
# class SubClass(DataService):
@voltage.setter # name = "Hello"
def voltage(self, value: float) -> None: # _voltage = 10.0
self._voltage = value # _current = 1.0
#
@property # @property
def current(self) -> float: # def power(self) -> float:
return self._current # return self._voltage * self.current
#
@current.setter # @property
def current(self, value: float) -> None: # def voltage(self) -> float:
self._current = value # return self._voltage
#
class ServiceClass(DataService): # @voltage.setter
class_attr = [SubClass() for i in range(2)] # def voltage(self, value: float) -> None:
# self._voltage = value
@property #
def voltage(self) -> float: # @property
return self.class_attr[0].voltage # def current(self) -> float:
# return self._current
test_service = ServiceClass() #
test_service.class_attr[1].current = 10.0 # @current.setter
# def current(self, value: float) -> None:
# using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for # self._current = value
# 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 # class ServiceClass(DataService):
# because every time any item in the list `test_service.class_attr` is changed, # class_attr = [SubClass() for i in range(2)]
# a notification will be emitted. #
assert "ServiceClass.class_attr[1].current changed to 10.0" in caplog.text # @property
assert "ServiceClass.class_attr[1].power changed to 100.0" in caplog.text # def voltage(self) -> float:
assert "ServiceClass.voltage changed to 10.0" in caplog.text # return self.class_attr[0].voltage
#
# service_instance = ServiceClass()
def test_subsubclass_properties(caplog: LogCaptureFixture) -> None: # state_manager = StateManager(service_instance)
class SubSubClass(DataService): # DataServiceObserver(state_manager)
_voltage = 10.0 #
# service_instance.class_attr[1].current = 10.0
@property #
def voltage(self) -> float: # # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for
return self._voltage # # 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
@voltage.setter # # because every time any item in the list `service_instance.class_attr` is changed,
def voltage(self, value: float) -> None: # # a notification will be emitted.
self._voltage = value # assert "'class_attr[1].current' changed to '10.0'" in caplog.text
# assert "'class_attr[1].power' changed to '100.0'" in caplog.text
class SubClass(DataService): # assert "'voltage' changed to '10.0'" in caplog.text
class_attr = SubSubClass() #
current = 0.5 #
# def test_subsubclass_properties(caplog: LogCaptureFixture) -> None:
@property # class SubSubClass(DataService):
def power(self) -> float: # _voltage = 10.0
return self.class_attr.voltage * self.current #
# @property
class ServiceClass(DataService): # def voltage(self) -> float:
class_attr = [SubClass() for i in range(2)] # return self._voltage
#
@property # @voltage.setter
def power(self) -> float: # def voltage(self, value: float) -> None:
return self.class_attr[0].power # self._voltage = value
#
test_service = ServiceClass() # class SubClass(DataService):
# class_attr = SubSubClass()
test_service.class_attr[1].class_attr.voltage = 100.0 # current = 0.5
assert ( #
"ServiceClass.class_attr[0].class_attr.voltage changed to 100.0" in caplog.text # @property
) # def power(self) -> float:
assert ( # return self.class_attr.voltage * self.current
"ServiceClass.class_attr[1].class_attr.voltage changed to 100.0" in caplog.text #
) # class ServiceClass(DataService):
assert "ServiceClass.class_attr[0].power changed to 50.0" in caplog.text # class_attr = [SubClass() for i in range(2)]
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text #
assert "ServiceClass.power changed to 50.0" in caplog.text # @property
# def power(self) -> float:
# return self.class_attr[0].power
def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None: #
class SubSubClass(DataService): # service_instance = ServiceClass()
def __init__(self) -> None: # state_manager = StateManager(service_instance)
self._voltage = 10.0 # DataServiceObserver(state_manager)
super().__init__() #
# service_instance.class_attr[1].class_attr.voltage = 100.0
@property # assert (
def voltage(self) -> float: # "'class_attr[0].class_attr.voltage' changed to '100.0'" in caplog.text
return self._voltage # )
# assert (
@voltage.setter # "'class_attr[1].class_attr.voltage' changed to '100.0'" in caplog.text
def voltage(self, value: float) -> None: # )
self._voltage = value # assert "'class_attr[0].power' changed to '50.0'" in caplog.text
# assert "'class_attr[1].power' changed to '50.0'" in caplog.text
class SubClass(DataService): # assert "'power' changed to '50.0'" in caplog.text
def __init__(self) -> None: #
self.attr = [SubSubClass()] #
self.current = 0.5 # def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
super().__init__() # class SubSubClass(DataService):
# def __init__(self) -> None:
@property # super().__init__()
def power(self) -> float: # self._voltage = 10.0
return self.attr[0].voltage * self.current #
# @property
class ServiceClass(DataService): # def voltage(self) -> float:
class_attr = [SubClass() for i in range(2)] # return self._voltage
#
@property # @voltage.setter
def power(self) -> float: # def voltage(self, value: float) -> None:
return self.class_attr[0].power # self._voltage = value
#
test_service = ServiceClass() # class SubClass(DataService):
# def __init__(self) -> None:
test_service.class_attr[1].attr[0].voltage = 100.0 # super().__init__()
# again, changing an item in a list will trigger the callbacks. This is why a # self.attr = [SubSubClass()]
# notification for `ServiceClass.power` is emitted although it did not change its # self.current = 0.5
# value #
assert "ServiceClass.class_attr[1].attr[0].voltage changed to 100.0" in caplog.text # @property
assert "ServiceClass.class_attr[1].power changed to 50.0" in caplog.text # def power(self) -> float:
assert "ServiceClass.power changed to 5.0" in caplog.text # 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

View File

@ -1,9 +1,10 @@
from typing import Any from typing import Any
from pytest import LogCaptureFixture
import pydase.units as u import pydase.units as u
from pydase.data_service.data_service import DataService 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: def test_DataService_setattr(caplog: LogCaptureFixture) -> None:
@ -19,26 +20,28 @@ def test_DataService_setattr(caplog: LogCaptureFixture) -> None:
def current(self, value: Any) -> None: def current(self, value: Any) -> None:
self._current = value 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 # You can just set floats to the Quantity objects. The DataService __setattr__ will
# automatically convert this # automatically convert this
service.voltage = 10.0 # type: ignore service_instance.voltage = 10.0 # type: ignore
service.current = 1.5 service_instance.current = 1.5
assert service.voltage == 10.0 * u.units.V # type: ignore assert service_instance.voltage == 10.0 * u.units.V # type: ignore
assert service.current == 1.5 * u.units.mA assert service_instance.current == 1.5 * u.units.mA
assert "ServiceClass.voltage changed to 10.0 V" in caplog.text assert "'voltage' changed to '10.0 V'" in caplog.text
assert "ServiceClass.current changed to 1.5 mA" in caplog.text assert "'current' changed to '1.5 mA'" in caplog.text
service.voltage = 12.0 * u.units.V # type: ignore service_instance.voltage = 12.0 * u.units.V # type: ignore
service.current = 1.51 * u.units.A service_instance.current = 1.51 * u.units.A
assert service.voltage == 12.0 * u.units.V # type: ignore assert service_instance.voltage == 12.0 * u.units.V # type: ignore
assert service.current == 1.51 * u.units.A assert service_instance.current == 1.51 * u.units.A
assert "ServiceClass.voltage changed to 12.0 V" in caplog.text assert "'voltage' changed to '12.0 V'" in caplog.text
assert "ServiceClass.current changed to 1.51 A" in caplog.text assert "'current' changed to '1.51 A'" in caplog.text
def test_convert_to_quantity() -> None: def test_convert_to_quantity() -> None:
@ -61,23 +64,25 @@ def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None:
def current(self, value: Any) -> None: def current(self, value: Any) -> None:
self._current = value 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 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"} 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: def test_autoconvert_offset_to_baseunit() -> None:
@ -104,9 +109,9 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None:
} }
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self): def __init__(self) -> None:
self._unit: u.Quantity = 1 * u.units.A
super().__init__() super().__init__()
self._unit: u.Quantity = 1 * u.units.A
@property @property
def some_unit(self) -> u.Quantity: def some_unit(self) -> u.Quantity:
@ -117,8 +122,10 @@ def test_loading_from_json(caplog: LogCaptureFixture) -> None:
assert isinstance(value, u.Quantity) assert isinstance(value, u.Quantity)
self._unit = value 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

View File

@ -1,10 +1,9 @@
import asyncio import asyncio
from enum import Enum from enum import Enum
import pytest
import pydase import pydase
import pydase.units as u import pydase.units as u
import pytest
from pydase.components.coloured_enum import ColouredEnum from pydase.components.coloured_enum import ColouredEnum
from pydase.utils.serializer import ( from pydase.utils.serializer import (
SerializationPathError, SerializationPathError,
@ -43,13 +42,13 @@ def test_enum_serialize() -> None:
class EnumAttribute(pydase.DataService): class EnumAttribute(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.some_enum = EnumClass.FOO
super().__init__() super().__init__()
self.some_enum = EnumClass.FOO
class EnumPropertyWithoutSetter(pydase.DataService): class EnumPropertyWithoutSetter(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
self._some_enum = EnumClass.FOO
super().__init__() super().__init__()
self._some_enum = EnumClass.FOO
@property @property
def some_enum(self) -> EnumClass: def some_enum(self) -> EnumClass:
@ -57,8 +56,8 @@ def test_enum_serialize() -> None:
class EnumPropertyWithSetter(pydase.DataService): class EnumPropertyWithSetter(pydase.DataService):
def __init__(self) -> None: def __init__(self) -> None:
self._some_enum = EnumClass.FOO
super().__init__() super().__init__()
self._some_enum = EnumClass.FOO
@property @property
def some_enum(self) -> EnumClass: def some_enum(self) -> EnumClass:

View File

@ -1,28 +1,27 @@
from pydase import DataService
from pytest import LogCaptureFixture from pytest import LogCaptureFixture
from pydase import DataService
def test_setattr_warnings(caplog: LogCaptureFixture) -> None:
def test_setattr_warnings(caplog: LogCaptureFixture) -> None: # noqa
# def test_setattr_warnings(capsys: CaptureFixture) -> None: # def test_setattr_warnings(capsys: CaptureFixture) -> None:
class SubClass: class SubClass:
name = "Hello" name = "Hello"
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.attr_1 = SubClass()
super().__init__() super().__init__()
self.attr_1 = SubClass()
ServiceClass() ServiceClass()
assert "Warning: Class 'SubClass' does not inherit from DataService." in caplog.text 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): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self.__something = ""
super().__init__() super().__init__()
self.__something = ""
ServiceClass() 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: class SubClass:
name = "Hello" name = "Hello"
class ServiceClass(DataService): class ServiceClass(DataService):
def __init__(self) -> None: def __init__(self) -> None:
self._subclass = SubClass
super().__init__() super().__init__()
self._subclass = SubClass
ServiceClass() ServiceClass()