Files
mostovik-backend/tests/apps/parsers/test_checko_sections.py
2026-10-05 17:49:22 +02:00

203 lines
7.8 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.
"""Verified Checko DTOs, complete pagination and fail-closed section collection."""
import importlib
import json
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from apps.parsers.clients.checko import CheckoClient
from apps.parsers.registry_snapshots import SnapshotValidationError
FIXTURES = Path(__file__).parents[2] / "fixtures/checko_sections"
ORG = SimpleNamespace(inn="7707083893", ogrn="1027700132195", name="АО Проверка")
def fixture(name):
return json.loads((FIXTURES / name).read_text())
def client_with(*responses):
client = CheckoClient(api_key="synthetic-unit-test")
client._http_client.get_json = Mock(side_effect=responses)
return client
def collect(client, source="checko_messages"):
try:
module = importlib.import_module("apps.parsers.checko_sections")
except ModuleNotFoundError:
pytest.fail("collect_checko_section is not implemented")
return list(module.collect_checko_section(client, organization=ORG, source=source))
def test_license_identity_ignores_mutable_expiry_and_keeps_current_aliases():
first = collect(client_with(fixture("license.json")), "checko_licenses")[0]
later = collect(client_with(fixture("renewed.json")), "checko_licenses")[0]
assert first.external_id == later.external_id
assert first.record_type == "company_license"
assert first.payload["authority"] == "Синтетический лицензирующий орган"
assert first.payload["activities"] == ["Синтетический вид деятельности"]
assert first.payload["valid_until"] is None
assert later.payload["valid_until"] == "2030-01-01"
assert first.record_date == first.payload["issued_on"] == "2020-01-02"
assert first.amount is None
def test_all_pages_collected_and_messages_not_bankruptcy():
client = client_with(
fixture("messages-page-1.json"), fixture("messages-page-2.json")
)
rows = collect(client)
assert len(rows) == 2
assert {row.record_type for row in rows} == {"company_message"}
assert rows[0].payload["message_type"] == "FinancialLeaseContract2"
assert rows[1].payload["message_type"] == "Liquidation"
assert rows[0].payload["participant_identifiers"] == [ORG.inn]
for index, call in enumerate(client._http_client.get_json.call_args_list, 1):
assert call.args[0] == "/fedresurs"
assert call.kwargs["params"]["page"] == str(index)
assert call.kwargs["params"]["limit"] == "100"
assert call.kwargs["params"]["role"] == "all"
assert all(row.amount is None and "amount" not in row.payload for row in rows)
assert "full_text" not in rows[0].payload
def test_leasing_filters_verified_types_after_full_result():
client = client_with(
fixture("messages-page-1.json"), fixture("messages-page-2.json")
)
rows = collect(client, "checko_leasing")
assert len(rows) == 1
assert rows[0].record_type == "leasing_contract"
assert rows[0].payload["lease_event_type"] == "FinancialLeaseContract2"
assert client._http_client.get_json.call_count == 2
@pytest.mark.parametrize(
"source,name",
[
("checko_messages", "messages-empty.json"),
("checko_leasing", "messages-empty.json"),
("checko_licenses", "license-empty.json"),
],
)
def test_genuine_empty(source, name):
assert collect(client_with(fixture(name)), source) == []
@pytest.mark.parametrize("name", ["denied.json", "rate-limit.json"])
@pytest.mark.parametrize(
"source", ["checko_messages", "checko_leasing", "checko_licenses"]
)
def test_denied_is_not_verified_empty(name, source):
with pytest.raises(SnapshotValidationError):
collect(client_with(fixture(name)), source)
def test_repeated_cursor_is_not_complete():
with pytest.raises(SnapshotValidationError):
collect(
client_with(
fixture("messages-page-1.json"), fixture("messages-page-1.json")
)
)
def test_conflicting_provider_identity_is_rejected():
first, second = fixture("messages-page-1.json"), fixture("messages-page-2.json")
second["data"]["Записи"][0]["GUID"] = first["data"]["Записи"][0]["GUID"]
with pytest.raises(SnapshotValidationError):
collect(client_with(first, second))
@pytest.mark.parametrize(
"corruption", ["guid", "identity", "count", "pages", "envelope", "participants"]
)
def test_incomplete_or_unknown_message_schema_rejected(corruption):
response = fixture("messages-page-1.json")
if corruption == "guid":
response["data"]["Записи"][0].pop("GUID")
elif corruption == "identity":
response["company"]["ИНН"] = "0000000000"
elif corruption == "count":
response["data"]["ЗапВсего"] = 3
elif corruption == "pages":
response["data"]["СтрВсего"] = 101
elif corruption == "envelope":
response["data"].pop("Записи")
else:
response["data"]["Записи"][0]["Участники"] = "bad"
with pytest.raises(SnapshotValidationError):
collect(client_with(response, fixture("messages-page-2.json")))
@pytest.mark.parametrize(
"corruption", ["section", "number", "authority", "issued", "identity"]
)
def test_missing_license_section_or_composite_identity_rejected(corruption):
response = fixture("license.json")
if corruption == "section":
response["data"].pop("Лиценз")
elif corruption == "identity":
response["data"]["ОГРН"] = "0000000000000"
else:
response["data"]["Лиценз"][0].pop(
{"number": "Номер", "authority": "ЛицОрг", "issued": "Дата"}[corruption]
)
with pytest.raises(SnapshotValidationError):
collect(client_with(response), "checko_licenses")
def test_unknown_section_does_not_call_upstream():
client = client_with()
with pytest.raises(SnapshotValidationError):
collect(client, "bankruptcy")
client._http_client.get_json.assert_not_called()
def test_repeated_page_content_with_advanced_number_rejected():
first = fixture("messages-page-1.json")
second = fixture("messages-page-1.json")
second["data"]["СтрТекущ"] = 2
with pytest.raises(SnapshotValidationError):
collect(client_with(first, second))
@pytest.mark.parametrize("source", ["checko_messages", "checko_leasing"])
def test_overlapping_pages_cannot_prove_total_before_leasing_filter(source):
from copy import deepcopy
first, second = fixture("messages-page-1.json"), fixture("messages-page-2.json")
shared = deepcopy(first["data"]["Записи"][0])
first["data"]["Записи"].append(dict(shared, GUID="other-first"))
second["data"]["Записи"].insert(0, shared)
for response in (first, second):
response["data"]["ЗапВсего"] = 4
with pytest.raises(SnapshotValidationError) as error:
collect(client_with(first, second), source)
assert str(error.value.__cause__) == "incomplete_checko_record_count"
def test_license_unknown_raw_fields_survive():
response = fixture("license.json")
response["data"]["Лиценз"][0]["FutureField"] = {"nested": [None, 0, False]}
row = collect(client_with(response), "checko_licenses")[0]
assert row.payload["upstream"]["futurefield"] == {"nested": [None, 0, False]}
def test_bad_license_activities_rejected():
response = fixture("license.json")
response["data"]["Лиценз"][0]["ВидДеят"] = "AB"
with pytest.raises(SnapshotValidationError):
collect(client_with(response), "checko_licenses")
@pytest.mark.parametrize("value", [None, "bad", ["bad"]])
def test_malformed_license_section_rejected(value):
response = fixture("license.json")
response["data"]["Лиценз"] = value
with pytest.raises(SnapshotValidationError):
collect(client_with(response), "checko_licenses")