feat: expose FSSP source APIs and exports
This commit is contained in:
@@ -36,7 +36,7 @@ def test_discriminated_records_keep_named_ordering_and_old_operations(contract):
|
||||
schemas = contract["components"]["schemas"]
|
||||
for name in ("OrganizationSourceRecord", "OrganizationSourceRecordList"):
|
||||
record = schemas[name]
|
||||
assert len(record["oneOf"]) == 6
|
||||
assert len(record["oneOf"]) == 7
|
||||
assert record["discriminator"]["propertyName"] == "source_group"
|
||||
for group, record_type in (
|
||||
("budget_process_registry", "budget_registry_organization"),
|
||||
@@ -175,3 +175,21 @@ def test_invalid_or_absent_detail_is_typed_error(registry_query, uid, expected):
|
||||
"invalid_uid",
|
||||
"source_record_not_found",
|
||||
}
|
||||
|
||||
|
||||
def test_fssp_variant_preserves_signed_nullable_amounts(contract):
|
||||
schemas = contract["components"]["schemas"]
|
||||
for kind in ("List", "Detail"):
|
||||
name = schemas[
|
||||
"OrganizationSourceRecord"
|
||||
if kind == "Detail"
|
||||
else "OrganizationSourceRecordList"
|
||||
]["discriminator"]["mapping"]["enforcement_proceedings"].rsplit("/", 1)[1]
|
||||
variant = schemas[name]
|
||||
assert variant["properties"]["amount"]["nullable"] is True
|
||||
assert variant["properties"]["amount"]["pattern"] == r"^-?[0-9]+\.[0-9]{2}$"
|
||||
assert variant["properties"]["status"]["enum"] == [
|
||||
"unknown",
|
||||
"ended",
|
||||
"terminated",
|
||||
]
|
||||
|
||||
190
tests/apps/organizations/test_fssp_contract.py
Normal file
190
tests/apps/organizations/test_fssp_contract.py
Normal file
@@ -0,0 +1,190 @@
|
||||
"""Public FSSP records match the read-only frontend contract."""
|
||||
|
||||
from datetime import date
|
||||
from io import BytesIO
|
||||
from tempfile import TemporaryDirectory
|
||||
from uuid import UUID
|
||||
|
||||
from apps.parsers.fssp_csv import ENDED_DATASET, FSSP_SOURCE, GENERAL_DATASET
|
||||
from apps.parsers.fssp_snapshot import FsspInput, import_fssp_snapshots
|
||||
from apps.parsers.models import ParserLoadLog
|
||||
from apps.parsers.snapshot_metadata import get_snapshot_artifact
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
from organizations.models import Organization
|
||||
from organizations.source_record_export import _source_group_queryset
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from tests.apps.parsers.test_fssp_snapshot import csv_bytes
|
||||
from tests.apps.user.factories import UserFactory
|
||||
|
||||
|
||||
class FsspContractTests(TestCase):
|
||||
def setUp(self):
|
||||
temporary = TemporaryDirectory()
|
||||
self.addCleanup(temporary.cleanup)
|
||||
settings = override_settings(
|
||||
MEDIA_ROOT=temporary.name + "/media",
|
||||
PARSER_PRIVATE_ARTIFACT_ROOT=temporary.name + "/private",
|
||||
)
|
||||
settings.enable()
|
||||
self.addCleanup(settings.disable)
|
||||
self.org = Organization.objects.create(
|
||||
name="АО Проверка",
|
||||
inn="7707083893",
|
||||
ogrn="1027700132195",
|
||||
okpo="12345678",
|
||||
directory_imported_at=timezone.now(),
|
||||
)
|
||||
self.artifact, _ = import_fssp_snapshots(
|
||||
inputs=[
|
||||
FsspInput(
|
||||
GENERAL_DATASET,
|
||||
BytesIO(csv_bytes("general.csv")),
|
||||
"general.csv",
|
||||
date(2026, 10, 5),
|
||||
),
|
||||
FsspInput(
|
||||
ENDED_DATASET,
|
||||
BytesIO(
|
||||
csv_bytes(
|
||||
"ended.csv",
|
||||
**{
|
||||
"The number of enforcement proceeding": "555/26/77001-ИП"
|
||||
},
|
||||
)
|
||||
),
|
||||
"ended.csv",
|
||||
date(2026, 10, 5),
|
||||
),
|
||||
],
|
||||
load_batch=1,
|
||||
)
|
||||
self.client = APIClient()
|
||||
self.client.force_authenticate(UserFactory.create_user())
|
||||
self.url = "/api/v2/organization-source-records/"
|
||||
|
||||
def test_list_detail_export_same_uid_and_signed_amount(self):
|
||||
response = self.client.get(
|
||||
self.url,
|
||||
{
|
||||
"source_group": "enforcement_proceedings",
|
||||
"ordering": "amount",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.data
|
||||
assert len(response.data["data"]) == 2
|
||||
row = response.data["data"][0]
|
||||
assert row["amount"] == "-12.50"
|
||||
assert row["payload"]["debt_amount"] == "-12.50"
|
||||
assert row["payload"]["claim_amount"] == "250.00"
|
||||
assert row["payload"]["fetched_at"].endswith("Z")
|
||||
assert row["record_date"] == row["payload"]["initiated_on"] == "2026-10-01"
|
||||
assert row["title"] == "Исполнительное производство 123/26/77001-ИП"
|
||||
detail = self.client.get(self.url + row["uid"] + "/")
|
||||
assert detail.status_code == 200, detail.data
|
||||
assert detail.data["payload"] == row["payload"]
|
||||
assert detail.data["organization"]["uid"] == str(self.org.uid)
|
||||
assert set(
|
||||
_source_group_queryset(
|
||||
"enforcement_proceedings", export_year=2026
|
||||
).values_list("uid", flat=True)
|
||||
) == {UUID(item["uid"]) for item in response.data["data"]}
|
||||
|
||||
def test_document_raw_search_and_terminated_ordering(self):
|
||||
response = self.client.get(
|
||||
self.url,
|
||||
{
|
||||
"source_group": "enforcement_proceedings",
|
||||
"search": "Взыскание сбора",
|
||||
"ordering": "-payload__terminated_on,payload__document_raw,uid",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.data
|
||||
assert len(response.data["data"]) == 2
|
||||
assert response.data["data"][0]["status"] == "ended"
|
||||
assert response.data["data"][1]["payload"]["terminated_on"] is None
|
||||
|
||||
def test_snapshot_history_uses_aggregate_not_latest_input(self):
|
||||
log = ParserLoadLog.objects.create(source=FSSP_SOURCE, batch_id=1)
|
||||
assert get_snapshot_artifact(log).uid == self.artifact.uid
|
||||
|
||||
def test_signed_amount_filter_is_scoped_to_fssp(self):
|
||||
response = self.client.get(
|
||||
self.url,
|
||||
{
|
||||
"source": FSSP_SOURCE,
|
||||
"amount_from": "-20.00",
|
||||
"amount_to": "0.00",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.data
|
||||
assert len(response.data["data"]) == 1
|
||||
assert response.data["data"][0]["amount"] == "-12.50"
|
||||
other = self.client.get(
|
||||
self.url,
|
||||
{
|
||||
"source_group": "government_support",
|
||||
"amount_from": "-20.00",
|
||||
},
|
||||
)
|
||||
assert other.status_code == 400
|
||||
|
||||
def test_payload_has_every_frontend_field_with_nullable_values(self):
|
||||
from organizations.source_groups import get_source_group_descriptor
|
||||
|
||||
descriptor = get_source_group_descriptor(FSSP_SOURCE)
|
||||
assert (descriptor.source_group, descriptor.record_type) == (
|
||||
"enforcement_proceedings",
|
||||
"enforcement_proceeding",
|
||||
)
|
||||
response = self.client.get(self.url, {"source": FSSP_SOURCE})
|
||||
required = set(
|
||||
"origin_kind queried_inn debtor_raw debtor_name debtor_inn debtor_legal_address debtor_actual_address proceeding_raw proceeding_number consolidated_proceeding_number initiated_on document_raw document_type document_date document_number document_issuer document_subject_raw creditor_inn termination_raw terminated_on termination_reason termination_article termination_part termination_point service_text service_links subject_raw subject debt_amount claim_amount debt_currency department_raw department_name department_address bailiff_raw bailiff_name bailiff_phones source_url fetched_at parse_warnings".split()
|
||||
)
|
||||
for row in response.data["data"]:
|
||||
assert required <= row["payload"].keys()
|
||||
assert row["payload"]["queried_inn"] is None
|
||||
assert row["payload"]["service_links"] == []
|
||||
assert row["payload"]["bailiff_phones"] == []
|
||||
|
||||
def test_documented_filters_orderings_and_invalid_values(self):
|
||||
base = {
|
||||
"source_group": "enforcement_proceedings",
|
||||
"source": FSSP_SOURCE,
|
||||
"record_type": "enforcement_proceeding",
|
||||
"organization": str(self.org.uid),
|
||||
}
|
||||
for field in (
|
||||
"extension__organization__name",
|
||||
"record_date",
|
||||
"amount",
|
||||
"payload__document_raw",
|
||||
"payload__terminated_on",
|
||||
"payload__service_text",
|
||||
"uid",
|
||||
):
|
||||
for prefix in ("", "-"):
|
||||
response = self.client.get(
|
||||
self.url, {**base, "ordering": prefix + field}
|
||||
)
|
||||
assert response.status_code == 200, (field, response.data)
|
||||
assert len(response.data["data"]) == 2
|
||||
response = self.client.get(
|
||||
self.url, {**base, "date_from": "2026-10-01", "date_to": "2026-10-01"}
|
||||
)
|
||||
assert len(response.data["data"]) == 2
|
||||
for params in (
|
||||
{"date_from": "2026-02-31"},
|
||||
{"organization": "bad"},
|
||||
{"date_from": "2026-10-05", "date_to": "2026-10-01"},
|
||||
{"page_size": "101"},
|
||||
{"ordering": "foreign"},
|
||||
):
|
||||
assert self.client.get(self.url, {**base, **params}).status_code == 400
|
||||
empty = self.client.get(self.url, {**base, "date_from": "2027-01-01"})
|
||||
assert empty.data["data"] == []
|
||||
assert empty.data["meta"]["pagination"]["total_pages"] == 0
|
||||
beyond = self.client.get(self.url, {**base, "page": 99})
|
||||
assert beyond.status_code == 200, beyond.data
|
||||
assert beyond.data["data"] == []
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from apps.parsers.budget_registry import normalize_budget_record
|
||||
from apps.parsers.fssp_csv import FSSP_SOURCE, GENERAL_DATASET, iter_fssp_records
|
||||
from apps.parsers.models import ParserSourceArtifact
|
||||
from apps.parsers.sme_support import support_payload
|
||||
from apps.parsers.sro_membership import normalize_sro_membership
|
||||
@@ -108,7 +110,31 @@ def _canonical_records():
|
||||
lookup_value=organization.ogrn,
|
||||
),
|
||||
)
|
||||
return [budget, sme, sro], raw_budget
|
||||
fssp_org = Organization.objects.create(
|
||||
name="АО Проверка",
|
||||
inn="7707083893",
|
||||
ogrn="1027700132195",
|
||||
okpo="12345678",
|
||||
directory_imported_at=timezone.now(),
|
||||
opk_registry_membership=False,
|
||||
)
|
||||
csv_fixture = Path(__file__).parents[2] / "fixtures/fssp/general.csv"
|
||||
row = next(
|
||||
iter_fssp_records(StringIO(csv_fixture.read_text()), dataset=GENERAL_DATASET)
|
||||
)
|
||||
fssp = _create_record(
|
||||
fssp_org,
|
||||
FSSP_SOURCE,
|
||||
{
|
||||
"title": row.title,
|
||||
"record_date": row.record_date,
|
||||
"amount": row.amount,
|
||||
"status": row.status,
|
||||
"url": row.url,
|
||||
"payload": row.payload,
|
||||
},
|
||||
)
|
||||
return [budget, sme, sro, fssp], raw_budget
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@@ -129,8 +155,8 @@ def test_source_record_runtime_fixtures():
|
||||
return body
|
||||
|
||||
mixed = capture("mixed-list", "list", client.get(list_url))
|
||||
assert len(mixed["data"]) == 3
|
||||
assert mixed["meta"]["pagination"]["total_count"] == 3
|
||||
assert len(mixed["data"]) == 4
|
||||
assert mixed["meta"]["pagination"]["total_count"] == 4
|
||||
details = {}
|
||||
for record in records:
|
||||
listing = capture(
|
||||
|
||||
@@ -66,16 +66,23 @@ def issue(client, group="budget_process_registry", file_format="json"):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source,group",
|
||||
[
|
||||
("budget_ubpandnubp", "budget_process_registry"),
|
||||
("fssp_enforcement_proceedings", "enforcement_proceedings"),
|
||||
],
|
||||
)
|
||||
def test_ticket_pins_generation_across_publication_and_rapid_rebuilds(
|
||||
export_client, tmp_path
|
||||
export_client, tmp_path, source, group
|
||||
):
|
||||
first_artifact = publication()
|
||||
first_artifact = publication(source=source)
|
||||
first = exports.build_source_record_export_artifacts()
|
||||
assert first.source_snapshots == {"budget_ubpandnubp": str(first_artifact.pk)}
|
||||
ticket = issue(export_client)
|
||||
assert first.source_snapshots == {source: str(first_artifact.pk)}
|
||||
ticket = issue(export_client, group=group)
|
||||
assert ticket.status_code == 201
|
||||
publication(batch=2)
|
||||
stale = issue(export_client)
|
||||
publication(source=source, batch=2)
|
||||
stale = issue(export_client, group=group)
|
||||
assert stale.status_code == 503
|
||||
assert stale.data["code"] == "source_export_not_ready"
|
||||
exports.build_source_record_export_artifacts()
|
||||
@@ -87,7 +94,7 @@ def test_ticket_pins_generation_across_publication_and_rapid_rebuilds(
|
||||
assert package.generation_id == first.generation_id
|
||||
with zipfile.ZipFile(BytesIO(b"".join(package.archive_chunks))) as archive:
|
||||
assert archive.testzip() is None
|
||||
assert issue(export_client).status_code == 201
|
||||
assert issue(export_client, group=group).status_code == 201
|
||||
retired = tmp_path / "generations" / first.generation_id / ".retired"
|
||||
old = time.time() - 301
|
||||
os.utime(retired, (old, old))
|
||||
@@ -210,7 +217,7 @@ def test_sro_export_keeps_all_years_and_identifier_strings(export_client, file_f
|
||||
record_date="2001-01-01",
|
||||
)
|
||||
generation = exports.build_source_record_export_artifacts()
|
||||
assert generation.artifacts_count == 49
|
||||
assert generation.artifacts_count == 52
|
||||
response = issue(export_client, "sro_membership", file_format)
|
||||
assert response.status_code == 201
|
||||
package = exports.consume_source_record_export_download_ticket(
|
||||
|
||||
@@ -128,9 +128,9 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
call_command("build_source_record_exports", stdout=command_output)
|
||||
|
||||
generation = load_current_source_record_export_generation()
|
||||
self.assertEqual(generation.artifacts_count, 49)
|
||||
self.assertEqual(generation.artifacts_count, 52)
|
||||
self.assertEqual(generation.export_year, timezone.localdate().year)
|
||||
self.assertIn('"artifacts_count": 49', command_output.getvalue())
|
||||
self.assertIn('"artifacts_count": 52', command_output.getvalue())
|
||||
|
||||
def test_sanctions_xlsx_export_uses_source_contract_columns_and_boolean_flags(self):
|
||||
organization = Organization.objects.create(
|
||||
@@ -641,7 +641,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertEqual(
|
||||
response["X-Source-Export-Generated-At"], generation.generated_at
|
||||
)
|
||||
self.assertEqual(generation.artifacts_count, 49)
|
||||
self.assertEqual(generation.artifacts_count, 52)
|
||||
self.assertIn(
|
||||
'filename="planned-inspections__financial-indicators_',
|
||||
response["Content-Disposition"],
|
||||
@@ -840,8 +840,8 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
key=lambda item: item.file_name,
|
||||
)
|
||||
|
||||
self.assertEqual(generation.artifacts_count, 49)
|
||||
self.assertEqual(generation.files_count, 50)
|
||||
self.assertEqual(generation.artifacts_count, 52)
|
||||
self.assertEqual(generation.files_count, 53)
|
||||
self.assertEqual(
|
||||
[item.file_name for item in artifacts],
|
||||
[
|
||||
@@ -1065,7 +1065,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
first_generation = build_source_record_export_artifacts()
|
||||
current_generation = load_current_source_record_export_generation()
|
||||
|
||||
self.assertEqual(first_generation.artifacts_count, 49)
|
||||
self.assertEqual(first_generation.artifacts_count, 52)
|
||||
self.assertEqual(
|
||||
current_generation.generation_id, first_generation.generation_id
|
||||
)
|
||||
|
||||
@@ -147,7 +147,7 @@ class SourceRecordExportArtifactsTaskTest(TestCase):
|
||||
result = refresh_source_record_export_artifacts()
|
||||
|
||||
self.assertEqual(result["status"], "success")
|
||||
self.assertEqual(result["artifacts_count"], 49)
|
||||
self.assertEqual(result["artifacts_count"], 52)
|
||||
self.assertEqual(result["export_year"], timezone.localdate().year)
|
||||
self.assertIsNone(cache.get(settings.SOURCE_RECORD_EXPORT_LOCK_KEY))
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ class TestCompaniesCommandsTest(TestCase):
|
||||
ParserLoadLog.Source.ROPK_SANCTIONS,
|
||||
ParserLoadLog.Source.INTEGRATED_STRUCTURE_DOCUMENTS,
|
||||
ParserLoadLog.Source.FEDRESURS_COMPANY_PROFILE,
|
||||
ParserLoadLog.Source.FSSP_ENFORCEMENT_PROCEEDINGS,
|
||||
}
|
||||
}
|
||||
for company in companies:
|
||||
@@ -273,7 +274,7 @@ class TestCompaniesCommandsTest(TestCase):
|
||||
)
|
||||
self.assertEqual(
|
||||
OrganizationSourceRecord.objects.count(),
|
||||
20 * (len(ParserLoadLog.Source) - 3),
|
||||
20 * (len(ParserLoadLog.Source) - 4),
|
||||
)
|
||||
|
||||
def test_create_replaces_stale_source_record_for_same_legacy_row(self):
|
||||
|
||||
Reference in New Issue
Block a user