From 2540f994787a7ac4dc6cad4b22dc30aabc0a8c75 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 07:05:35 +0000 Subject: [PATCH] Initial upload: ConfigConvert CLI with full test suite and CI/CD --- tests/test_flatten.py | 113 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/test_flatten.py diff --git a/tests/test_flatten.py b/tests/test_flatten.py new file mode 100644 index 0000000..391d3d6 --- /dev/null +++ b/tests/test_flatten.py @@ -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)