Тесты: unit+api с фейковым эмбеддером, real-model smoke за RUN_REAL_MODEL

This commit is contained in:
2026-10-07 13:54:40 +03:00
parent 01d54a5e03
commit d448884294
9 changed files with 618 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
"""UT: чистая логика сравнения — пороги, лучший эталон, границы зон."""
import numpy as np
import pytest
from app.dino.comparison import ComparisonResult, cosine_similarities, decide
class TestDecide:
def test_match_on_and_above_threshold(self):
# UT: best >= THRESHOLD_MATCH -> match (граница включительно)
assert decide([0.55], 0.55, 0.40).verdict == "match"
assert decide([0.9], 0.55, 0.40).verdict == "match"
def test_uncertain_zone(self):
# UT: THRESHOLD_NO_MATCH <= best < THRESHOLD_MATCH -> uncertain
assert decide([0.5499], 0.55, 0.40).verdict == "uncertain"
assert decide([0.40], 0.55, 0.40).verdict == "uncertain" # граница снизу включительно
def test_no_match_below_threshold(self):
# UT: best < THRESHOLD_NO_MATCH -> no_match
result = decide([0.3999], 0.55, 0.40)
assert result.verdict == "no_match"
assert result.is_match is False
def test_best_reference_index_across_many(self):
# UT: эталоны с разных ракурсов — вердикт по максимуму
result = decide([0.10, 0.71, 0.30, 0.56], 0.55, 0.40)
assert result.verdict == "match"
assert result.best_reference_index == 1
assert result.best_similarity == pytest.approx(0.71)
assert result.similarities == (0.10, 0.71, 0.30, 0.56)
def test_empty_similarities_rejected(self):
with pytest.raises(ValueError):
decide([], 0.55, 0.40)
def test_is_match_flag_consistency(self):
match = decide([0.99], 0.55, 0.40)
assert match.is_match is True
uncertain = decide([0.45], 0.55, 0.40)
assert uncertain.is_match is False
class TestCosineSimilarities:
def test_normalized_inputs_dot_product(self):
probe = np.array([1.0, 0.0, 0.0], dtype=np.float32)
references = np.array(
[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.6, 0.8, 0.0]], dtype=np.float32
)
sims = cosine_similarities(probe, references)
assert sims[0] == pytest.approx(1.0)
assert sims[1] == pytest.approx(0.0)
assert sims[2] == pytest.approx(0.6)
def test_self_similarity_is_one(self):
vector = np.array([[0.1, -0.2, 0.3, 0.9]], dtype=np.float32)
vector /= np.linalg.norm(vector)
assert cosine_similarities(vector[0], vector)[0] == pytest.approx(1.0, abs=1e-6)
+72
View File
@@ -0,0 +1,72 @@
"""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