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.units as u
import pytest
from pydase.components.coloured_enum import ColouredEnum
from pydase.data_service.data_service_observer import DataServiceObserver
from pydase.data_service.state_manager import (
StateManager,
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
file = tmp_path / "test_state.json"
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)
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
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)
service = Service()
manager = StateManager(service=service, filename=str(file))
manager.load_state()
state_manager = StateManager(service=service, filename=str(file))
DataServiceObserver(state_manager)
state_manager.load_state()
assert service.some_unit == u.Quantity(12, "A") # 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.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 (
"Property 'name' has no '@load_state' decorator. "
"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
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"
with pytest.warns(DeprecationWarning):
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
def test_filename_error(caplog: LogCaptureFixture):
def test_filename_error(caplog: LogCaptureFixture) -> None:
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
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
file = tmp_path / "test_state.json"
@ -221,7 +226,7 @@ def test_changed_type(tmp_path: Path, caplog: LogCaptureFixture):
) 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
file = tmp_path / "test_state.json"

View File

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

View File

@ -33,7 +33,7 @@ def test_properties(caplog: LogCaptureFixture) -> None:
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
service_instance.voltage = 1
service_instance.voltage = 1.0
assert "'power' 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
# 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
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 service_instance.subsub_name == "Hello Peepz"
assert "'sub_name' changed to 'Hi Peepz'" in caplog.text
assert "'subsub_name' " not in caplog.text # subsub_name does not depend on change
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" not in caplog.text
) # sub_name does not depend on change
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 = 11.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
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[0].current = 10.0
assert "'class_attr[0].current' changed to '10.0'" in caplog.text
assert "'class_attr[0].power' changed to '100.0'" in caplog.text
caplog.clear()
service_instance.class_attr[0].voltage = 11.0
assert "'class_attr[0].voltage' changed to '11.0'" in caplog.text
assert "'class_attr[0].power' changed to '110.0'" in caplog.text
assert "'voltage' changed to '11.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[0].attr[0].voltage = 100.0
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
assert "'power' changed to '50.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
def test_update_DataService_attribute(caplog: LogCaptureFixture) -> None:
def test_set_service_attribute_value_by_path(caplog: LogCaptureFixture) -> None:
class ServiceClass(DataService):
voltage = 1.0 * u.units.V
_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)
DataServiceObserver(state_manager)
service_instance.update_DataService_attribute(
path_list=[], attr_name="voltage", value=1.0 * u.units.mV
state_manager.set_service_attribute_value_by_path(
path="voltage", value=1.0 * u.units.mV
)
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
caplog.clear()
service_instance.update_DataService_attribute(
path_list=[], attr_name="voltage", value={"magnitude": 123, "unit": "kV"}
state_manager.set_service_attribute_value_by_path(
path="voltage", value={"magnitude": 123, "unit": "kV"}
)
assert "'voltage' changed to '123.0 kV'" in caplog.text

View File

@ -1,5 +1,6 @@
import asyncio
from enum import Enum
from typing import Any
import pydase
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
@ -400,17 +401,10 @@ def test_get_class_attribute_inside_list(setup_dict):
def test_get_invalid_list_index(setup_dict, caplog: pytest.LogCaptureFixture):
with pytest.raises(SerializationPathError):
get_nested_dict_by_path(setup_dict, "attr_list[10]")
assert (
"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):
with pytest.raises(SerializationPathError):
get_nested_dict_by_path(setup_dict, "invalid_path")
assert (
"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
)