81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
"""Tests for YAML converter."""
|
|
|
|
import pytest
|
|
import tempfile
|
|
import os
|
|
import yaml
|
|
|
|
from config_convert.converters import YAMLConverter
|
|
|
|
|
|
class TestYAMLConverter:
|
|
@pytest.fixture
|
|
def converter(self):
|
|
return YAMLConverter()
|
|
|
|
def test_loads_valid_yaml(self, converter):
|
|
data = "name: test\nvalue: 123"
|
|
result = converter.loads(data)
|
|
assert result == {"name": "test", "value": 123}
|
|
|
|
def test_loads_nested_yaml(self, converter):
|
|
data = """database:
|
|
host: localhost
|
|
port: 5432
|
|
"""
|
|
result = converter.loads(data)
|
|
assert result == {"database": {"host": "localhost", "port": 5432}}
|
|
|
|
def test_loads_array_yaml(self, converter):
|
|
data = "tags:\n - a\n - b\n - c"
|
|
result = converter.loads(data)
|
|
assert result == {"tags": ["a", "b", "c"]}
|
|
|
|
def test_loads_multiline_string(self, converter):
|
|
data = """description: |
|
|
This is a
|
|
multiline string
|
|
"""
|
|
result = converter.loads(data)
|
|
assert result["description"] == "This is a\nmultiline string\n"
|
|
|
|
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", "port": 5432}}
|
|
result = converter.dumps(data)
|
|
assert "host: localhost" in result
|
|
|
|
def test_roundtrip(self, converter):
|
|
original = {"name": "test", "version": "1.0.0", "enabled": True, "count": 42}
|
|
dumped = converter.dumps(original)
|
|
loaded = converter.loads(dumped)
|
|
assert loaded == original
|
|
|
|
def test_load_file(self, converter, temp_file, sample_yaml):
|
|
gen = temp_file(sample_yaml, suffix=".yaml")
|
|
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_yaml):
|
|
path = tempfile.mktemp(suffix=".yaml")
|
|
try:
|
|
data = yaml.safe_load(sample_yaml)
|
|
converter.dump(data, path)
|
|
result = converter.load(path)
|
|
assert result == data
|
|
finally:
|
|
if os.path.exists(path):
|
|
os.unlink(path)
|
|
|
|
def test_invalid_yaml_raises(self, converter):
|
|
with pytest.raises(Exception):
|
|
converter.loads("[invalid yaml")
|