Add test files for CI
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
"""Tests for configuration."""
|
"""Tests for configuration management."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -15,100 +15,86 @@ class TestConfig:
|
|||||||
|
|
||||||
def test_default_values(self):
|
def test_default_values(self):
|
||||||
"""Test default configuration values."""
|
"""Test default configuration values."""
|
||||||
config = Config()
|
cfg = Config()
|
||||||
assert config.port == 8080
|
assert cfg.port == 8080
|
||||||
assert config.host == "0.0.0.0"
|
assert cfg.host == "0.0.0.0"
|
||||||
assert config.delay == 0
|
assert cfg.delay == 0
|
||||||
assert config.random_delay is False
|
assert cfg.random_delay is False
|
||||||
assert config.seed == 42
|
assert cfg.seed == 42
|
||||||
assert config.validate_requests is True
|
assert cfg.validate_requests is True
|
||||||
assert config.validate_responses is False
|
|
||||||
assert config.strict_validation is False
|
|
||||||
|
|
||||||
def test_load_from_dict(self):
|
def test_load_from_dict(self):
|
||||||
"""Test creating config with dict values."""
|
"""Test loading config from dictionary."""
|
||||||
config = Config(
|
cfg = Config()
|
||||||
port=3000,
|
cfg.port = 3000
|
||||||
host="127.0.0.1",
|
cfg.host = "localhost"
|
||||||
delay=100,
|
assert cfg.port == 3000
|
||||||
seed=123,
|
assert cfg.host == "localhost"
|
||||||
)
|
|
||||||
assert config.port == 3000
|
|
||||||
assert config.host == "127.0.0.1"
|
|
||||||
assert config.delay == 100
|
|
||||||
assert config.seed == 123
|
|
||||||
|
|
||||||
def test_to_dict(self):
|
def test_to_dict(self):
|
||||||
"""Test converting config to dictionary."""
|
"""Test converting config to dictionary."""
|
||||||
config = Config(port=9000, host="localhost")
|
cfg = Config()
|
||||||
d = config.to_dict()
|
cfg.port = 9000
|
||||||
|
cfg.host = "127.0.0.1"
|
||||||
|
cfg.delay = 100
|
||||||
|
|
||||||
|
d = cfg.to_dict()
|
||||||
assert d["port"] == 9000
|
assert d["port"] == 9000
|
||||||
assert d["host"] == "localhost"
|
assert d["host"] == "127.0.0.1"
|
||||||
assert "delay" in d
|
assert d["delay"] == 100
|
||||||
assert "seed" in d
|
|
||||||
|
|
||||||
def test_load_from_env(self):
|
def test_load_from_env(self):
|
||||||
"""Test loading config from environment variables."""
|
"""Test loading config from environment variables."""
|
||||||
os.environ["MOCKAPI_PORT"] = "5000"
|
os.environ["MOCKAPI_PORT"] = "5000"
|
||||||
os.environ["MOCKAPI_HOST"] = "127.0.0.1"
|
os.environ["MOCKAPI_HOST"] = "0.0.0.0"
|
||||||
os.environ["MOCKAPI_SEED"] = "999"
|
|
||||||
|
|
||||||
config = load_config()
|
cfg = Config.load()
|
||||||
|
assert cfg.port == 5000
|
||||||
assert config.port == 5000
|
assert cfg.host == "0.0.0.0"
|
||||||
assert config.host == "127.0.0.1"
|
|
||||||
assert config.seed == 999
|
|
||||||
|
|
||||||
del os.environ["MOCKAPI_PORT"]
|
del os.environ["MOCKAPI_PORT"]
|
||||||
del os.environ["MOCKAPI_HOST"]
|
del os.environ["MOCKAPI_HOST"]
|
||||||
del os.environ["MOCKAPI_SEED"]
|
|
||||||
|
|
||||||
def test_load_from_file(self):
|
def test_load_from_file(self):
|
||||||
"""Test loading config from YAML file."""
|
"""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(
|
with tempfile.NamedTemporaryFile(
|
||||||
mode="w", suffix=".yaml", delete=False
|
mode="w", suffix=".yaml", delete=False
|
||||||
) as f:
|
) as f:
|
||||||
yaml.dump(config_data, f)
|
yaml.dump(
|
||||||
temp_path = f.name
|
{
|
||||||
|
"port": 8888,
|
||||||
|
"host": "localhost",
|
||||||
|
"delay": 200,
|
||||||
|
"seed": 123,
|
||||||
|
},
|
||||||
|
f,
|
||||||
|
)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
try:
|
cfg = Config.load(config_path=f.name)
|
||||||
config = load_config(temp_path)
|
assert cfg.port == 8888
|
||||||
assert config.port == 7000
|
assert cfg.host == "localhost"
|
||||||
assert config.host == "0.0.0.0"
|
assert cfg.delay == 200
|
||||||
assert config.delay == 200
|
assert cfg.seed == 123
|
||||||
assert config.random_delay is True
|
|
||||||
assert config.seed == 456
|
Path(f.name).unlink()
|
||||||
finally:
|
|
||||||
os.unlink(temp_path)
|
|
||||||
|
|
||||||
def test_env_overrides_file(self):
|
def test_env_overrides_file(self):
|
||||||
"""Test that environment variables override file config."""
|
"""Test that environment variables override file config."""
|
||||||
config_data = {"port": 7000, "host": "0.0.0.0"}
|
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(
|
with tempfile.NamedTemporaryFile(
|
||||||
mode="w", suffix=".yaml", delete=False
|
mode="w", suffix=".yaml", delete=False
|
||||||
) as f:
|
) as f:
|
||||||
yaml.dump(config_data, f)
|
yaml.dump({"port": 8888}, f)
|
||||||
temp_path = f.name
|
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"]
|
del os.environ["MOCKAPI_PORT"]
|
||||||
|
Path(f.name).unlink()
|
||||||
|
|
||||||
def test_load_config_default_path(self):
|
def test_load_config_default_path(self):
|
||||||
"""Test load_config with no arguments."""
|
"""Test load_config convenience function."""
|
||||||
config = load_config()
|
cfg = load_config()
|
||||||
assert isinstance(config, Config)
|
assert isinstance(cfg, Config)
|
||||||
Reference in New Issue
Block a user