153 lines
4.9 KiB
Python
153 lines
4.9 KiB
Python
from __future__ import annotations
|
||
|
||
from app.pipeline.mapping import (
|
||
FIXED_REPORT_COLUMNS,
|
||
_extract_tag_candidate_keys,
|
||
build_fixed_row,
|
||
)
|
||
|
||
|
||
def _column_with(prefix: str) -> str:
|
||
for column in FIXED_REPORT_COLUMNS:
|
||
if column.startswith(prefix):
|
||
return column
|
||
raise AssertionError(f"Колонка с префиксом '{prefix}' не найдена")
|
||
|
||
|
||
def test_build_fixed_row_maps_known_fields() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=2,
|
||
operation_fields={
|
||
"ИдФайл": "FILE_01",
|
||
"КодОперации": "5007",
|
||
"СумОперации": "100.55",
|
||
},
|
||
participant_fields={
|
||
"ИННФЛ": "123456789012",
|
||
"Тип": "01",
|
||
},
|
||
)
|
||
|
||
assert row["Имя XML файла"] == "f.xml"
|
||
assert row["Идентификатор записи"] == "R1"
|
||
assert row["Уникальный номер операции"] == "2"
|
||
assert row["Код вида операции"] == "5007"
|
||
assert row["Сумма в валюте проведения"] == "100.55"
|
||
assert row[_column_with("ИНН ")] == "123456789012"
|
||
assert row["Тип участника"] == "01"
|
||
|
||
|
||
def test_build_fixed_row_keeps_missing_values_empty() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={},
|
||
participant_fields={},
|
||
)
|
||
for column in FIXED_REPORT_COLUMNS:
|
||
assert column in row
|
||
assert row["Код вида операции"] == ""
|
||
assert row[_column_with("ИНН ")] == ""
|
||
|
||
|
||
def test_build_fixed_row_does_not_take_operation_inn_for_participant() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={
|
||
"ИНН": "0000000000",
|
||
"Тип": "99",
|
||
},
|
||
participant_fields={
|
||
"ИННФЛ": "111111111111",
|
||
"Тип": "01",
|
||
},
|
||
)
|
||
assert row[_column_with("ИНН ")] == "111111111111"
|
||
assert row["Тип участника"] == "01"
|
||
|
||
|
||
def test_fixed_report_columns_over_200() -> None:
|
||
assert len(FIXED_REPORT_COLUMNS) == 204
|
||
assert FIXED_REPORT_COLUMNS[126] == "Признак принадлежности к публичным лицам"
|
||
|
||
|
||
def test_build_fixed_row_uses_alternative_path_leaf_for_emitent_name() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={
|
||
"ФИОСтрока": "Эмитент ФЛ",
|
||
},
|
||
participant_fields={},
|
||
)
|
||
assert row["Наименование/ ФИО эмитента ЦП"] == "Эмитент ФЛ"
|
||
|
||
|
||
def test_build_fixed_row_maps_dotted_participant_keys() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={},
|
||
participant_fields={
|
||
"УчастникФЛИП.СведФЛИП.ИННФЛИП": "123456789012",
|
||
"УчастникФЛИП.СведФЛИП.СведДокУдЛичн.НомДок": "451099",
|
||
},
|
||
)
|
||
assert row[_column_with("ИНН ")] == "123456789012"
|
||
assert row["Номер"] == "451099"
|
||
|
||
|
||
def test_build_fixed_row_splits_pipe_separated_xml_tag_values() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={
|
||
"Индекс": "674500",
|
||
},
|
||
participant_fields={},
|
||
)
|
||
assert row["Адрес по структуре (целый)"] == "674500"
|
||
|
||
|
||
def test_extract_tag_candidate_keys_supports_comma_separator() -> None:
|
||
candidates = _extract_tag_candidate_keys(
|
||
"КППЮЛ, ИдентификацияФЛ, ПризнакОргФормаИНБОЮЛ"
|
||
)
|
||
assert candidates == ("КППЮЛ", "ИдентификацияФЛ", "ПризнакОргФормаИНБОЮЛ")
|
||
|
||
|
||
def test_build_fixed_row_uses_first_suffix_value_from_index() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={},
|
||
participant_fields={
|
||
"УчастникФЛИП.Первый.ИННФЛИП": "111111111111",
|
||
"УчастникФЛИП.Второй.ИННФЛИП": "222222222222",
|
||
},
|
||
)
|
||
assert row[_column_with("ИНН ")] == "111111111111"
|
||
|
||
|
||
def test_build_fixed_row_skips_empty_suffix_values_in_index() -> None:
|
||
row = build_fixed_row(
|
||
file_name="f.xml",
|
||
record_id="R1",
|
||
operation_index=1,
|
||
operation_fields={},
|
||
participant_fields={
|
||
"УчастникФЛИП.Первый.ИННФЛИП": "",
|
||
"УчастникФЛИП.Второй.ИННФЛИП": "222222222222",
|
||
},
|
||
)
|
||
assert row[_column_with("ИНН ")] == "222222222222"
|