Initial upload: ConfigConvert CLI with full test suite and CI/CD
This commit is contained in:
83
tests/test_converters/test_toml.py
Normal file
83
tests/test_converters/test_toml.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""Tests for TOML converter."""
|
||||
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from config_convert.converters import TOMLConverter
|
||||
|
||||
|
||||
class TestTOMLConverter:
|
||||
@pytest.fixture
|
||||
def converter(self):
|
||||
return TOMLConverter()
|
||||
|
||||
def test_loads_valid_toml(self, converter):
|
||||
data = 'name = "test"\nvalue = 123'
|
||||
result = converter.loads(data)
|
||||
assert result == {"name": "test", "value": 123}
|
||||
|
||||
def test_loads_nested_toml(self, converter):
|
||||
data = """[database]
|
||||
host = "localhost"
|
||||
port = 5432
|
||||
"""
|
||||
result = converter.loads(data)
|
||||
assert result == {"database": {"host": "localhost", "port": 5432}}
|
||||
|
||||
def test_loads_array_toml(self, converter):
|
||||
data = 'tags = ["a", "b", "c"]'
|
||||
result = converter.loads(data)
|
||||
assert result == {"tags": ["a", "b", "c"]}
|
||||
|
||||
def test_loads_boolean_toml(self, converter):
|
||||
data = 'enabled = true\ndisabled = false'
|
||||
result = converter.loads(data)
|
||||
assert result == {"enabled": True, "disabled": False}
|
||||
|
||||
def test_dumps_basic_dict(self, converter):
|
||||
data = {"name": "test", "value": 123}
|
||||
result = converter.dumps(data)
|
||||
assert 'name = "test"' in result
|
||||
|
||||
def test_dumps_nested_dict(self, converter):
|
||||
data = {"database": {"host": "localhost"}}
|
||||
result = converter.dumps(data)
|
||||
assert "[database]" in result
|
||||
|
||||
def test_roundtrip(self, converter):
|
||||
original = {"name": "test", "version": "1.0.0", "enabled": True}
|
||||
dumped = converter.dumps(original)
|
||||
loaded = converter.loads(dumped)
|
||||
assert loaded == original
|
||||
|
||||
def test_load_file(self, converter, temp_file, sample_toml):
|
||||
gen = temp_file(sample_toml, suffix=".toml")
|
||||
path = next(gen)
|
||||
try:
|
||||
result = converter.load(path)
|
||||
assert result["name"] == "test-project"
|
||||
finally:
|
||||
os.unlink(path)
|
||||
|
||||
def test_dump_file(self, converter, temp_dir, sample_toml):
|
||||
import shutil
|
||||
path = tempfile.mktemp(suffix=".toml")
|
||||
try:
|
||||
if sys.version_info >= (3, 11):
|
||||
import tomllib
|
||||
else:
|
||||
import tomli as tomllib
|
||||
data = tomllib.loads(sample_toml)
|
||||
converter.dump(data, path)
|
||||
result = converter.load(path)
|
||||
assert result == data
|
||||
finally:
|
||||
if Path(path).exists():
|
||||
os.unlink(path)
|
||||
|
||||
def test_invalid_toml_raises(self, converter):
|
||||
with pytest.raises(Exception):
|
||||
converter.loads('invalid = "toml" extra')
|
||||
Reference in New Issue
Block a user