arm-ka-fixtures/synthgen/tests/test_dictionary.py
Andrey Epifancev b2a89394c9 synthgen: словарь компаний, состав группы, выгрузка ЕЦП (#38)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-12 23:39:15 +04:00

73 lines
2.4 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.

import re
from pathlib import Path
import openpyxl
import pytest
from synthgen import dictionary, inn
from synthgen.dictionary import build
REAL_ECP = Path("/Users/andrey.epifancev/projects/awp-ca/fixtures/directories/ecp-export-russkiy-biskvit.xlsx")
REAL_OSV_DIR = Path("/Users/andrey.epifancev/projects/awp-ca/fixtures/osv/rb/2024-Q1")
_FORMS = re.compile(r"\b(ООО|АО|ПАО|ЗАО|ОАО|ИП)\b", re.IGNORECASE)
def _norm(text: str) -> str:
text = _FORMS.sub(" ", text)
return re.sub(r"[^0-9a-zа-яё]+", " ", text.lower().replace("ё", "е")).strip()
def test_dictionary_size_and_kinds():
orgs = dictionary.load()
assert len(orgs) == 400
assert sum(o.kind == "банк" for o in orgs) == 30
assert 30 <= sum(o.form == "ИП" for o in orgs) <= 50
names = [o.name.lower() for o in orgs]
assert len(set(names)) == len(names)
def test_dictionary_inn():
for o in dictionary.load():
assert inn.is_valid(o.inn)
if o.form == "ИП":
assert len(o.inn) == 12 and o.kpp == ""
number = int(o.inn[2:-2])
else:
assert len(o.inn) == 10
assert re.fullmatch(r"\d{9}", o.kpp)
number = int(o.inn[2:-1])
assert 100001 <= number <= 109999
def _real_names() -> set[str]:
names: set[str] = set()
wb = openpyxl.load_workbook(REAL_ECP, read_only=True)
for ws in wb.worksheets:
rows = ws.iter_rows(values_only=True)
header = next(rows, None)
if not header or "Организация" not in header:
continue
col = list(header).index("Организация")
names.update(_norm(str(r[col])) for r in rows if r[col])
for path in sorted(REAL_OSV_DIR.glob("*.xlsx")):
wb = openpyxl.load_workbook(path, read_only=True)
ws = wb.worksheets[0]
names.update(_norm(str(r[0])) for r in ws.iter_rows(max_col=1, values_only=True) if r and r[0])
names.discard("")
return names
def test_dictionary_not_real():
if not REAL_ECP.exists() or not REAL_OSV_DIR.is_dir():
pytest.skip("нет реальных фикстур")
real = _real_names()
assert len(real) > 50
clash = [o.name for o in dictionary.load() if _norm(o.name) in real]
assert clash == []
def test_build_is_reproducible(tmp_path):
out = tmp_path / "companies.csv"
build.write_csv(out)
assert out.read_bytes() == dictionary.CSV_PATH.read_bytes()