Add test suite for confgen
This commit is contained in:
208
app/tests/test_parsers.py
Normal file
208
app/tests/test_parsers.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""Tests for config parsers."""
|
||||
|
||||
import pytest
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from src.confgen.parsers import ConfigParser
|
||||
|
||||
|
||||
class TestConfigParser:
|
||||
"""Tests for ConfigParser."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.parser = ConfigParser()
|
||||
|
||||
def test_parse_json(self):
|
||||
"""Test parsing JSON content."""
|
||||
content = '{"name": "myapp", "port": 8080}'
|
||||
result = self.parser.parse_json(content)
|
||||
|
||||
assert result["name"] == "myapp"
|
||||
assert result["port"] == 8080
|
||||
|
||||
def test_parse_yaml(self):
|
||||
"""Test parsing YAML content."""
|
||||
content = """
|
||||
name: myapp
|
||||
port: 8080
|
||||
features:
|
||||
- feature1
|
||||
- feature2
|
||||
"""
|
||||
result = self.parser.parse_yaml(content)
|
||||
|
||||
assert result["name"] == "myapp"
|
||||
assert result["port"] == 8080
|
||||
assert "feature1" in result["features"]
|
||||
|
||||
def test_parse_toml(self):
|
||||
"""Test parsing TOML content."""
|
||||
content = """
|
||||
name = "myapp"
|
||||
port = 8080
|
||||
|
||||
[features]
|
||||
feature1 = true
|
||||
"""
|
||||
result = self.parser.parse_toml(content)
|
||||
|
||||
assert result["name"] == "myapp"
|
||||
assert result["port"] == 8080
|
||||
assert result["features"]["feature1"] is True
|
||||
|
||||
def test_parse_auto_detect_json(self):
|
||||
"""Test auto-detecting JSON format."""
|
||||
content = '{"name": "myapp"}'
|
||||
result = self.parser.parse(content)
|
||||
|
||||
assert isinstance(result, dict)
|
||||
assert result["name"] == "myapp"
|
||||
|
||||
def test_parse_auto_detect_yaml(self):
|
||||
"""Test auto-detecting YAML format."""
|
||||
content = "name: myapp"
|
||||
result = self.parser.parse(content)
|
||||
|
||||
assert isinstance(result, dict)
|
||||
assert result["name"] == "myapp"
|
||||
|
||||
def test_to_json(self):
|
||||
"""Test converting to JSON."""
|
||||
data = {"name": "myapp", "port": 8080}
|
||||
result = self.parser.to_json(data)
|
||||
|
||||
assert "myapp" in result
|
||||
assert "8080" in result
|
||||
|
||||
def test_to_yaml(self):
|
||||
"""Test converting to YAML."""
|
||||
data = {"name": "myapp", "port": 8080}
|
||||
result = self.parser.to_yaml(data)
|
||||
|
||||
assert "myapp" in result
|
||||
assert "8080" in result
|
||||
|
||||
def test_to_toml(self):
|
||||
"""Test converting to TOML."""
|
||||
data = {"name": "myapp", "port": 8080}
|
||||
result = self.parser.to_toml(data)
|
||||
|
||||
assert "myapp" in result
|
||||
assert "8080" in result
|
||||
|
||||
def test_detect_format_from_path_json(self):
|
||||
"""Test detecting format from JSON file path."""
|
||||
result = self.parser.detect_format_from_path("config.json")
|
||||
assert result == "json"
|
||||
|
||||
def test_detect_format_from_path_yaml(self):
|
||||
"""Test detecting format from YAML file path."""
|
||||
result = self.parser.detect_format_from_path("config.yaml")
|
||||
assert result == "yaml"
|
||||
|
||||
def test_detect_format_from_path_yml(self):
|
||||
"""Test detecting format from YML file path."""
|
||||
result = self.parser.detect_format_from_path("config.yml")
|
||||
assert result == "yaml"
|
||||
|
||||
def test_detect_format_from_path_toml(self):
|
||||
"""Test detecting format from TOML file path."""
|
||||
result = self.parser.detect_format_from_path("config.toml")
|
||||
assert result == "toml"
|
||||
|
||||
def test_load_file_json(self):
|
||||
"""Test loading a JSON file."""
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".json", delete=False
|
||||
) as f:
|
||||
json.dump({"name": "myapp", "port": 8080}, f)
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
result = self.parser.load_file(temp_path)
|
||||
|
||||
assert result["name"] == "myapp"
|
||||
assert result["port"] == 8080
|
||||
finally:
|
||||
Path(temp_path).unlink()
|
||||
|
||||
def test_load_file_yaml(self):
|
||||
"""Test loading a YAML file."""
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".yaml", delete=False
|
||||
) as f:
|
||||
f.write("name: myapp\nport: 8080")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
result = self.parser.load_file(temp_path)
|
||||
|
||||
assert result["name"] == "myapp"
|
||||
assert result["port"] == 8080
|
||||
finally:
|
||||
Path(temp_path).unlink()
|
||||
|
||||
def test_save_file(self):
|
||||
"""Test saving a config file."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
data = {"name": "myapp", "port": 8080}
|
||||
self.parser.save_file(f"{tmpdir}/config.yaml", data)
|
||||
|
||||
assert Path(f"{tmpdir}/config.yaml").exists()
|
||||
|
||||
with open(f"{tmpdir}/config.yaml") as f:
|
||||
content = f.read()
|
||||
|
||||
assert "myapp" in content
|
||||
assert "8080" in content
|
||||
|
||||
def test_parse_invalid_json(self):
|
||||
"""Test that invalid JSON raises an error."""
|
||||
content = '{"name": "myapp",'
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid JSON"):
|
||||
self.parser.parse_json(content)
|
||||
|
||||
def test_parse_invalid_yaml(self):
|
||||
"""Test that invalid YAML raises an error."""
|
||||
content = "name: [invalid yaml"
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid YAML"):
|
||||
self.parser.parse_yaml(content)
|
||||
|
||||
def test_parse_nested_dict(self):
|
||||
"""Test parsing nested dictionaries."""
|
||||
content = """
|
||||
database:
|
||||
host: localhost
|
||||
port: 5432
|
||||
settings:
|
||||
ssl: true
|
||||
timeout: 30
|
||||
"""
|
||||
result = self.parser.parse_yaml(content)
|
||||
|
||||
assert result["database"]["host"] == "localhost"
|
||||
assert result["database"]["port"] == 5432
|
||||
assert result["database"]["settings"]["ssl"] is True
|
||||
|
||||
def test_parse_with_list(self):
|
||||
"""Test parsing configurations with lists."""
|
||||
content = """
|
||||
services:
|
||||
- name: web
|
||||
port: 80
|
||||
- name: api
|
||||
port: 8080
|
||||
features:
|
||||
- feature1
|
||||
- feature2
|
||||
"""
|
||||
result = self.parser.parse_yaml(content)
|
||||
|
||||
assert len(result["services"]) == 2
|
||||
assert result["services"][0]["name"] == "web"
|
||||
assert len(result["features"]) == 2
|
||||
Reference in New Issue
Block a user