Files
open_cv/tests/api/test_verify.py
T
2026-09-29 00:17:58 +03:00

146 lines
7.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Интеграционные тесты POST /api/v1/verify — docs/08 §8.3, IT-02..IT-07,
IT-11..IT-13, IT-16."""
from __future__ import annotations
import pytest
from tests.api.helpers import (
make_blob_ref,
make_image_ref,
post_features,
post_verify,
rewrite_blob_meta,
)
class TestVerifyEndpoint:
def test_it02_roundtrip_same_photo(self, client, facade_a, blob_a):
"""IT-02: /features → /verify с той же пробой — match."""
response = post_verify(client, facade_a, [make_blob_ref(blob_a)])
assert response.status_code == 200
body = response.json()
assert body["verdict"] == "match"
assert body["is_match"] is True
assert body["confidence"] >= 0.6
assert body["details"]["best_reference_index"] == 0
assert body["details"]["references_compared"] == 1
assert len(body["probe_sha256"]) == 64
assert body["processing_ms"] >= 0
thresholds = body["details"]["thresholds"]
assert thresholds["inliers_match"] == 50
assert thresholds["inlier_ratio_match"] == pytest.approx(0.30)
assert thresholds["coverage_min"] == pytest.approx(0.05)
assert thresholds["inliers_uncertain_min"] == 20
def test_it03_probe_of_other_building(self, client, facade_b, blob_a):
"""IT-03: проба другого здания — no_match/uncertain, не match."""
response = post_verify(client, facade_b, [make_blob_ref(blob_a)])
assert response.status_code == 200
body = response.json()
assert body["verdict"] in ("no_match", "uncertain")
assert body["is_match"] is False
def test_it04_no_references(self, client, facade_a):
"""IT-04: 0 частей reference — 422 VALIDATION_ERROR."""
response = post_verify(client, facade_a, [])
assert response.status_code == 422
body = response.json()
assert body["error"]["code"] == "VALIDATION_ERROR"
assert body["error"]["details"]["part"] == "reference"
def test_it04_too_many_references(self, client, facade_a):
"""IT-04: 33 части reference — 400 TOO_MANY_REFERENCES.
Части-пустышки: лимит числа эталонов проверяется до разбора контента,
но после ранней CL-проверки тела.
"""
refs = [("reference.bin", b"garbage", "application/octet-stream")] * 33
response = post_verify(client, facade_a, refs)
assert response.status_code == 400
assert response.json()["error"]["code"] == "TOO_MANY_REFERENCES"
def test_it05_unsupported_format_version(self, client, facade_a, blob_a):
"""IT-05: блоб с format_version=99 — 400 FEATURE_FORMAT_UNSUPPORTED,
в details — индекс эталона."""
foreign = rewrite_blob_meta(blob_a, format_version=99)
response = post_verify(client, facade_a, [make_blob_ref(foreign)])
assert response.status_code == 400
body = response.json()
assert body["error"]["code"] == "FEATURE_FORMAT_UNSUPPORTED"
assert body["error"]["details"]["reference_index"] == 0
assert body["error"]["details"]["received"] == 99
def test_it06_corrupted_blob(self, client, facade_a, blob_a):
"""IT-06: повреждённый блоб (обрезаны байты) — 400 FEATURE_BLOB_INVALID."""
response = post_verify(client, facade_a, [make_blob_ref(blob_a[: len(blob_a) // 2])])
assert response.status_code == 400
assert response.json()["error"]["code"] == "FEATURE_BLOB_INVALID"
def test_unrecognized_reference_content(self, client, facade_a):
response = post_verify(
client, facade_a, [("reference.bin", b"\x00\x01\x02garbage", "application/octet-stream")]
)
assert response.status_code == 400
assert response.json()["error"]["code"] == "IMAGE_DECODE_FAILED"
def test_it11_determinism(self, client, facade_a, blob_a):
"""IT-11: два одинаковых /verify — идентичные результаты (NFR-009)."""
first = post_verify(client, facade_a, [make_blob_ref(blob_a)]).json()
second = post_verify(client, facade_a, [make_blob_ref(blob_a)]).json()
assert first["verdict"] == second["verdict"]
assert first["confidence"] == second["confidence"]
assert first["details"]["metrics"] == second["details"]["metrics"]
def test_it12_debug_mode_image_reference(self, client, facade_a):
"""IT-12: эталон-изображение без /features — 200, match (Q-4)."""
response = post_verify(client, facade_a, [make_image_ref(facade_a)])
assert response.status_code == 200
assert response.json()["verdict"] == "match"
def test_it13_mixed_references(self, client, facade_a, facade_b, blob_a):
"""IT-13: смешанные эталоны; best_reference_index сквозной по порядку."""
refs = [make_image_ref(facade_b), make_blob_ref(blob_a), make_image_ref(facade_a)]
response = post_verify(client, facade_a, refs)
assert response.status_code == 200
body = response.json()
assert body["details"]["references_compared"] == 3
assert body["verdict"] == "match"
assert body["details"]["best_reference_index"] == 1
def test_it16_request_override_beats_blob(self, client, facade_a):
"""IT-16: переопределение порога в /verify побеждает значение из блоба."""
blob = post_features(
client, facade_a, settings={"comparison": {"match_inliers_min": 500}}
).content
no_override = post_verify(client, facade_a, [make_blob_ref(blob)]).json()
assert no_override["details"]["thresholds"]["inliers_match"] == 500
overridden = post_verify(
client, facade_a, [make_blob_ref(blob)],
settings={"comparison": {"match_inliers_min": 30}},
).json()
assert overridden["details"]["thresholds"]["inliers_match"] == 30
def test_it16_profile_from_blob(self, client, facade_a):
"""Профиль, сохранённый в блобе, действует при /verify без настроек."""
blob = post_features(client, facade_a, settings={"profile": "monument"}).content
body = post_verify(client, facade_a, [make_blob_ref(blob)]).json()
assert body["details"]["thresholds"]["geometry_model"] == "fundamental"
def test_verify_413_body_limit(self, client, facade_a, facade_b):
"""Суммарный запрос сверх VERIFY_MAX_BODY_MB (8 МБ в тестах) — 413."""
heavy = [make_image_ref(facade_b)] * 32 # 32 × ~0.3 МБ + проба > 8 МБ
response = post_verify(client, facade_a, heavy)
assert response.status_code == 413
assert response.json()["error"]["code"] == "PAYLOAD_TOO_LARGE"
def test_missing_probe_part(self, client, facade_a, blob_a):
response = client.post(
"/api/v1/verify",
headers={"X-API-Key": "test-key"},
files=[("reference", ("reference.npz", blob_a, "application/octet-stream"))],
)
assert response.status_code == 422
assert response.json()["error"]["details"]["part"] == "probe"