60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
"""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)
|