feat: expose unified exchange activity APIs and exports

This commit is contained in:
Aleksandr Meshchryakov
2026-10-05 17:33:33 +02:00
parent d69ba600d4
commit 3ce9898c0e
30 changed files with 1324 additions and 85 deletions

View File

@@ -2,8 +2,10 @@
import json
import os
from importlib import import_module
from io import StringIO
from pathlib import Path
from unittest.mock import patch
from uuid import uuid4
import pytest
@@ -20,6 +22,7 @@ from organizations.models import Organization, OrganizationSourceRecord
from organizations.source_groups import get_source_group_descriptor
from rest_framework.test import APIClient
from tests.apps.organizations.test_exchange_contract import exchange_payload
from tests.apps.parsers.test_checko_sections import client_with, fixture
from tests.apps.parsers.test_registry_payload_contract import full_budget_fixture
from tests.apps.parsers.test_registry_snapshots import _xml
@@ -163,7 +166,29 @@ def _canonical_records():
},
)
)
return [budget, sme, sro, fssp, *sections], raw_budget
exchange = _create_record(
fssp_org,
"stock_exchange_activity",
{
"title": "Synthetic instrument",
"record_date": "2026-10-05",
"amount": None,
"status": "",
"url": "https://iss.moex.com/iss/securities/FIXTURE1.json",
"payload": exchange_payload(),
},
)
spb = OrganizationSourceRecord.objects.create(
extension=exchange.extension,
source="stock_exchange_activity",
record_type="exchange_instrument",
external_id="spb:FIXTURE1",
load_batch=9001,
title="Synthetic SPB instrument",
url="https://spbexchange.ru/listing/securities/list/",
payload=exchange_payload("spb"),
)
return [budget, sme, sro, fssp, *sections, exchange, spb], raw_budget
@pytest.mark.django_db
@@ -184,22 +209,31 @@ def test_source_record_runtime_fixtures():
return body
mixed = capture("mixed-list", "list", client.get(list_url))
assert len(mixed["data"]) == 7
assert mixed["meta"]["pagination"]["total_count"] == 7
assert len(mixed["data"]) == 9
assert mixed["meta"]["pagination"]["total_count"] == 9
details = {}
listed_sources = set()
for record in records:
listing = capture(
f"{record.source}-list",
"list",
client.get(list_url, {"source": record.source}),
)
assert len(listing["data"]) == 1
if record.source not in listed_sources:
listing = capture(
f"{record.source}-list",
"list",
client.get(list_url, {"source": record.source}),
)
assert len(listing["data"]) == (
2 if record.source == "stock_exchange_activity" else 1
)
listed_sources.add(record.source)
detail_url = reverse(
"api_v2:organizations:organization-source-records-detail",
kwargs={"uid": record.uid},
)
details[record.source] = capture(
f"{record.source}-detail", "detail", client.get(detail_url)
f"{record.source}-spb-detail"
if record is records[-1]
else f"{record.source}-detail",
"detail",
client.get(detail_url),
)
budget_payload = details["budget_ubpandnubp"]["payload"]
assert budget_payload["upstream"] == raw_budget
@@ -238,6 +272,57 @@ def test_source_record_runtime_fixtures():
status,
)
from core.celery import app as celery_app
import_module("apps.parsers.tasks")
sources = [
("fssp_enforcement_proceedings", "fssp-enforcement-proceedings"),
("checko_leasing", "checko-leasing"),
("checko_messages", "checko-messages"),
("checko_licenses", "checko-licenses"),
("stock_exchange_activity", "stock-exchange-activity"),
]
for source, slug in sources:
capture(
f"{source}-refresh-forbidden",
"refresh",
client.post(f"/api/v1/sources/{slug}/refresh/", {}, format="json"),
403,
)
client.force_authenticate(UserFactory.create_user(is_staff=True))
for source, slug in sources:
refresh_url = f"/api/v1/sources/{slug}/refresh/"
capture(
f"{source}-refresh-invalid",
"refresh",
client.post(
refresh_url,
{"params": {"file_url": "https://foreign.invalid"}},
format="json",
),
400,
)
task = celery_app.tasks[f"parsers.{source}.refresh"]
with patch.object(task, "apply_async"):
capture(
f"{source}-refresh-accepted",
"refresh",
client.post(refresh_url, {}, format="json"),
202,
)
capture(
f"{source}-refresh-conflict",
"refresh",
client.post(refresh_url, {}, format="json"),
409,
)
capture(
"missing-refresh",
"refresh",
client.post("/api/v1/sources/unknown-source/refresh/", {}, format="json"),
404,
)
# Ordinary pytest runs create no artifact. CI opts in with an explicit path.
destination = os.environ.get("OPENAPI_RUNTIME_FIXTURES")
if destination: