diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..f683265 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,135 @@ +"""Tests for configuration module.""" + +import os +import tempfile +from pathlib import Path + +import pytest +import yaml + +from devtoolbelt.config import Config, get_config + + +class TestConfig: + """Tests for Config class.""" + + @pytest.fixture + def temp_config_file(self): + """Create a temporary config file.""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.yml', delete=False) as f: + config_data = { + 'databases': { + 'testdb': { + 'type': 'postgresql', + 'host': 'localhost', + 'port': 5432, + 'database': 'test' + } + }, + 'api_endpoints': { + 'testapi': 'https://api.example.com' + }, + 'default_database': 'testdb' + } + yaml.dump(config_data, f) + temp_path = f.name + + yield temp_path + + if os.path.exists(temp_path): + os.unlink(temp_path) + + def test_load_config(self, temp_config_file): + """Test loading configuration from file.""" + config = Config(Path(temp_config_file)) + loaded = config.load() + + assert 'databases' in loaded + assert 'testdb' in loaded['databases'] + assert loaded['databases']['testdb']['type'] == 'postgresql' + + def test_get_value(self, temp_config_file): + """Test getting configuration values.""" + config = Config(Path(temp_config_file)) + config.load() + + assert config.get('databases.testdb.type') == 'postgresql' + assert config.get('databases.testdb.host') == 'localhost' + assert config.get('databases.nonexistent', 'default') == 'default' + + def test_set_value(self, temp_config_file): + """Test setting configuration values.""" + config = Config(Path(temp_config_file)) + config.load() + + config.set('databases.newdb.type', 'mysql') + config.set('new_key.nested.value', 'test') + + assert config.get('databases.newdb.type') == 'mysql' + assert config.get('new_key.nested.value') == 'test' + + def test_get_database_configs(self, temp_config_file): + """Test getting database configurations.""" + config = Config(Path(temp_config_file)) + config.load() + + databases = config.get_database_configs() + assert 'testdb' in databases + assert databases['testdb']['type'] == 'postgresql' + + def test_get_api_endpoints(self, temp_config_file): + """Test getting API endpoints.""" + config = Config(Path(temp_config_file)) + config.load() + + endpoints = config.get_api_endpoints() + assert 'testapi' in endpoints + assert endpoints['testapi'] == 'https://api.example.com' + + def test_get_default_db(self, temp_config_file): + """Test getting default database.""" + config = Config(Path(temp_config_file)) + config.load() + + assert config.get_default_db() == 'testdb' + + def test_save_config(self, temp_config_file): + """Test saving configuration.""" + config = Config(Path(temp_config_file)) + config.load() + + config.set('databases.another.type', 'sqlite') + config.save() + + config2 = Config(Path(temp_config_file)) + config2.load() + assert config2.get('databases.another.type') == 'sqlite' + + def test_nonexistent_file(self): + """Test handling of nonexistent config file.""" + config = Config(Path('/nonexistent/path/config.yml')) + loaded = config.load() + assert loaded == {} + + def test_get_config_helper(self, temp_config_file): + """Test get_config helper function.""" + config = get_config(temp_config_file) + assert config.get('databases.testdb.type') == 'postgresql' + + +class TestConfigCreation: + """Tests for config file creation.""" + + @pytest.fixture + def new_config_path(self): + """Path for a new config file.""" + with tempfile.TemporaryDirectory() as tmpdir: + yield Path(tmpdir) / 'new_config.yml' + + def test_save_creates_directory(self, new_config_path): + """Test that save creates parent directory.""" + config = Config(new_config_path) + config.set('test.key', 'value') + config.save() + + assert new_config_path.exists()