diff --git a/tests/test_converters.py b/tests/test_converters.py new file mode 100644 index 0000000..a1cf1b7 --- /dev/null +++ b/tests/test_converters.py @@ -0,0 +1,116 @@ +"""Tests for the converter module.""" + +import json +import pytest + +from configconverter.converters import Converter +from configconverter.exceptions import ParseError, InvalidFormatError, UnsupportedConversionError + + +class TestConverter: + """Tests for the Converter class.""" + + @pytest.fixture + def converter(self): + return Converter() + + def test_detect_json_format(self, converter): + content = '{"name": "test", "value": 123}' + assert converter.detect_format(content) == "json" + + def test_detect_json_array(self, converter): + content = '[{"id": 1}, {"id": 2}]' + assert converter.detect_format(content) == "json" + + def test_detect_yaml_format(self, converter): + content = 'name: test\nvalue: 123\n' + assert converter.detect_format(content) == "yaml" + + def test_detect_toml_format(self, converter): + content = 'name = "test"\nvalue = 123\n' + result = converter.detect_format(content) + assert result in ("toml", "yaml") + + def test_convert_json_to_yaml(self, converter): + json_content = '{"name": "test", "value": 123}' + result = converter.convert(json_content, "json", "yaml") + assert "name: test" in result + assert "value: 123" in result + + def test_convert_json_to_toml(self, converter): + json_content = '{"name": "test", "value": 123}' + result = converter.convert(json_content, "json", "toml") + assert 'name = "test"' in result + assert "value = 123" in result + + def test_convert_yaml_to_json(self, converter): + yaml_content = 'name: test\nvalue: 123\n' + result = converter.convert(yaml_content, "yaml", "json") + data = json.loads(result) + assert data["name"] == "test" + assert data["value"] == 123 + + def test_convert_yaml_to_toml(self, converter): + yaml_content = 'name: test\nvalue: 123\n' + result = converter.convert(yaml_content, "yaml", "toml") + assert 'name = "test"' in result + + def test_convert_toml_to_json(self, converter): + toml_content = 'name = "test"\nvalue = 123\n' + result = converter.convert(toml_content, "toml", "json") + data = json.loads(result) + assert data["name"] == "test" + assert data["value"] == 123 + + def test_convert_toml_to_yaml(self, converter): + toml_content = 'name = "test"\nvalue = 123\n' + result = converter.convert(toml_content, "toml", "yaml") + assert "name: test" in result + + def test_invalid_json_syntax(self, converter): + content = '{"name": "test",}' + with pytest.raises(ParseError): + converter.convert(content, "json", "yaml") + + def test_invalid_yaml_syntax(self, converter): + content = "name: test\n value: 123\n" + with pytest.raises(ParseError): + converter.convert(content, "yaml", "json") + + def test_invalid_toml_syntax(self, converter): + content = 'name = "test"\nvalue = 123\ninvalid line here\n' + with pytest.raises(ParseError): + converter.convert(content, "toml", "json") + + def test_unsupported_format(self, converter): + content = "some random text that is not valid" + try: + converter.detect_format(content) + except (InvalidFormatError, Exception): + pass + + def test_unsupported_conversion_direction(self, converter): + content = '{"name": "test"}' + with pytest.raises(UnsupportedConversionError): + converter.convert(content, "json", "xml") + + def test_nested_structure_json_to_yaml(self, converter): + json_content = json.dumps({ + "server": { + "host": "localhost", + "port": 8080 + }, + "databases": [ + {"name": "db1", "enabled": True}, + {"name": "db2", "enabled": False} + ] + }) + result = converter.convert(json_content, "json", "yaml") + assert "server:" in result + assert "host: localhost" in result + assert "databases:" in result + + def test_case_insensitive_format(self, converter): + json_content = '{"name": "test"}' + result = converter.convert(json_content, "JSON", "YAML") + assert "name: test" in result