Skip to content

Commit b736fc7

Browse files
committed
fix: restore validation and add tests for PathData.from_dict
- Add back input validation for data type and required keys - Restore error logging for invalid input - Add debug logging for value creation - Add tests for validation and error handling: - Test invalid data type - Test missing required keys - Test value path mismatch This ensures proper error handling and logging when creating PathData instances from dictionaries, with full test coverage.
1 parent f0d389b commit b736fc7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

helm_values_manager/models/path_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,27 @@ def from_dict(
126126
127127
Returns:
128128
PathData: New PathData instance
129+
130+
Raises:
131+
ValueError: If the dictionary structure is invalid
129132
"""
133+
if not isinstance(data, dict):
134+
logger.error("Invalid data type provided: %s", type(data))
135+
raise ValueError("Data must be a dictionary")
136+
137+
logger.debug("Creating PathData from dict with path: %s", data.get("path"))
138+
139+
required_keys = {"path", "metadata", "values"}
140+
if not all(key in data for key in required_keys):
141+
missing = required_keys - set(data.keys())
142+
logger.error("Missing required keys in data: %s", missing)
143+
raise ValueError(f"Missing required keys: {missing}")
144+
130145
path_data = cls(path=data["path"], metadata=data["metadata"])
131146

132147
# Create Value instances for each environment
133148
for env, value_data in data.get("values", {}).items():
149+
logger.debug("Creating value for environment %s", env)
134150
value = create_value_fn(data["path"], env, value_data)
135151
path_data.set_value(env, value)
136152

tests/unit/models/test_path_data.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,33 @@ def create_value_fn(path, env, value_data):
156156
}
157157
assert len(list(path_data.get_environments())) == 1
158158
assert "test_env" in path_data._values
159+
160+
161+
def test_from_dict_invalid_type():
162+
"""Test from_dict with invalid data type."""
163+
with pytest.raises(ValueError, match="Data must be a dictionary"):
164+
PathData.from_dict(["not a dict"], lambda p, e, d: None)
165+
166+
167+
def test_from_dict_missing_keys():
168+
"""Test from_dict with missing required keys."""
169+
data = {"path": "test.path"} # Missing metadata and values
170+
with pytest.raises(ValueError, match="Missing required keys: {'metadata', 'values'}"):
171+
PathData.from_dict(data, lambda p, e, d: None)
172+
173+
174+
def test_from_dict_value_path_mismatch():
175+
"""Test from_dict when create_value_fn returns value with wrong path."""
176+
data = {
177+
"path": "test.path",
178+
"metadata": {"required": True},
179+
"values": {"test_env": {"value": "test"}},
180+
}
181+
182+
def create_value_fn(path, env, value_data):
183+
mock_value = Mock(spec=Value)
184+
mock_value.path = "wrong.path" # Mismatched path
185+
return mock_value
186+
187+
with pytest.raises(ValueError, match=r"Value path wrong\.path doesn't match PathData path test\.path"):
188+
PathData.from_dict(data, create_value_fn)

0 commit comments

Comments
 (0)