File size: 3,135 Bytes
186b436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""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)} тестов прошло")