gendesign/tradein-mvp/backend/tests/test_repair_state_normalizer.py
Light1YT dfd608d6f2 refactor(tradein): нормализовать repair_state → enum на ингесте (#621)
listings.repair_state хранил сырые значения парсеров (cosmetic/euro/
designer/required/without/…), не совместимые с ожидаемым estimator-ом
enum needs_repair/standard/good/excellent. Покрытие ~2%, но значения
не совпадали с enum — repair_coef всегда возвращал 1.0 (no-op).

Решение: централизованный helper repair_state_normalizer.py со всеми
10 известными raw-значениями → 4 enum-buckets. Все ingest-пути
нормализуют на входе; миграция 073 бэкфилит существующие строки.
2026-05-28 20:12:35 +05:00

71 lines
3.2 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.

"""Unit-тесты для repair_state_normalizer.normalize_repair_state() (#621).
Покрывают все известные raw-значения → enum mapping,
idempotency для уже нормализованных значений и None-guard.
"""
import logging
import pytest
from app.services.scrapers.repair_state_normalizer import normalize_repair_state
# ── needs_repair ─────────────────────────────────────────────────────────────
@pytest.mark.parametrize("raw", ["required", "without", "no", "rough"])
def test_needs_repair_values(raw: str) -> None:
assert normalize_repair_state(raw) == "needs_repair"
# ── standard ─────────────────────────────────────────────────────────────────
def test_cosmetic_maps_to_standard() -> None:
assert normalize_repair_state("cosmetic") == "standard"
# ── good ─────────────────────────────────────────────────────────────────────
@pytest.mark.parametrize("raw", ["euro", "fine"])
def test_good_values(raw: str) -> None:
assert normalize_repair_state(raw) == "good"
# ── excellent ────────────────────────────────────────────────────────────────
@pytest.mark.parametrize("raw", ["designer", "design"])
def test_excellent_values(raw: str) -> None:
assert normalize_repair_state(raw) == "excellent"
# ── None guard ───────────────────────────────────────────────────────────────
def test_none_returns_none() -> None:
assert normalize_repair_state(None) is None
# ── idempotency ──────────────────────────────────────────────────────────────
@pytest.mark.parametrize("enum_val", ["needs_repair", "standard", "good", "excellent"])
def test_already_normalized_is_passthrough(enum_val: str) -> None:
"""Уже нормализованное значение возвращается as-is (idempotent при ре-обработке)."""
assert normalize_repair_state(enum_val) == enum_val
# ── unknown value ────────────────────────────────────────────────────────────
def test_unknown_raw_returns_none(caplog: pytest.LogCaptureFixture) -> None:
"""Нераспознанное значение → None + WARNING в лог."""
with caplog.at_level(
logging.WARNING,
logger="app.services.scrapers.repair_state_normalizer",
):
result = normalize_repair_state("whitebox")
assert result is None
assert "whitebox" in caplog.text