Spaces:
Runtime error
Runtime error
| """Unit-тесты для нормализации номеров (без региона).""" | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from app.services.plate_normalizer import ( | |
| latin_to_cyrillic, | |
| normalize_plate, | |
| is_valid_gost, | |
| ) | |
| def test_latin_to_cyrillic(): | |
| assert latin_to_cyrillic("A123BC") == "А123ВС" | |
| assert latin_to_cyrillic("a123bc") == "А123ВС" | |
| assert latin_to_cyrillic("X999YH") == "Х999УН" | |
| def test_valid_plate_passes(): | |
| text, valid = normalize_plate("A123BC") | |
| assert valid is True | |
| assert text == "А123ВС" | |
| def test_region_stripped(): | |
| # Регион должен срезаться | |
| text, valid = normalize_plate("A123BC777") | |
| assert valid is True | |
| assert text == "А123ВС" | |
| def test_region_2digit_stripped(): | |
| text, valid = normalize_plate("A123BC77") | |
| assert valid is True | |
| assert text == "А123ВС" | |
| def test_dirty_ocr_with_garbage_region(): | |
| # C227HAT69 -> срезаем всё после НА | |
| text, valid = normalize_plate("C227HAT69") | |
| assert valid is True | |
| assert text == "С227НА" | |
| def test_padding_removed(): | |
| text, valid = normalize_plate("A123BC_") | |
| assert valid is True | |
| assert text == "А123ВС" | |
| def test_no_region_input(): | |
| # Уже без региона | |
| text, valid = normalize_plate("M222MM") | |
| assert valid is True | |
| assert text == "М222ММ" | |
| def test_invalid_passes_through(): | |
| # OCR прочитал мусор без буквенного формата | |
| text, valid = normalize_plate("222115") | |
| assert valid is False | |
| def test_is_valid_gost(): | |
| assert is_valid_gost("А123ВС") is True # формат буква+3цифры+2буквы | |
| assert is_valid_gost("А123ВС777") is False # с регионом — НЕ валидно (теперь регион не часть формата) | |
| assert is_valid_gost("Я123ВС") is False # Я не в алфавите ГОСТ | |
| assert is_valid_gost("А12ВС") is False # 2 цифры вместо 3 | |
| def test_positional_zero_to_letter(): | |
| # AO13MP97 -> А013МР (O на цифровой позиции -> 0, регион срезан) | |
| text, valid = normalize_plate("AO13MP97") | |
| assert valid is True | |
| assert text == "А013МР" | |
| def test_positional_all_O_confusion(): | |
| # OOO10077 -> О001ОО | |
| text, valid = normalize_plate("OOO10077") | |
| assert valid is True | |
| assert text == "О001ОО" | |
| def test_positional_letter_on_digit(): | |
| # АО0ХММ - О на позиции 2 (цифра) -> 0 | |
| text, valid = normalize_plate("AOOMM") # слишком коротко - не соберётся | |
| assert valid is False | |
| if __name__ == "__main__": | |
| tests = [v for k, v in globals().items() if k.startswith("test_") and callable(v)] | |
| passed = 0 | |
| for t in tests: | |
| try: | |
| t() | |
| print(f"✓ {t.__name__}") | |
| passed += 1 | |
| except AssertionError as e: | |
| print(f"✗ {t.__name__}: {e}") | |
| print(f"\n{passed}/{len(tests)} тестов прошло") |