diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index eb4a82d..c33fa4a 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -1,4 +1,4 @@ -"""Tests for configuration.""" +"""Tests for configuration management.""" import os import tempfile @@ -15,100 +15,86 @@ class TestConfig: def test_default_values(self): """Test default configuration values.""" - config = Config() - assert config.port == 8080 - assert config.host == "0.0.0.0" - assert config.delay == 0 - assert config.random_delay is False - assert config.seed == 42 - assert config.validate_requests is True - assert config.validate_responses is False - assert config.strict_validation is False + cfg = Config() + assert cfg.port == 8080 + assert cfg.host == "0.0.0.0" + assert cfg.delay == 0 + assert cfg.random_delay is False + assert cfg.seed == 42 + assert cfg.validate_requests is True def test_load_from_dict(self): - """Test creating config with dict values.""" - config = Config( - port=3000, - host="127.0.0.1", - delay=100, - seed=123, - ) - assert config.port == 3000 - assert config.host == "127.0.0.1" - assert config.delay == 100 - assert config.seed == 123 + """Test loading config from dictionary.""" + cfg = Config() + cfg.port = 3000 + cfg.host = "localhost" + assert cfg.port == 3000 + assert cfg.host == "localhost" def test_to_dict(self): """Test converting config to dictionary.""" - config = Config(port=9000, host="localhost") - d = config.to_dict() + cfg = Config() + cfg.port = 9000 + cfg.host = "127.0.0.1" + cfg.delay = 100 + + d = cfg.to_dict() assert d["port"] == 9000 - assert d["host"] == "localhost" - assert "delay" in d - assert "seed" in d + assert d["host"] == "127.0.0.1" + assert d["delay"] == 100 def test_load_from_env(self): """Test loading config from environment variables.""" os.environ["MOCKAPI_PORT"] = "5000" - os.environ["MOCKAPI_HOST"] = "127.0.0.1" - os.environ["MOCKAPI_SEED"] = "999" + os.environ["MOCKAPI_HOST"] = "0.0.0.0" - config = load_config() - - assert config.port == 5000 - assert config.host == "127.0.0.1" - assert config.seed == 999 + cfg = Config.load() + assert cfg.port == 5000 + assert cfg.host == "0.0.0.0" del os.environ["MOCKAPI_PORT"] del os.environ["MOCKAPI_HOST"] - del os.environ["MOCKAPI_SEED"] def test_load_from_file(self): """Test loading config from YAML file.""" - config_data = { - "port": 7000, - "host": "0.0.0.0", - "delay": 200, - "random_delay": True, - "seed": 456, - } - with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: - yaml.dump(config_data, f) - temp_path = f.name + yaml.dump( + { + "port": 8888, + "host": "localhost", + "delay": 200, + "seed": 123, + }, + f, + ) + f.flush() - try: - config = load_config(temp_path) - assert config.port == 7000 - assert config.host == "0.0.0.0" - assert config.delay == 200 - assert config.random_delay is True - assert config.seed == 456 - finally: - os.unlink(temp_path) + cfg = Config.load(config_path=f.name) + assert cfg.port == 8888 + assert cfg.host == "localhost" + assert cfg.delay == 200 + assert cfg.seed == 123 + + Path(f.name).unlink() def test_env_overrides_file(self): """Test that environment variables override file config.""" - config_data = {"port": 7000, "host": "0.0.0.0"} - with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: - yaml.dump(config_data, f) - temp_path = f.name + yaml.dump({"port": 8888}, f) + f.flush() - os.environ["MOCKAPI_PORT"] = "9000" + os.environ["MOCKAPI_PORT"] = "9999" + cfg = Config.load(config_path=f.name) + assert cfg.port == 9999 - try: - config = load_config(temp_path) - assert config.port == 9000 - finally: - os.unlink(temp_path) del os.environ["MOCKAPI_PORT"] + Path(f.name).unlink() def test_load_config_default_path(self): - """Test load_config with no arguments.""" - config = load_config() - assert isinstance(config, Config) + """Test load_config convenience function.""" + cfg = load_config() + assert isinstance(cfg, Config) \ No newline at end of file