Merge pull request #1077 from element-hq/gaelg/migration-nested-tests

Nested values: Add tests and verify behaviour with lists
This commit is contained in:
Gaël Goinvic authored and GitHub committed 2026-02-26 14:33:03 +01:00
commit 044b67d4e4
3 files changed
+152 -16

No files matched your search

+1
View File
@@ -0,0 +1 @@
CI: Verify nested values behaviour in migration scripts.
@@ -0,0 +1,114 @@
# Copyright 2026 Element Creations Ltd
#
# SPDX-License-Identifier: AGPL-3.0-only
import pytest
from ..utils import get_nested_value, remove_nested_value, set_nested_value
@pytest.fixture
def sample_config():
"""Fixture to provide a fresh config dictionary for each test."""
return {"a": {"b": {"c": 42, "d": [10, 20, 30]}, "e": "hello"}, "f": [{"x": 1}, {"x": 2}, {"x": 3}]}
def test_get_nested_value(sample_config):
# Test direct key access
assert get_nested_value(sample_config, "a") == {"b": {"c": 42, "d": [10, 20, 30]}, "e": "hello"}
assert get_nested_value(sample_config, "f") == [{"x": 1}, {"x": 2}, {"x": 3}]
# Test nested dictionary access
assert get_nested_value(sample_config, "a.b.c") == 42
assert get_nested_value(sample_config, "a.e") == "hello"
# Test list access
assert get_nested_value(sample_config, "a.b.d.1") == 20
assert get_nested_value(sample_config, "a.b.d.-1") == 30
assert get_nested_value(sample_config, "f.1.x") == 2
# Test invalid paths
assert get_nested_value(sample_config, "a.b.x") is None
assert get_nested_value(sample_config, "a.b.d.5") is None
assert get_nested_value(sample_config, "a.b.d.x") is None
assert get_nested_value(sample_config, "x") is None
assert get_nested_value(sample_config, "a.x.y") is None
# Test edge cases
assert get_nested_value({}, "a") is None
assert get_nested_value(sample_config, "") is None
assert get_nested_value(sample_config, "a.b.d.4") is None # Invalid list index
def test_set_nested_value_direct_key(sample_config):
set_nested_value(sample_config, "a", {"new": "value"})
assert sample_config["a"] == {"new": "value"}
def test_set_nested_value_nested_dict(sample_config):
set_nested_value(sample_config, "a.b.c", 100)
assert sample_config["a"]["b"]["c"] == 100
def test_set_nested_value_list(sample_config):
set_nested_value(sample_config, "a.b.d.1", 200)
assert sample_config["a"]["b"]["d"][1] == 200
def test_set_nested_value_new_nested_keys(sample_config):
set_nested_value(sample_config, "a.new.nested.key", "created")
assert sample_config["a"]["new"]["nested"]["key"] == "created"
def test_set_nested_value_list_index(sample_config):
set_nested_value(sample_config, "f.1.x", 42)
assert sample_config["f"][1]["x"] == 42
def test_set_nested_value_empty_config():
config = {}
set_nested_value(config, "a.b.c", 1)
assert config == {"a": {"b": {"c": 1}}}
def test_remove_nested_value_direct_key(sample_config):
remove_nested_value(sample_config, "a")
assert sample_config == {"f": [{"x": 1}, {"x": 2}, {"x": 3}]}
def test_remove_nested_value_nested_dict(sample_config):
remove_nested_value(sample_config, "a.b.c")
assert sample_config == {"a": {"b": {"d": [10, 20, 30]}, "e": "hello"}, "f": [{"x": 1}, {"x": 2}, {"x": 3}]}
def test_remove_nested_value_list_item(sample_config):
remove_nested_value(sample_config, "a.b.d.1")
assert sample_config == {"a": {"b": {"c": 42, "d": [10, 30]}, "e": "hello"}, "f": [{"x": 1}, {"x": 2}, {"x": 3}]}
def test_remove_nested_value_list_dict(sample_config):
remove_nested_value(sample_config, "f.1")
assert sample_config == {"a": {"b": {"c": 42, "d": [10, 20, 30]}, "e": "hello"}, "f": [{"x": 1}, {"x": 3}]}
def test_remove_nested_value_nonexistent_path(sample_config):
remove_nested_value(sample_config, "a.b.x")
assert sample_config == {
"a": {"b": {"c": 42, "d": [10, 20, 30]}, "e": "hello"},
"f": [{"x": 1}, {"x": 2}, {"x": 3}],
}
def test_remove_nested_value_empty_path(sample_config):
remove_nested_value(sample_config, "")
assert sample_config == {
"a": {"b": {"c": 42, "d": [10, 20, 30]}, "e": "hello"},
"f": [{"x": 1}, {"x": 2}, {"x": 3}],
}
def test_remove_nested_value_empty_config():
config = {}
remove_nested_value(config, "a.b.c")
assert config == {}
+37 -16
View File
@@ -57,16 +57,24 @@ def set_nested_value(config: dict[str, Any], path: str, value: Any) -> None:
# Navigate/create the path
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
if not isinstance(current, dict):
if isinstance(current, dict):
if part not in current:
current[part] = {}
current = current[part]
elif isinstance(current, list):
if int(part) > len(current):
# Create missing list items
current.extend([None] * (int(part) - len(current) + 1))
current = current[int(part)]
else:
# Can't navigate further, overwrite with dict
current = {}
config[parts[0]] = current
# Set the final value
current[parts[-1]] = value
if isinstance(current, list):
current[int(parts[-1])] = value
else:
current[parts[-1]] = value
else:
# Direct key
config[path] = value
@@ -90,10 +98,16 @@ def get_nested_value(config: dict[str, Any], path: str) -> Any:
# Navigate to the value
for part in parts:
if part not in current:
return None
current = current[part]
if not isinstance(current, dict) and part != parts[-1]:
if isinstance(current, dict):
if part not in current:
return None
current = current[part]
elif isinstance(current, list):
try:
current = current[int(part)]
except (IndexError, ValueError):
return None
elif part != parts[-1]:
return None # Can't navigate further
return current
@@ -117,16 +131,23 @@ def remove_nested_value(config: dict[str, Any], path: str) -> None:
# Navigate to the parent
for part in parts[:-1]:
if part not in current:
return # Path doesn't exist
current = current[part]
if not isinstance(current, dict):
return # Can't navigate further
if isinstance(current, dict):
if part not in current:
return
current = current[part]
elif isinstance(current, list):
try:
current = current[int(part)]
except (IndexError, ValueError):
return
# Remove the final key
final_key = parts[-1]
if final_key in current:
if isinstance(current, dict) and final_key in current:
del current[final_key]
elif isinstance(current, list) and int(final_key) < len(current):
del current[int(final_key)]
else:
# Direct key
if path in config: