updates tests

This commit is contained in:
Mose Müller 2023-12-04 17:23:42 +01:00
parent 0aa1595da4
commit 89b5a9cc9e
5 changed files with 321 additions and 324 deletions

View File

@ -4,7 +4,9 @@ from typing import Any
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.data_service.data_service_observer import DataServiceObserver
from pydase.data_service.state_manager import ( from pydase.data_service.state_manager import (
StateManager, StateManager,
has_load_state_decorator, has_load_state_decorator,
@ -116,7 +118,7 @@ LOAD_STATE = {
} }
def test_save_state(tmp_path: Path): def test_save_state(tmp_path: Path) -> None:
# Create a StateManager instance with a temporary file # Create a StateManager instance with a temporary file
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"
manager = StateManager(service=Service(), filename=str(file)) manager = StateManager(service=Service(), filename=str(file))
@ -128,7 +130,7 @@ def test_save_state(tmp_path: Path):
assert file.read_text() == json.dumps(CURRENT_STATE, indent=4) assert file.read_text() == json.dumps(CURRENT_STATE, indent=4)
def test_load_state(tmp_path: Path, caplog: LogCaptureFixture): def test_load_state(tmp_path: Path, caplog: LogCaptureFixture) -> None:
# Create a StateManager instance with a temporary file # Create a StateManager instance with a temporary file
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"
@ -137,8 +139,9 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
json.dump(LOAD_STATE, f, indent=4) json.dump(LOAD_STATE, f, indent=4)
service = Service() service = Service()
manager = StateManager(service=service, filename=str(file)) state_manager = StateManager(service=service, filename=str(file))
manager.load_state() DataServiceObserver(state_manager)
state_manager.load_state()
assert service.some_unit == u.Quantity(12, "A") # has changed assert service.some_unit == u.Quantity(12, "A") # has changed
assert service.list_attr[0] == 1.4 # has changed assert service.list_attr[0] == 1.4 # has changed
@ -151,7 +154,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 "'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
@ -167,15 +170,17 @@ def test_load_state(tmp_path: Path, caplog: LogCaptureFixture):
assert "Value of attribute 'subservice.name' has not changed..." in caplog.text assert "Value of attribute 'subservice.name' has not changed..." in caplog.text
def test_filename_warning(tmp_path: Path, caplog: LogCaptureFixture): def test_filename_warning(tmp_path: Path, caplog: LogCaptureFixture) -> None:
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"
service = Service(filename=str(file)) with pytest.warns(DeprecationWarning):
StateManager(service=service, filename=str(file)) service = Service(filename=str(file))
StateManager(service=service, filename=str(file))
assert f"Overwriting filename {str(file)!r} with {str(file)!r}." in caplog.text assert f"Overwriting filename {str(file)!r} with {str(file)!r}." in caplog.text
def test_filename_error(caplog: LogCaptureFixture): def test_filename_error(caplog: LogCaptureFixture) -> None:
service = Service() service = Service()
manager = StateManager(service=service) manager = StateManager(service=service)
@ -186,7 +191,7 @@ def test_filename_error(caplog: LogCaptureFixture):
) )
def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture): def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture) -> None:
# Create a StateManager instance with a temporary file # Create a StateManager instance with a temporary file
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"
@ -204,7 +209,7 @@ def test_readonly_attribute(tmp_path: Path, caplog: LogCaptureFixture):
) )
def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture): def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture) -> None:
# Create a StateManager instance with a temporary file # Create a StateManager instance with a temporary file
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"
@ -221,7 +226,7 @@ def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
) in caplog.text ) in caplog.text
def test_property_load_state(tmp_path: Path): def test_property_load_state(tmp_path: Path) -> None:
# Create a StateManager instance with a temporary file # Create a StateManager instance with a temporary file
file = tmp_path / "test_state.json" file = tmp_path / "test_state.json"

View File

@ -1,6 +1,8 @@
import logging import logging
import pydase import pydase
from pydase.data_service.data_service_observer import DataServiceObserver
from pydase.data_service.state_manager import StateManager
from pytest import LogCaptureFixture from pytest import LogCaptureFixture
logger = logging.getLogger() logger = logging.getLogger()
@ -21,8 +23,10 @@ def test_autostart_task_callback(caplog: LogCaptureFixture) -> None:
async def my_other_task(self) -> None: async def my_other_task(self) -> None:
logger.info("Triggered other task.") logger.info("Triggered other task.")
service = MyService() service_instance = MyService()
service._task_manager.start_autostart_tasks() state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance._task_manager.start_autostart_tasks()
assert "'my_task' changed to '{}'" in caplog.text assert "'my_task' changed to '{}'" in caplog.text
assert "'my_other_task' changed to '{}'" in caplog.text assert "'my_other_task' changed to '{}'" in caplog.text
@ -48,14 +52,16 @@ def test_DataService_subclass_autostart_task_callback(
class MyService(pydase.DataService): class MyService(pydase.DataService):
sub_service = MySubService() sub_service = MySubService()
service = MyService() service_instance = MyService()
service._task_manager.start_autostart_tasks() state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance._task_manager.start_autostart_tasks()
assert "'sub_service.my_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 assert "'sub_service.my_other_task' changed to '{}'" in caplog.text
def test_DataServiceList_subclass_autostart_task_callback( def test_DataService_subclass_list_autostart_task_callback(
caplog: LogCaptureFixture, caplog: LogCaptureFixture,
) -> None: ) -> None:
class MySubService(pydase.DataService): class MySubService(pydase.DataService):
@ -75,8 +81,10 @@ def test_DataServiceList_subclass_autostart_task_callback(
class MyService(pydase.DataService): class MyService(pydase.DataService):
sub_services_list = [MySubService() for i in range(2)] sub_services_list = [MySubService() for i in range(2)]
service = MyService() service_instance = MyService()
service._task_manager.start_autostart_tasks() state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance._task_manager.start_autostart_tasks()
assert "'sub_services_list[0].my_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[0].my_other_task' changed to '{}'" in caplog.text

View File

@ -33,7 +33,7 @@ def test_properties(caplog: LogCaptureFixture) -> None:
state_manager = StateManager(service_instance) state_manager = StateManager(service_instance)
DataServiceObserver(state_manager) DataServiceObserver(state_manager)
service_instance.voltage = 1 service_instance.voltage = 1.0
assert "'power' 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 assert "'voltage' changed to '1.0'" in caplog.text
@ -45,287 +45,277 @@ def test_properties(caplog: LogCaptureFixture) -> None:
assert "'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}"
#
# service_instance = ServiceClass() service_instance = ServiceClass()
# state_manager = StateManager(service_instance) state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager) DataServiceObserver(state_manager)
#
# service_instance.name = "Peepz" service_instance.name = "Peepz"
#
# assert "'name' changed to 'Peepz'" in caplog.text assert "'name' changed to 'Peepz'" in caplog.text
# assert "'sub_name' changed to 'Hello Peepz'" in caplog.text assert "'sub_name' changed to 'Hello Peepz'" in caplog.text
# assert "'subsub_name' changed to 'Hello Peepz'" in caplog.text assert "'subsub_name' changed to 'Hello Peepz'" in caplog.text
# caplog.clear() caplog.clear()
#
# service_instance.class_attr.name = "Hi" service_instance.class_attr.name = "Hi"
# assert service_instance.subsub_name == "Hello Peepz"
# assert "'sub_name' changed to 'Hi Peepz'" in caplog.text
# assert ( assert "'sub_name' changed to 'Hi Peepz'" in caplog.text
# "'subsub_name' changed to 'Hello Peepz'" in caplog.text assert "'subsub_name' " not in caplog.text # subsub_name does not depend on change
# ) # registers subclass changes assert "'class_attr.name' changed to 'Hi'" in caplog.text
# assert "'class_attr.name' changed to 'Hi'" in caplog.text caplog.clear()
# caplog.clear()
# service_instance.class_attr.class_attr.name = "Ciao"
# service_instance.class_attr.class_attr.name = "Ciao"
# assert (
# assert ( "'sub_name' changed to" not in caplog.text
# "'sub_name' changed to 'Hi Peepz'" in caplog.text ) # sub_name does not depend on change
# ) # registers subclass changes assert "'subsub_name' changed to 'Ciao Peepz'" in caplog.text
# assert "'subsub_name' changed to 'Ciao Peepz'" in caplog.text assert "'class_attr.class_attr.name' changed to 'Ciao'" in caplog.text
# assert "'class_attr.class_attr.name' changed to 'Ciao'" in caplog.text caplog.clear()
# caplog.clear()
#
# def test_simple_list_properties(caplog: LogCaptureFixture) -> None:
# def test_simple_list_properties(caplog: LogCaptureFixture) -> None: class ServiceClass(DataService):
# class ServiceClass(DataService): list = ["Hello", "Ciao"]
# list = ["Hello", "Ciao"] name = "World"
# name = "World"
# @property
# @property def total_name(self) -> str:
# def total_name(self) -> str: return f"{self.list[0]} {self.name}"
# return f"{self.list[0]} {self.name}"
# service_instance = ServiceClass()
# service_instance = ServiceClass() state_manager = StateManager(service_instance)
# state_manager = StateManager(service_instance) DataServiceObserver(state_manager)
# DataServiceObserver(state_manager)
# service_instance.name = "Peepz"
# service_instance.name = "Peepz"
# assert "'name' changed to 'Peepz'" in caplog.text
# assert "'name' changed to 'Peepz'" in caplog.text assert "'total_name' changed to 'Hello Peepz'" in caplog.text
# assert "'total_name' changed to 'Hello Peepz'" in caplog.text caplog.clear()
# caplog.clear()
# service_instance.list[0] = "Hi"
# service_instance.list[0] = "Hi"
# assert "'total_name' changed to 'Hi Peepz'" in caplog.text
# assert "'total_name' changed to 'Hi Peepz'" in caplog.text assert "'list[0]' changed to 'Hi'" in caplog.text
# assert "'list[0]' changed to 'Hi'" in caplog.text
#
# def test_class_list_properties(caplog: LogCaptureFixture) -> None:
# def test_class_list_properties(caplog: LogCaptureFixture) -> None: class SubClass(DataService):
# class SubClass(DataService): name = "Hello"
# name = "Hello"
# class ServiceClass(DataService):
# class ServiceClass(DataService): list = [SubClass()]
# list = [SubClass()] name = "World"
# name = "World"
# @property
# @property def total_name(self) -> str:
# def total_name(self) -> str: return f"{self.list[0].name} {self.name}"
# return f"{self.list[0].name} {self.name}"
# service_instance = ServiceClass()
# service_instance = ServiceClass() state_manager = StateManager(service_instance)
# state_manager = StateManager(service_instance) DataServiceObserver(state_manager)
# DataServiceObserver(state_manager)
# service_instance.name = "Peepz"
# service_instance.name = "Peepz"
# assert "'name' changed to 'Peepz'" in caplog.text
# assert "'name' changed to 'Peepz'" in caplog.text assert "'total_name' changed to 'Hello Peepz'" in caplog.text
# assert "'total_name' changed to 'Hello Peepz'" in caplog.text caplog.clear()
# caplog.clear()
# service_instance.list[0].name = "Hi"
# service_instance.list[0].name = "Hi"
# assert "'total_name' changed to 'Hi Peepz'" in caplog.text
# assert "'total_name' changed to 'Hi Peepz'" in caplog.text assert "'list[0].name' changed to 'Hi'" in caplog.text
# assert "'list[0].name' changed to 'Hi'" in caplog.text
#
# def test_subclass_properties(caplog: LogCaptureFixture) -> None:
# def test_subclass_properties(caplog: LogCaptureFixture) -> None: class SubClass(DataService):
# class SubClass(DataService): name = "Hello"
# name = "Hello" _voltage = 11.0
# _voltage = 10.0 _current = 1.0
# _current = 1.0
# @property
# @property def power(self) -> float:
# def power(self) -> float: return self._voltage * self.current
# return self._voltage * self.current
# @property
# @property def voltage(self) -> float:
# def voltage(self) -> float: return self._voltage
# return self._voltage
# @voltage.setter
# @voltage.setter def voltage(self, value: float) -> None:
# def voltage(self, value: float) -> None: self._voltage = value
# self._voltage = value
# @property
# @property def current(self) -> float:
# def current(self) -> float: return self._current
# return self._current
# @current.setter
# @current.setter def current(self, value: float) -> None:
# def current(self, value: float) -> None: self._current = value
# self._current = value
# class ServiceClass(DataService):
# class ServiceClass(DataService): class_attr = SubClass()
# class_attr = SubClass()
# @property
# @property def voltage(self) -> float:
# def voltage(self) -> float: return self.class_attr.voltage
# return self.class_attr.voltage
# service_instance = ServiceClass()
# service_instance = ServiceClass() state_manager = StateManager(service_instance)
# state_manager = StateManager(service_instance) DataServiceObserver(state_manager)
# DataServiceObserver(state_manager)
# service_instance.class_attr.voltage = 10.0
# service_instance.class_attr.voltage = 10.0
# assert "'class_attr.voltage' changed to '10.0'" in caplog.text
# # using a set here as "ServiceClass.voltage = 10.0" is emitted twice. Once for assert "'class_attr.power' changed to '10.0'" in caplog.text
# # changing voltage, and once for changing power. assert "'voltage' changed to '10.0'" in caplog.text
# assert "'class_attr.voltage' changed to '10.0'" in caplog.text caplog.clear()
# 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"
# def test_subclass_properties_2(caplog: LogCaptureFixture) -> None: _voltage = 10.0
# class SubClass(DataService): _current = 1.0
# name = "Hello"
# _voltage = 10.0 @property
# _current = 1.0 def power(self) -> float:
# return self._voltage * self.current
# @property
# def power(self) -> float: @property
# return self._voltage * self.current def voltage(self) -> float:
# return self._voltage
# @property
# def voltage(self) -> float: @voltage.setter
# return self._voltage def voltage(self, value: float) -> None:
# self._voltage = value
# @voltage.setter
# def voltage(self, value: float) -> None: @property
# self._voltage = value def current(self) -> float:
# return self._current
# @property
# def current(self) -> float: @current.setter
# return self._current def current(self, value: float) -> None:
# self._current = value
# @current.setter
# def current(self, value: float) -> None: class ServiceClass(DataService):
# self._current = value class_attr = [SubClass() for i in range(2)]
#
# class ServiceClass(DataService): @property
# class_attr = [SubClass() for i in range(2)] def voltage(self) -> float:
# return self.class_attr[0].voltage
# @property
# def voltage(self) -> float: service_instance = ServiceClass()
# return self.class_attr[0].voltage state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager)
# service_instance = ServiceClass()
# state_manager = StateManager(service_instance) service_instance.class_attr[0].current = 10.0
# DataServiceObserver(state_manager)
# assert "'class_attr[0].current' changed to '10.0'" in caplog.text
# service_instance.class_attr[1].current = 10.0 assert "'class_attr[0].power' changed to '100.0'" in caplog.text
# caplog.clear()
# # 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 service_instance.class_attr[0].voltage = 11.0
# # only dependent on class_attr[0] but still emits an update notification. This is assert "'class_attr[0].voltage' changed to '11.0'" in caplog.text
# # because every time any item in the list `service_instance.class_attr` is changed, assert "'class_attr[0].power' changed to '110.0'" in caplog.text
# # a notification will be emitted. assert "'voltage' changed to '11.0'" in caplog.text
# 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
# def test_subsubclass_properties(caplog: LogCaptureFixture) -> None:
# class SubSubClass(DataService): @property
# _voltage = 10.0 def voltage(self) -> float:
# return self._voltage
# @property
# def voltage(self) -> float: @voltage.setter
# return self._voltage def voltage(self, value: float) -> None:
# self._voltage = value
# @voltage.setter
# def voltage(self, value: float) -> None: class SubClass(DataService):
# self._voltage = value class_attr = SubSubClass()
# current = 0.5
# class SubClass(DataService):
# class_attr = SubSubClass() @property
# current = 0.5 def power(self) -> float:
# return self.class_attr.voltage * self.current
# @property
# def power(self) -> float: class ServiceClass(DataService):
# return self.class_attr.voltage * self.current class_attr = [SubClass() for i in range(2)]
#
# class ServiceClass(DataService): @property
# class_attr = [SubClass() for i in range(2)] def power(self) -> float:
# return self.class_attr[0].power
# @property
# def power(self) -> float: service_instance = ServiceClass()
# return self.class_attr[0].power state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager)
# service_instance = ServiceClass()
# state_manager = StateManager(service_instance) service_instance.class_attr[1].class_attr.voltage = 100.0
# DataServiceObserver(state_manager) 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
# service_instance.class_attr[1].class_attr.voltage = 100.0 assert "'class_attr[0].power' changed to '50.0'" in caplog.text
# assert ( assert "'class_attr[1].power' changed to '50.0'" in caplog.text
# "'class_attr[0].class_attr.voltage' changed to '100.0'" in caplog.text assert "'power' changed to '50.0'" in caplog.text
# )
# assert (
# "'class_attr[1].class_attr.voltage' changed to '100.0'" in caplog.text def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None:
# ) class SubSubClass(DataService):
# assert "'class_attr[0].power' changed to '50.0'" in caplog.text def __init__(self) -> None:
# assert "'class_attr[1].power' changed to '50.0'" in caplog.text super().__init__()
# assert "'power' changed to '50.0'" in caplog.text self._voltage = 10.0
#
# @property
# def test_subsubclass_instance_properties(caplog: LogCaptureFixture) -> None: def voltage(self) -> float:
# class SubSubClass(DataService): return self._voltage
# def __init__(self) -> None:
# super().__init__() @voltage.setter
# self._voltage = 10.0 def voltage(self, value: float) -> None:
# self._voltage = value
# @property
# def voltage(self) -> float: class SubClass(DataService):
# return self._voltage def __init__(self) -> None:
# super().__init__()
# @voltage.setter self.attr = [SubSubClass()]
# def voltage(self, value: float) -> None: self.current = 0.5
# self._voltage = value
# @property
# class SubClass(DataService): def power(self) -> float:
# def __init__(self) -> None: return self.attr[0].voltage * self.current
# super().__init__()
# self.attr = [SubSubClass()] class ServiceClass(DataService):
# self.current = 0.5 class_attr = [SubClass() for i in range(2)]
#
# @property @property
# def power(self) -> float: def power(self) -> float:
# return self.attr[0].voltage * self.current return self.class_attr[0].power
#
# class ServiceClass(DataService): service_instance = ServiceClass()
# class_attr = [SubClass() for i in range(2)] state_manager = StateManager(service_instance)
# DataServiceObserver(state_manager)
# @property
# def power(self) -> float: service_instance.class_attr[0].attr[0].voltage = 100.0
# return self.class_attr[0].power assert "'class_attr[0].attr[0].voltage' changed to '100.0'" in caplog.text
# assert "'class_attr[0].power' changed to '50.0'" in caplog.text
# service_instance = ServiceClass() assert "'power' changed to '50.0'" in caplog.text
# 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

@ -51,7 +51,7 @@ def test_convert_to_quantity() -> None:
assert u.convert_to_quantity(1.0 * u.units.mV) == 1.0 * u.units.mV assert u.convert_to_quantity(1.0 * u.units.mV) == 1.0 * u.units.mV
def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None: def test_set_service_attribute_value_by_path(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService): class ServiceClass(DataService):
voltage = 1.0 * u.units.V voltage = 1.0 * u.units.V
_current: u.Quantity = 1.0 * u.units.mA _current: u.Quantity = 1.0 * u.units.mA
@ -68,20 +68,20 @@ def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None:
state_manager = StateManager(service_instance) state_manager = StateManager(service_instance)
DataServiceObserver(state_manager) DataServiceObserver(state_manager)
service_instance.update_DataService_attribute( state_manager.set_service_attribute_value_by_path(
path_list=[], attr_name="voltage", value=1.0 * u.units.mV path="voltage", value=1.0 * u.units.mV
) )
assert "'voltage' changed to '1.0 mV'" in caplog.text assert "'voltage' changed to '1.0 mV'" in caplog.text
caplog.clear()
service_instance.update_DataService_attribute(path_list=[], attr_name="voltage", value=2) state_manager.set_service_attribute_value_by_path(path="voltage", value=2)
assert "'voltage' changed to '2.0 mV'" in caplog.text assert "'voltage' changed to '2.0 mV'" in caplog.text
caplog.clear()
service_instance.update_DataService_attribute( state_manager.set_service_attribute_value_by_path(
path_list=[], attr_name="voltage", value={"magnitude": 123, "unit": "kV"} path="voltage", value={"magnitude": 123, "unit": "kV"}
) )
assert "'voltage' changed to '123.0 kV'" in caplog.text assert "'voltage' changed to '123.0 kV'" in caplog.text

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
from enum import Enum from enum import Enum
from typing import Any
import pydase import pydase
import pydase.units as u import pydase.units as u
@ -31,7 +32,7 @@ from pydase.utils.serializer import (
), ),
], ],
) )
def test_dump(test_input, expected): def test_dump(test_input: Any, expected: dict[str, Any]) -> None:
assert dump(test_input) == expected assert dump(test_input) == expected
@ -400,17 +401,10 @@ def test_get_class_attribute_inside_list(setup_dict):
def test_get_invalid_list_index(setup_dict, caplog: pytest.LogCaptureFixture): def test_get_invalid_list_index(setup_dict, caplog: pytest.LogCaptureFixture):
get_nested_dict_by_path(setup_dict, "attr_list[10]") with pytest.raises(SerializationPathError):
assert ( get_nested_dict_by_path(setup_dict, "attr_list[10]")
"Error occured trying to change 'attr_list[10]': list index "
"out of range" in caplog.text
)
def test_get_invalid_path(setup_dict, caplog: pytest.LogCaptureFixture): def test_get_invalid_path(setup_dict, caplog: pytest.LogCaptureFixture):
get_nested_dict_by_path(setup_dict, "invalid_path") with pytest.raises(SerializationPathError):
assert ( get_nested_dict_by_path(setup_dict, "invalid_path")
"Error occured trying to access the key 'invalid_path': it is either "
"not present in the current dictionary or its value does not contain "
"a 'value' key." in caplog.text
)