init app
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
"""Юнит-тесты резолвера настроек и валидации — docs/08 §8.2, UT-16, UT-18."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from app.cv.exceptions import InvalidSettings, UnknownProfile
|
||||
from app.cv.params import (
|
||||
RequestSettings,
|
||||
parse_settings,
|
||||
validate_stored_settings,
|
||||
)
|
||||
from app.cv.resolver import (
|
||||
effective_comparison,
|
||||
effective_extraction,
|
||||
effective_probe_extraction,
|
||||
)
|
||||
from tests.conftest import make_config
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def config():
|
||||
return make_config()
|
||||
|
||||
|
||||
class TestResolver:
|
||||
def test_ut16_comparison_priority(self, config):
|
||||
"""UT-16: env → профиль → блоб → запрос (docs/04 §4.5.4)."""
|
||||
blob = RequestSettings(
|
||||
profile="monument", extraction={}, comparison={"lowe_ratio": 0.70}
|
||||
)
|
||||
request = RequestSettings(profile=None, extraction={}, comparison={})
|
||||
|
||||
# env-умолчания
|
||||
base = effective_comparison(config, None, None)
|
||||
assert base.lowe_ratio == config.lowe_ratio
|
||||
assert base.geometry_model == "homography"
|
||||
|
||||
# профиль из блоба подключается, если запрос профиль не задаёт
|
||||
with_blob_profile = effective_comparison(config, request, blob)
|
||||
assert with_blob_profile.geometry_model == "fundamental" # из monument
|
||||
assert with_blob_profile.lowe_ratio == 0.70 # из блоба
|
||||
assert with_blob_profile.match_inliers_min == 40 # из monument
|
||||
|
||||
# профиль запроса сильнее профиля блоба, переопределения запроса —
|
||||
# сильнее всего
|
||||
req = RequestSettings(
|
||||
profile="facade",
|
||||
extraction={},
|
||||
comparison={"match_inliers_min": 70, "lowe_ratio": 0.68},
|
||||
)
|
||||
top = effective_comparison(config, req, blob)
|
||||
assert top.geometry_model == "homography" # facade из запроса победил monument
|
||||
assert top.match_inliers_min == 70
|
||||
assert top.lowe_ratio == 0.68
|
||||
|
||||
def test_ut16_extraction_priority(self, config):
|
||||
"""UT-16: извлечение — env → профиль → переопределения запроса."""
|
||||
req = RequestSettings(
|
||||
profile="monument",
|
||||
extraction={"max_image_size": 1000},
|
||||
comparison={},
|
||||
)
|
||||
params = effective_extraction(config, req)
|
||||
assert params.max_image_size == 1000 # запрос
|
||||
assert params.clahe_clip_limit == 3.0 # профиль monument
|
||||
assert params.max_keypoints == config.sift_max_keypoints # env
|
||||
|
||||
# проба в /verify: extraction-override запроса не действует (FR-002)
|
||||
probe = effective_probe_extraction(config, req)
|
||||
assert probe.max_image_size == config.sift_max_image_size
|
||||
assert probe.clahe_clip_limit == 3.0 # профиль действует
|
||||
|
||||
def test_cross_field_validation(self, config):
|
||||
"""uncertain_inliers_min > match_inliers_min в эффективных значениях — 422."""
|
||||
req = RequestSettings(profile=None, extraction={}, comparison={"match_inliers_min": 15})
|
||||
with pytest.raises(InvalidSettings):
|
||||
effective_comparison(config, req, None)
|
||||
|
||||
|
||||
class TestSettingsValidation:
|
||||
def test_ut18_unknown_profile(self, config):
|
||||
with pytest.raises(UnknownProfile):
|
||||
parse_settings({"profile": "skyscraper"}, config)
|
||||
|
||||
def test_ut18_unknown_key(self, config):
|
||||
with pytest.raises(InvalidSettings, match="(?i)неизвестн") as exc:
|
||||
parse_settings({"comparison": {"ratio_lowe": 0.7}}, config)
|
||||
assert exc.value.details.get("key") == "ratio_lowe"
|
||||
|
||||
def test_ut18_out_of_range(self, config):
|
||||
with pytest.raises(InvalidSettings) as exc:
|
||||
parse_settings({"comparison": {"lowe_ratio": 0.05}}, config)
|
||||
assert exc.value.details["key"] == "lowe_ratio"
|
||||
|
||||
def test_ut18_unknown_top_level_key(self, config):
|
||||
with pytest.raises(InvalidSettings):
|
||||
parse_settings({"comparson": {}}, config)
|
||||
|
||||
def test_valid_settings_pass(self, config):
|
||||
parsed = parse_settings(
|
||||
{
|
||||
"profile": "monument",
|
||||
"extraction": {"clahe_clip_limit": 3.0, "clahe_tile_grid": [8, 8]},
|
||||
"comparison": {"geometry_model": "fundamental", "lowe_ratio": 0.72},
|
||||
},
|
||||
config,
|
||||
)
|
||||
assert parsed.profile == "monument"
|
||||
assert parsed.comparison["geometry_model"] == "fundamental"
|
||||
|
||||
def test_max_keypoints_capped_by_env(self, config):
|
||||
with pytest.raises(InvalidSettings, match="потолка|ceil"):
|
||||
parse_settings({"extraction": {"max_keypoints": 60_000}}, config)
|
||||
|
||||
def test_stored_settings_invalid_becomes_blob_invalid(self, config):
|
||||
from app.cv.exceptions import BlobInvalid
|
||||
|
||||
with pytest.raises(BlobInvalid):
|
||||
validate_stored_settings({"profile": "no-such-profile"}, config)
|
||||
Reference in New Issue
Block a user