Initial upload: ConfigConvert CLI with full test suite and CI/CD
This commit is contained in:
113
tests/test_flatten.py
Normal file
113
tests/test_flatten.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""Tests for flatten/unflatten utilities."""
|
||||
|
||||
import pytest
|
||||
|
||||
from config_convert.utils import flatten_dict, unflatten_dict
|
||||
|
||||
|
||||
class TestFlattenDict:
|
||||
def test_flatten_simple_dict(self):
|
||||
data = {"name": "test", "value": 123}
|
||||
result = flatten_dict(data)
|
||||
assert result == {"name": "test", "value": 123}
|
||||
|
||||
def test_flatten_nested_dict(self):
|
||||
data = {"database": {"host": "localhost", "port": 5432}}
|
||||
result = flatten_dict(data)
|
||||
assert result == {"database.host": "localhost", "database.port": 5432}
|
||||
|
||||
def test_flatten_deeply_nested(self):
|
||||
data = {"a": {"b": {"c": {"d": "deep"}}}}
|
||||
result = flatten_dict(data)
|
||||
assert result == {"a.b.c.d": "deep"}
|
||||
|
||||
def test_flatten_with_list(self):
|
||||
data = {"items": [{"name": "a"}, {"name": "b"}]}
|
||||
result = flatten_dict(data)
|
||||
assert result == {"items[0].name": "a", "items[1].name": "b"}
|
||||
|
||||
def test_flatten_with_custom_sep(self):
|
||||
data = {"database": {"host": "localhost"}}
|
||||
result = flatten_dict(data, sep="_")
|
||||
assert result == {"database_host": "localhost"}
|
||||
|
||||
def test_flatten_mixed_types(self):
|
||||
data = {
|
||||
"name": "test",
|
||||
"count": 42,
|
||||
"enabled": True,
|
||||
"database": {"host": "localhost", "port": 5432},
|
||||
"tags": ["a", "b", "c"]
|
||||
}
|
||||
result = flatten_dict(data)
|
||||
assert "name" in result
|
||||
assert "database.host" in result
|
||||
assert "tags[0]" in result
|
||||
|
||||
|
||||
class TestUnflattenDict:
|
||||
def test_unflatten_simple_dict(self):
|
||||
data = {"name": "test", "value": 123}
|
||||
result = unflatten_dict(data)
|
||||
assert result == data
|
||||
|
||||
def test_unflatten_nested_dict(self):
|
||||
data = {"database.host": "localhost", "database.port": 5432}
|
||||
result = unflatten_dict(data)
|
||||
assert result == {"database": {"host": "localhost", "port": 5432}}
|
||||
|
||||
def test_unflatten_deeply_nested(self):
|
||||
data = {"a.b.c.d": "deep"}
|
||||
result = unflatten_dict(data)
|
||||
assert result == {"a": {"b": {"c": {"d": "deep"}}}}
|
||||
|
||||
def test_unflatten_with_brackets(self):
|
||||
data = {"items[0].name": "a", "items[1].name": "b"}
|
||||
result = unflatten_dict(data)
|
||||
assert result == {"items": [{"name": "a"}, {"name": "b"}]}
|
||||
|
||||
def test_unflatten_with_custom_sep(self):
|
||||
data = {"database_host": "localhost", "database_port": 5432}
|
||||
result = unflatten_dict(data, sep="_")
|
||||
assert result == {"database": {"host": "localhost", "port": 5432}}
|
||||
|
||||
|
||||
class TestFlattenUnflattenRoundtrip:
|
||||
def test_roundtrip_simple(self):
|
||||
original = {"name": "test", "value": 123}
|
||||
flat = flatten_dict(original)
|
||||
result = unflatten_dict(flat)
|
||||
assert result == original
|
||||
|
||||
def test_roundtrip_nested(self):
|
||||
original = {"database": {"host": "localhost", "port": 5432}}
|
||||
flat = flatten_dict(original)
|
||||
result = unflatten_dict(flat)
|
||||
assert result == original
|
||||
|
||||
def test_roundtrip_with_lists(self):
|
||||
original = {"items": [{"name": "a"}, {"name": "b"}]}
|
||||
flat = flatten_dict(original)
|
||||
result = unflatten_dict(flat)
|
||||
assert result == original
|
||||
|
||||
def test_roundtrip_complex(self):
|
||||
original = {
|
||||
"name": "test",
|
||||
"database": {
|
||||
"host": "localhost",
|
||||
"port": 5432,
|
||||
"settings": {"timeout": 30, "retries": 3}
|
||||
},
|
||||
"tags": ["a", "b", "c"]
|
||||
}
|
||||
flat = flatten_dict(original)
|
||||
result = unflatten_dict(flat)
|
||||
assert result == original
|
||||
|
||||
|
||||
class TestKeyConflict:
|
||||
def test_key_conflict_raises(self):
|
||||
data = {"a": "value", "a.b": "conflict"}
|
||||
with pytest.raises(ValueError):
|
||||
unflatten_dict(data)
|
||||
Reference in New Issue
Block a user