diff --git a/tests/unit/test_config_merger.py b/tests/unit/test_config_merger.py new file mode 100644 index 0000000..9ea7ace --- /dev/null +++ b/tests/unit/test_config_merger.py @@ -0,0 +1,37 @@ +import pytest +import json +import tempfile +from pathlib import Path +from tools.config_merger import load_config, ConfigValidationError + + +def test_load_config_valid_json(): + """Test loading a valid JSON config file""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + json.dump({"key": "value"}, f) + temp_path = f.name + + try: + result = load_config(temp_path) + assert result == {"key": "value"} + finally: + Path(temp_path).unlink() + + +def test_load_config_file_not_found(): + """Test loading non-existent config file""" + with pytest.raises(ConfigValidationError, match="not found"): + load_config("/nonexistent/path.json") + + +def test_load_config_invalid_json(): + """Test loading malformed JSON""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + f.write("{invalid json") + temp_path = f.name + + try: + with pytest.raises(ConfigValidationError, match="Invalid JSON"): + load_config(temp_path) + finally: + Path(temp_path).unlink() diff --git a/tools/config_merger.py b/tools/config_merger.py new file mode 100644 index 0000000..b79569c --- /dev/null +++ b/tools/config_merger.py @@ -0,0 +1,36 @@ +"""Configuration merging and validation for AI-Trader.""" + +import json +import sys +from pathlib import Path +from typing import Dict, Any, Optional + + +class ConfigValidationError(Exception): + """Raised when config validation fails.""" + pass + + +def load_config(path: str) -> Dict[str, Any]: + """ + Load and parse JSON config file. + + Args: + path: Path to JSON config file + + Returns: + Parsed config dictionary + + Raises: + ConfigValidationError: If file not found or invalid JSON + """ + config_path = Path(path) + + if not config_path.exists(): + raise ConfigValidationError(f"Config file not found: {path}") + + try: + with open(config_path, 'r') as f: + return json.load(f) + except json.JSONDecodeError as e: + raise ConfigValidationError(f"Invalid JSON in {path}: {e}")