From 7f4524b88e0e7e2f4a20df611fa75f6df0532c8e Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 22:26:09 +0000 Subject: [PATCH] Initial upload: config-converter-cli v1.0.0 --- tests/test_validators.py | 128 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/test_validators.py diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 0000000..606ece8 --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,128 @@ +"""Tests for the validator module.""" + +import pytest + +from configconverter.validators import Validator +from configconverter.exceptions import ParseError + + +class TestValidator: + """Tests for the Validator class.""" + + @pytest.fixture + def validator(self): + return Validator() + + def test_valid_json(self, validator): + content = '{"name": "test", "value": 123}' + is_valid, error = validator.validate(content, "json") + assert is_valid is True + assert error is None + + def test_valid_yaml(self, validator): + content = 'name: test\nvalue: 123\n' + is_valid, error = validator.validate(content, "yaml") + assert is_valid is True + assert error is None + + def test_valid_toml(self, validator): + content = 'name = "test"\nvalue = 123\n' + is_valid, error = validator.validate(content, "toml") + assert is_valid is True + assert error is None + + def test_invalid_json(self, validator): + content = '{"name": "test",}' + is_valid, error = validator.validate(content, "json") + assert is_valid is False + assert error is not None + assert isinstance(error, ParseError) + + def test_invalid_yaml(self, validator): + content = "name: test\n bad_indent: value\n" + is_valid, error = validator.validate(content, "yaml") + assert is_valid is False + assert error is not None + + def test_invalid_toml(self, validator): + content = 'name = "test"\nvalue = 123\ninvalid line with spaces at start\n' + is_valid, error = validator.validate(content, "toml") + assert is_valid is False + assert error is not None + + def test_auto_detect_json(self, validator): + content = '{"name": "test"}' + is_valid, error = validator.validate(content) + assert is_valid is True + + def test_auto_detect_yaml(self, validator): + content = 'name: test\n' + is_valid, error = validator.validate(content) + assert is_valid is True + + def test_auto_detect_toml(self, validator): + content = 'name = "test"\n' + is_valid, error = validator.validate(content) + assert is_valid is True + + def test_empty_content(self, validator): + content = "" + try: + is_valid, error = validator.validate(content) + assert is_valid is True + except Exception: + pass + + def test_whitespace_content(self, validator): + content = " \n\t\n " + try: + is_valid, error = validator.validate(content) + assert is_valid is True + except Exception: + pass + + def test_validate_file(self, validator, tmp_path): + file_path = tmp_path / "test.json" + file_path.write_text('{"name": "test"}') + + is_valid, error = validator.validate_file(str(file_path), "json") + assert is_valid is True + + def test_validate_file_not_found(self, validator): + is_valid, error = validator.validate_file("/nonexistent/path.json", "json") + assert is_valid is False + assert error is not None + + def test_complex_json_structure(self, validator): + content = ''' + { + "server": { + "host": "localhost", + "port": 8080, + "ssl": { + "enabled": true, + "cert": "/path/to/cert" + } + }, + "databases": [ + {"name": "primary", "replica": false}, + {"name": "replica", "replica": true} + ] + } + ''' + is_valid, error = validator.validate(content, "json") + assert is_valid is True + + def test_nested_yaml_structure(self, validator): + content = ''' + services: + - name: web + ports: + - 80 + - 443 + - name: api + ports: + - 8080 + ''' + is_valid, error = validator.validate(content, "yaml") + assert is_valid is True