mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-10-29 09:11:57 +00:00
fix(config): report computed fields as read-only on update (#1454)
PUT /v1/config/{path} answered "does not exist in model" when the
target was a computed field such as eos_device_instruction_entity_ids.
Raise an explicit read-only error instead, so API clients can tell
read-only fields from unknown ones.
Closes #913
This commit is contained in:
@@ -486,6 +486,18 @@ def test_set_nested_key(path, value, expected, exception, config_eos):
|
||||
f"Expected exception {exception}, but got {type(e)}: {e}"
|
||||
)
|
||||
|
||||
def test_set_nested_key_read_only_computed_field(config_eos):
|
||||
"""Setting a computed field is reported as read-only, not as missing."""
|
||||
path = "adapter/homeassistant/eos_device_instruction_entity_ids"
|
||||
with pytest.raises(TypeError, match="read-only computed field"):
|
||||
config_eos.set_nested_value(path, ["sensor.eos_battery1"])
|
||||
|
||||
|
||||
def test_set_nested_key_unknown_field_still_reports_missing(config_eos):
|
||||
"""A field that does not exist keeps the original 'does not exist' message."""
|
||||
with pytest.raises(TypeError, match="does not exist in model"):
|
||||
config_eos.set_nested_value("adapter/homeassistant/no_such_field", 1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path, expected_value, exception",
|
||||
|
||||
+15
-1
@@ -3,7 +3,7 @@ from typing import Any, Optional
|
||||
import pandas as pd
|
||||
import pendulum
|
||||
import pytest
|
||||
from pydantic import Field, ValidationError
|
||||
from pydantic import BaseModel, Field, ValidationError, computed_field
|
||||
|
||||
from akkudoktoreos.core.pydantic import (
|
||||
PydanticBaseModel,
|
||||
@@ -199,6 +199,20 @@ class TestPydanticModelNestedValueMixin:
|
||||
with pytest.raises(TypeError):
|
||||
PydanticModelNestedValueMixin._get_key_types(User, "unknown_field")
|
||||
|
||||
def test_get_key_types_for_computed_field(self):
|
||||
"""Test _get_key_types reports a computed field as read-only."""
|
||||
|
||||
class WithComputed(BaseModel):
|
||||
value: int = 1
|
||||
|
||||
@computed_field # type: ignore[prop-decorator]
|
||||
@property
|
||||
def doubled(self) -> int:
|
||||
return self.value * 2
|
||||
|
||||
with pytest.raises(TypeError, match="read-only computed field"):
|
||||
PydanticModelNestedValueMixin._get_key_types(WithComputed, "doubled")
|
||||
|
||||
def test_get_key_types_for_instance_raises(self, user_instance):
|
||||
"""Test _get_key_types raises an error for an instance."""
|
||||
with pytest.raises(TypeError):
|
||||
|
||||
Reference in New Issue
Block a user