"""UT: конфигурация — парсинг ENV, диапазоны и связные проверки (fail-fast).""" import pytest from app.config import Config, ConfigError class TestFromEnv: def test_defaults(self, monkeypatch): env_vars = [ "APP_API_KEY", "HOST", "PORT", "WORKERS", "DINO_MODEL_NAME", "DINO_DEVICE", "DINO_IMAGE_SIZE", "TORCH_HOME", "MAX_IMAGE_MB", "MIN_IMAGE_DIM", "MAX_REFERENCES_PER_REQUEST", "THRESHOLD_MATCH", "THRESHOLD_NO_MATCH", "LOG_LEVEL", ] for name in env_vars: monkeypatch.delenv(name, raising=False) config = Config.from_env() assert config.model_name == "dinov2_vits14" assert config.device == "cpu" assert config.image_size == 224 assert config.max_image_bytes == 10 * 1024 * 1024 assert config.max_references == 8 assert config.threshold_match == 0.55 assert config.threshold_no_match == 0.40 assert config.api_key is None def test_override(self, monkeypatch): monkeypatch.setenv("THRESHOLD_MATCH", "0.7") monkeypatch.setenv("MAX_REFERENCES_PER_REQUEST", "3") config = Config.from_env() assert config.threshold_match == 0.7 assert config.max_references == 3 def test_empty_string_means_default(self, monkeypatch): monkeypatch.setenv("APP_API_KEY", "") monkeypatch.delenv("DINO_MODEL_NAME", raising=False) assert Config.from_env().api_key is None class TestValidation: def test_image_size_must_be_multiple_of_patch(self, monkeypatch): monkeypatch.setenv("DINO_IMAGE_SIZE", "223") with pytest.raises(ConfigError, match="кратно 14"): Config.from_env() def test_threshold_order(self, monkeypatch): monkeypatch.setenv("THRESHOLD_MATCH", "0.4") monkeypatch.setenv("THRESHOLD_NO_MATCH", "0.5") with pytest.raises(ConfigError, match="THRESHOLD_NO_MATCH"): Config.from_env() def test_equal_thresholds_rejected(self, monkeypatch): monkeypatch.setenv("THRESHOLD_MATCH", "0.5") monkeypatch.setenv("THRESHOLD_NO_MATCH", "0.5") with pytest.raises(ConfigError): Config.from_env() def test_non_numeric_rejected(self, monkeypatch): monkeypatch.setenv("PORT", "http") with pytest.raises(ConfigError, match="PORT"): Config.from_env() def test_range_enforced(self, monkeypatch): monkeypatch.setenv("MAX_IMAGE_MB", "1000") with pytest.raises(ConfigError, match="MAX_IMAGE_MB"): Config.from_env() def test_max_body_bytes_scales_with_limits(self): from conftest import make_config small = make_config(max_image_bytes=1024, max_references=2) assert small.max_body_bytes == 3 * 1024 + 64 * 1024