mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-05-01 13:00:08 +02:00
Merge pull request #108 from tiqi-group/fix/cache_update_on_type_change
Fix/cache update on type change
This commit is contained in:
commit
441658ebc1
@ -44,6 +44,12 @@ class DataServiceObserver(PropertyObserver):
|
||||
|
||||
self._update_cache_value(full_access_path, value, cached_value_dict)
|
||||
|
||||
cached_value_dict = deepcopy(
|
||||
self.state_manager._data_service_cache.get_value_dict_from_cache(
|
||||
full_access_path
|
||||
)
|
||||
)
|
||||
|
||||
for callback in self._notification_callbacks:
|
||||
callback(full_access_path, value, cached_value_dict)
|
||||
|
||||
|
@ -9,7 +9,6 @@ from pydase.data_service.data_service_observer import DataServiceObserver
|
||||
from pydase.data_service.state_manager import StateManager
|
||||
from pydase.utils.helpers import get_object_attr_from_path_list
|
||||
from pydase.utils.logging import SocketIOHandler
|
||||
from pydase.utils.serializer import dump
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -62,7 +61,7 @@ class RunMethodDict(TypedDict):
|
||||
kwargs: dict[str, Any]
|
||||
|
||||
|
||||
def setup_sio_server( # noqa: C901
|
||||
def setup_sio_server(
|
||||
observer: DataServiceObserver,
|
||||
enable_cors: bool,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
@ -97,15 +96,6 @@ def setup_sio_server( # noqa: C901
|
||||
full_access_path: str, value: Any, cached_value_dict: dict[str, Any]
|
||||
) -> None:
|
||||
if cached_value_dict != {}:
|
||||
serialized_value = dump(value)
|
||||
if cached_value_dict["type"] != "method":
|
||||
cached_value_dict["type"] = serialized_value["type"]
|
||||
|
||||
cached_value_dict["value"] = serialized_value["value"]
|
||||
|
||||
# Check if the serialized value contains an "enum" key, and if so, copy it
|
||||
if "enum" in serialized_value:
|
||||
cached_value_dict["enum"] = serialized_value["enum"]
|
||||
|
||||
async def notify() -> None:
|
||||
try:
|
||||
|
@ -269,13 +269,20 @@ def set_nested_value_by_path(
|
||||
|
||||
# setting the new value
|
||||
serialized_value = dump(value)
|
||||
if "readonly" in current_dict:
|
||||
if current_dict["type"] != "method":
|
||||
current_dict["type"] = serialized_value["type"]
|
||||
current_dict["value"] = serialized_value["value"]
|
||||
else:
|
||||
serialized_value.pop("readonly", None)
|
||||
value_type = serialized_value.pop("type")
|
||||
if "readonly" in current_dict and current_dict["type"] != "method":
|
||||
current_dict["type"] = value_type
|
||||
|
||||
current_dict.update(serialized_value)
|
||||
|
||||
# removes keys that are not present in the serialized new value
|
||||
keys_to_keep = set(serialized_value.keys()) | {"type", "readonly"}
|
||||
|
||||
for key in list(current_dict.keys()):
|
||||
if key not in keys_to_keep:
|
||||
current_dict.pop(key, None)
|
||||
|
||||
|
||||
def get_nested_dict_by_path(
|
||||
serialization_dict: dict[str, Any],
|
||||
|
@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import enum
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
@ -18,6 +19,11 @@ from pydase.utils.serializer import (
|
||||
)
|
||||
|
||||
|
||||
class MyEnum(enum.Enum):
|
||||
RUNNING = "running"
|
||||
FINISHED = "finished"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test_input, expected",
|
||||
[
|
||||
@ -396,33 +402,55 @@ def setup_dict() -> dict[str, Any]:
|
||||
class ServiceClass(pydase.DataService):
|
||||
attr1 = 1.0
|
||||
attr2 = MySubclass()
|
||||
enum_attr = MyEnum.RUNNING
|
||||
attr_list = [0, 1, MySubclass()]
|
||||
|
||||
return ServiceClass().serialize()["value"]
|
||||
|
||||
|
||||
def test_update_attribute(setup_dict) -> None:
|
||||
def test_update_attribute(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr1", 15)
|
||||
assert setup_dict["attr1"]["value"] == 15
|
||||
|
||||
|
||||
def test_update_nested_attribute(setup_dict) -> None:
|
||||
def test_update_nested_attribute(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr2.attr3", 25.0)
|
||||
assert setup_dict["attr2"]["value"]["attr3"]["value"] == 25.0
|
||||
|
||||
|
||||
def test_update_list_entry(setup_dict) -> None:
|
||||
def test_update_float_attribute_to_enum(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr2.attr3", MyEnum.RUNNING)
|
||||
assert setup_dict["attr2"]["value"]["attr3"] == {
|
||||
"doc": None,
|
||||
"enum": {"FINISHED": "finished", "RUNNING": "running"},
|
||||
"readonly": False,
|
||||
"type": "Enum",
|
||||
"value": "RUNNING",
|
||||
}
|
||||
|
||||
|
||||
def test_update_enum_attribute_to_float(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "enum_attr", 1.01)
|
||||
assert setup_dict["enum_attr"] == {
|
||||
"doc": None,
|
||||
"readonly": False,
|
||||
"type": "float",
|
||||
"value": 1.01,
|
||||
}
|
||||
|
||||
|
||||
def test_update_list_entry(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr_list[1]", 20)
|
||||
assert setup_dict["attr_list"]["value"][1]["value"] == 20
|
||||
|
||||
|
||||
def test_update_list_append(setup_dict) -> None:
|
||||
def test_update_list_append(setup_dict: dict[str, Any]) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr_list[3]", 20)
|
||||
assert setup_dict["attr_list"]["value"][3]["value"] == 20
|
||||
|
||||
|
||||
def test_update_invalid_list_index(
|
||||
setup_dict, caplog: pytest.LogCaptureFixture
|
||||
setup_dict: dict[str, Any], caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
set_nested_value_by_path(setup_dict, "attr_list[10]", 30)
|
||||
assert (
|
||||
|
Loading…
x
Reference in New Issue
Block a user