132 lines
6.2 KiB
Python
132 lines
6.2 KiB
Python
"""Интеграционные тесты POST /api/v1/features — docs/08 §8.3, IT-01, IT-07,
|
||
IT-10, IT-14, IT-15."""
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import json
|
||
|
||
import numpy as np
|
||
import pytest
|
||
|
||
from tests.api.helpers import post_features
|
||
|
||
|
||
class TestFeaturesEndpoint:
|
||
def test_it01_valid_photo(self, client, facade_a):
|
||
"""IT-01: 200, контрактные заголовки, тело читается numpy.load."""
|
||
response = post_features(client, facade_a)
|
||
assert response.status_code == 200
|
||
assert response.headers["content-type"] == "application/octet-stream"
|
||
assert response.headers["x-feature-format-version"] == "1"
|
||
assert response.headers["x-algo-id"].startswith("sift-v1:")
|
||
kp_count = int(response.headers["x-keypoints-count"])
|
||
assert kp_count > 0
|
||
assert int(response.headers["x-image-width"]) == 1400
|
||
assert int(response.headers["x-image-height"]) == 900
|
||
|
||
npz = np.load(io.BytesIO(response.content), allow_pickle=False)
|
||
assert npz["descriptors"].shape == (kp_count, 128)
|
||
meta = json.loads(str(npz["meta"].item()))
|
||
assert meta["format_version"] == 1
|
||
assert meta["keypoints_count"] == kp_count
|
||
|
||
def test_it10_idempotency(self, client, facade_a):
|
||
"""IT-10: два вызова с тем же файлом — эквивалентные массивы (FR-007)."""
|
||
first = post_features(client, facade_a)
|
||
second = post_features(client, facade_a)
|
||
assert first.status_code == second.status_code == 200
|
||
|
||
n1 = np.load(io.BytesIO(first.content), allow_pickle=False)
|
||
n2 = np.load(io.BytesIO(second.content), allow_pickle=False)
|
||
assert np.array_equal(n1["descriptors"], n2["descriptors"])
|
||
assert np.array_equal(n1["keypoints"], n2["keypoints"])
|
||
m1 = json.loads(str(n1["meta"].item()))
|
||
m2 = json.loads(str(n2["meta"].item()))
|
||
m1.pop("created_at"), m2.pop("created_at") # различие метки времени допустимо
|
||
assert m1 == m2
|
||
|
||
def test_it14_settings_change_algo_id(self, client, facade_a):
|
||
"""IT-14: профиль monument + переопределение меняют X-Algo-Id;
|
||
meta.settings записываются в блоб."""
|
||
plain = post_features(client, facade_a)
|
||
tuned = post_features(
|
||
client, facade_a,
|
||
settings={"profile": "monument", "extraction": {"clahe_clip_limit": 4.0}},
|
||
)
|
||
assert plain.status_code == tuned.status_code == 200
|
||
assert tuned.headers["x-algo-id"] != plain.headers["x-algo-id"]
|
||
|
||
npz = np.load(io.BytesIO(tuned.content), allow_pickle=False)
|
||
meta = json.loads(str(npz["meta"].item()))
|
||
assert meta["settings"] == {
|
||
"profile": "monument",
|
||
"extraction": {"clahe_clip_limit": 4.0},
|
||
}
|
||
|
||
def test_it15_unknown_profile(self, client, facade_a):
|
||
response = post_features(client, facade_a, settings={"profile": "castle"})
|
||
assert response.status_code == 400
|
||
assert response.json()["error"]["code"] == "UNKNOWN_PROFILE"
|
||
|
||
def test_it15_unknown_settings_key(self, client, facade_a):
|
||
response = post_features(client, facade_a, settings={"comparison": {"threshold": 5}})
|
||
assert response.status_code == 422
|
||
assert response.json()["error"]["code"] == "INVALID_SETTINGS"
|
||
|
||
def test_it15_value_out_of_range(self, client, facade_a):
|
||
response = post_features(client, facade_a, settings={"comparison": {"lowe_ratio": 2.0}})
|
||
assert response.status_code == 422
|
||
assert response.json()["error"]["code"] == "INVALID_SETTINGS"
|
||
|
||
def test_it07_invalid_images(self, client, tiny_image):
|
||
"""IT-07: пустой файл, текст .jpg, 300×200 — 4xx, процесс жив."""
|
||
empty = post_features(client, b"")
|
||
assert empty.status_code == 400
|
||
assert empty.json()["error"]["code"] == "IMAGE_DECODE_FAILED"
|
||
|
||
text = post_features(client, b"this is definitely not an image")
|
||
assert text.status_code == 400
|
||
assert text.json()["error"]["code"] == "IMAGE_DECODE_FAILED"
|
||
|
||
small = post_features(client, tiny_image)
|
||
assert small.status_code == 400
|
||
assert small.json()["error"]["code"] == "IMAGE_TOO_SMALL"
|
||
|
||
# процесс жив после некорректного ввода (NFR-004)
|
||
assert post_features(client, tiny_image).status_code == 400
|
||
assert client.get("/health").status_code == 200
|
||
|
||
def test_it07_oversized_image_413(self, client):
|
||
"""IT-07: изображение больше MAX_IMAGE_MB — 413 (CL проверяется рано)."""
|
||
big = b"\xff\xd8\xff\xe0" + b"\x00" * (2 * 1024 * 1024) # > 1 МБ тестового лимита
|
||
response = post_features(client, big)
|
||
assert response.status_code == 413
|
||
assert response.json()["error"]["code"] == "PAYLOAD_TOO_LARGE"
|
||
|
||
def test_415_unsupported_media_type(self, client):
|
||
"""415: часть image заявлена как image/gif (05 §5.3)."""
|
||
response = client.post(
|
||
"/api/v1/features",
|
||
headers={"X-API-Key": "test-key"},
|
||
files={"image": ("image.gif", b"GIF89a....", "image/gif")},
|
||
)
|
||
assert response.status_code == 415
|
||
assert response.json()["error"]["code"] == "UNSUPPORTED_MEDIA_TYPE"
|
||
|
||
def test_missing_image_part(self, client, facade_a):
|
||
response = client.post(
|
||
"/api/v1/features",
|
||
headers={"X-API-Key": "test-key"},
|
||
files={"photo": ("image.jpg", facade_a, "image/jpeg")},
|
||
)
|
||
assert response.status_code == 422
|
||
assert response.json()["error"]["code"] == "VALIDATION_ERROR"
|
||
assert response.json()["error"]["details"]["part"] == "image"
|
||
|
||
def test_exif_orientation_headers(self, client, exif_image):
|
||
"""EXIF-ориентация применяется сервером: 1400×900 с orientation 6 → 900×1400."""
|
||
response = post_features(client, exif_image)
|
||
assert response.status_code == 200
|
||
assert int(response.headers["x-image-width"]) == 900
|
||
assert int(response.headers["x-image-height"]) == 1400
|