132 lines
3.8 KiB
Python
132 lines
3.8 KiB
Python
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from src.utils.config_manager import config
|
|
|
|
|
|
def test_config_manager_register_and_get():
|
|
config.reset()
|
|
config.register(name="app_name", val="komAI", desc="Application name", cat="app")
|
|
assert config.get("app_name", cat="app") == "komAI"
|
|
assert config.get_description("app_name", cat="app") == "Application name"
|
|
|
|
|
|
def test_config_manager_register_default_category():
|
|
config.reset()
|
|
config.register(name="param1", val="value1")
|
|
assert config.get("param1") == "value1"
|
|
|
|
|
|
def test_config_manager_singleton():
|
|
from src.utils.config_manager import config as config2
|
|
|
|
assert config is config2
|
|
|
|
|
|
def test_config_manager_load_save():
|
|
config.reset()
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "config.yaml"
|
|
config.register(name="app_name", val="TestApp", cat="app")
|
|
config.register(name="debug", val="true", cat="app")
|
|
|
|
config.save(str(config_path))
|
|
assert config_path.exists()
|
|
|
|
config.reset()
|
|
config.register(name="app_name", val="Other", cat="app")
|
|
config.register(name="debug", val="false", cat="app")
|
|
config.load(str(config_path))
|
|
|
|
assert config.get("app_name", cat="app") == "TestApp"
|
|
assert config.get("debug", cat="app") == "true"
|
|
|
|
|
|
def test_config_manager_env_override():
|
|
config.reset()
|
|
env_var = "TEST_APP_CONFIG"
|
|
os.environ[env_var] = "/nonexistent/path.yaml"
|
|
|
|
from src.utils.config_manager.config_manager import ConfigManager
|
|
|
|
test_config = ConfigManager(config_env=env_var)
|
|
test_config.register(name="param1", val="original")
|
|
test_config.load()
|
|
|
|
assert test_config._config_path == Path("/nonexistent/path.yaml")
|
|
del os.environ[env_var]
|
|
|
|
|
|
def test_config_parameter_get_parameter():
|
|
config.reset()
|
|
config.register(name="param1", val="value1", cat="test")
|
|
param = config.get_parameter("param1", cat="test")
|
|
assert param is not None
|
|
assert param.val == "value1"
|
|
assert param.name == "param1"
|
|
assert param.default == "value1"
|
|
|
|
|
|
def test_config_set_value():
|
|
config.reset()
|
|
config.register(name="param1", val="original", cat="test")
|
|
assert config.get("param1", cat="test") == "original"
|
|
|
|
config.set("param1", "modified", cat="test")
|
|
assert config.get("param1", cat="test") == "modified"
|
|
|
|
|
|
def test_config_validator():
|
|
config.reset()
|
|
|
|
def validate_level(old_val, new_val):
|
|
valid_levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
|
|
if new_val not in valid_levels:
|
|
raise ValueError(f"Invalid level: {new_val}")
|
|
|
|
config.register(name="level", val="INFO", cat="test", validator=validate_level)
|
|
assert config.get("level", cat="test") == "INFO"
|
|
|
|
config.set("level", "DEBUG", cat="test")
|
|
assert config.get("level", cat="test") == "DEBUG"
|
|
|
|
try:
|
|
config.set("level", "INVALID", cat="test")
|
|
assert False, "Should have raised ValueError"
|
|
except ValueError:
|
|
assert config.get("level", cat="test") == "DEBUG"
|
|
|
|
|
|
def run_tests():
|
|
tests = [
|
|
test_config_manager_register_and_get,
|
|
test_config_manager_register_default_category,
|
|
test_config_manager_singleton,
|
|
test_config_manager_load_save,
|
|
test_config_manager_env_override,
|
|
test_config_parameter_get_parameter,
|
|
test_config_set_value,
|
|
test_config_validator,
|
|
]
|
|
|
|
passed = 0
|
|
failed = 0
|
|
|
|
for test in tests:
|
|
try:
|
|
test()
|
|
print(f"PASS: {test.__name__}")
|
|
passed += 1
|
|
except Exception as e:
|
|
print(f"FAIL: {test.__name__} - {e}")
|
|
failed += 1
|
|
|
|
print(f"\n{passed}/{passed + failed} tests passed")
|
|
return failed == 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = run_tests()
|
|
exit(0 if success else 1)
|