Files
mockapi/tests/unit/test_config.py
7000pctAUTO ff73520767
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled
Add test files for CI
2026-03-22 21:24:20 +00:00

100 lines
2.7 KiB
Python

"""Tests for configuration management."""
import os
import tempfile
from pathlib import Path
import pytest
import yaml
from mockapi.core.config import Config, load_config
class TestConfig:
"""Test cases for Config."""
def test_default_values(self):
"""Test default configuration values."""
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 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."""
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"] == "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"] = "0.0.0.0"
cfg = Config.load()
assert cfg.port == 5000
assert cfg.host == "0.0.0.0"
del os.environ["MOCKAPI_PORT"]
del os.environ["MOCKAPI_HOST"]
def test_load_from_file(self):
"""Test loading config from YAML file."""
with tempfile.NamedTemporaryFile(
mode="w", suffix=".yaml", delete=False
) as f:
yaml.dump(
{
"port": 8888,
"host": "localhost",
"delay": 200,
"seed": 123,
},
f,
)
f.flush()
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."""
with tempfile.NamedTemporaryFile(
mode="w", suffix=".yaml", delete=False
) as f:
yaml.dump({"port": 8888}, f)
f.flush()
os.environ["MOCKAPI_PORT"] = "9999"
cfg = Config.load(config_path=f.name)
assert cfg.port == 9999
del os.environ["MOCKAPI_PORT"]
Path(f.name).unlink()
def test_load_config_default_path(self):
"""Test load_config convenience function."""
cfg = load_config()
assert isinstance(cfg, Config)