feat: expose unified exchange activity APIs and exports
This commit is contained in:
294
tests/apps/organizations/test_exchange_contract.py
Normal file
294
tests/apps/organizations/test_exchange_contract.py
Normal file
@@ -0,0 +1,294 @@
|
||||
"""Unified exchange card, typed observations and source API/export parity."""
|
||||
import importlib
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from apps.core.models import BackgroundJob
|
||||
from apps.parsers.models import ParserLoadLog, ParserSourceArtifact
|
||||
from apps.parsers.snapshot_metadata import get_snapshot_artifact
|
||||
from apps.parsers.source_cards import SOURCE_CARD_DEFINITIONS, SourceCardService
|
||||
from core.celery import app as celery_app
|
||||
from django.core.cache import cache
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from organizations.models import Organization, OrganizationSourceRecord
|
||||
from organizations.source_groups import get_source_group_descriptor
|
||||
from organizations.source_record_export import _build_record_row, _source_group_queryset
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from tests.apps.user.factories import UserFactory
|
||||
|
||||
SOURCE = "stock_exchange_activity"
|
||||
|
||||
|
||||
def exchange_payload(exchange="moex"):
|
||||
return {
|
||||
"exchange": exchange,
|
||||
"instrument_id": "FIXTURE1",
|
||||
"issuer_inn": "7707083893",
|
||||
"issuer_provider_id": "484" if exchange == "moex" else None,
|
||||
"isin": "RU0000000001",
|
||||
"instrument_kind": "exchange_bond",
|
||||
"listing_date": "2020-01-01",
|
||||
"nominal_value": "1000.00",
|
||||
"nominal_currency": "RUB",
|
||||
"parameters": {"unknown": {"keep": [None, False, 0, "001"]}},
|
||||
"trading": {
|
||||
"traded_on": "2026-10-05",
|
||||
"board_id": "TQCB",
|
||||
"upstream": {"LAST": "101.2300"},
|
||||
}
|
||||
if exchange == "moex"
|
||||
else None,
|
||||
"duration": {"value": "260", "unit": "days", "calculated_on": "2026-10-02"}
|
||||
if exchange == "moex"
|
||||
else None,
|
||||
"yield": {"value": None, "unit": None, "calculated_on": "2026-10-02"}
|
||||
if exchange == "moex"
|
||||
else None,
|
||||
"provenance": {"artifact_id": "00000000-0000-0000-0000-000000000001"},
|
||||
"upstream": {"unknown": [0, None, False, "0007"]},
|
||||
}
|
||||
|
||||
|
||||
class ExchangeContractTests(TestCase):
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
self.org = Organization.objects.create(
|
||||
name="Canonical issuer",
|
||||
inn="7707083893",
|
||||
ogrn="1027700132195",
|
||||
okpo="00032537",
|
||||
directory_imported_at=timezone.now(),
|
||||
)
|
||||
self.extension = get_source_group_descriptor(
|
||||
SOURCE
|
||||
).extension_model.objects.create(organization=self.org)
|
||||
ParserSourceArtifact.objects.create(
|
||||
source=SOURCE,
|
||||
load_batch=1,
|
||||
status="published",
|
||||
original_name="exchange-manifest.json",
|
||||
metadata={"snapshot_organizations_count": 1},
|
||||
published_count=2,
|
||||
)
|
||||
self.rows = [
|
||||
OrganizationSourceRecord.objects.create(
|
||||
extension=self.extension,
|
||||
source=SOURCE,
|
||||
record_type="exchange_instrument",
|
||||
external_id=exchange + ":FIXTURE1",
|
||||
title="Fixture",
|
||||
load_batch=1,
|
||||
payload=exchange_payload(exchange),
|
||||
)
|
||||
for exchange in ("moex", "spb")
|
||||
]
|
||||
self.client = APIClient()
|
||||
self.client.force_authenticate(UserFactory.create_user())
|
||||
self.url = "/api/v2/organization-source-records/"
|
||||
|
||||
def test_exact_unified_source_card_and_list_detail_export(self):
|
||||
descriptor = get_source_group_descriptor(SOURCE)
|
||||
assert descriptor.source_group == SOURCE
|
||||
assert descriptor.record_type == "exchange_instrument"
|
||||
assert (
|
||||
len(
|
||||
[
|
||||
c
|
||||
for c in SOURCE_CARD_DEFINITIONS
|
||||
if c.slug == "stock-exchange-activity"
|
||||
]
|
||||
)
|
||||
== 1
|
||||
)
|
||||
response = self.client.get(self.url, {"source_group": SOURCE})
|
||||
assert response.status_code == 200, response.data
|
||||
assert len(response.data["data"]) == 2
|
||||
for row in response.data["data"]:
|
||||
assert row["amount"] is None
|
||||
assert row["organization"]["okpo"] == "00032537"
|
||||
detail = self.client.get(self.url + row["uid"] + "/")
|
||||
assert detail.data["payload"] == row["payload"]
|
||||
record = _source_group_queryset(SOURCE, export_year=2026).get(
|
||||
uid=row["uid"]
|
||||
)
|
||||
exported = _build_record_row(record, include_financial_lines=False)
|
||||
assert exported["payload.exchange"] == row["payload"]["exchange"]
|
||||
assert exported["payload.nominal_value"] == "1000.00"
|
||||
assert exported["amount"] is None
|
||||
if row["payload"]["exchange"] == "moex":
|
||||
assert exported["payload.duration.value"] == "260"
|
||||
assert exported["payload.duration.unit"] == "days"
|
||||
assert row["payload"]["yield"]["value"] is None
|
||||
|
||||
def test_exchange_filters_ordering_and_invalid_exchange(self):
|
||||
response = self.client.get(
|
||||
self.url,
|
||||
{
|
||||
"source_group": SOURCE,
|
||||
"exchange": "moex",
|
||||
"instrument_kind": "exchange_bond",
|
||||
"ordering": "-payload__instrument_id",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.data
|
||||
assert len(response.data["data"]) == 1
|
||||
assert response.data["data"][0]["payload"]["exchange"] == "moex"
|
||||
assert (
|
||||
self.client.get(
|
||||
self.url, {"source_group": SOURCE, "exchange": "unknown"}
|
||||
).status_code
|
||||
== 400
|
||||
)
|
||||
|
||||
def test_input_artifact_never_overrides_aggregate_provenance(self):
|
||||
log = ParserLoadLog.objects.create(source=SOURCE, batch_id=1, status="success")
|
||||
child = ParserSourceArtifact.objects.create(
|
||||
source=SOURCE,
|
||||
load_batch=1,
|
||||
status="parsed",
|
||||
original_name="moex.ndjson",
|
||||
metadata={"dataset": "moex", "input_only": True},
|
||||
)
|
||||
assert get_snapshot_artifact(log).uid != child.uid
|
||||
|
||||
|
||||
def test_invalid_exchange_rejected_by_named_serializer():
|
||||
module = importlib.import_module("organizations.exchange_payload_serializers")
|
||||
serializer = module.ExchangeInstrumentPayloadSerializer(
|
||||
data=exchange_payload("invalid")
|
||||
)
|
||||
assert not serializer.is_valid()
|
||||
assert "exchange" in serializer.errors
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_one_job_refresh_run_shared_guard_permissions():
|
||||
importlib.import_module("apps.parsers.tasks")
|
||||
task = celery_app.tasks["parsers.stock_exchange_activity.refresh"]
|
||||
client = APIClient()
|
||||
client.force_authenticate(UserFactory.create_user())
|
||||
urls = [
|
||||
"/api/v1/sources/stock-exchange-activity/refresh/",
|
||||
"/api/v1/parsers/run/stock_exchange_activity/",
|
||||
]
|
||||
assert all(client.post(url, {}, format="json").status_code == 403 for url in urls)
|
||||
client.force_authenticate(UserFactory.create_user(is_staff=True))
|
||||
with patch.object(task, "apply_async"):
|
||||
assert client.post(urls[0], {}, format="json").status_code == 202
|
||||
assert client.post(urls[1], {}, format="json").status_code == 409
|
||||
assert BackgroundJob.objects.count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_worker_success_propagates_combined_manifest_counters_and_failure_keeps_success():
|
||||
from apps.parsers import exchange_snapshot
|
||||
from apps.parsers.registry_snapshots import SnapshotResult, SnapshotValidationError
|
||||
|
||||
importlib.import_module("apps.parsers.tasks")
|
||||
task = celery_app.tasks["parsers.stock_exchange_activity.refresh"]
|
||||
cache.clear()
|
||||
|
||||
def refresh(**kwargs):
|
||||
artifact = ParserSourceArtifact.objects.create(
|
||||
source=SOURCE,
|
||||
load_batch=kwargs["load_batch"],
|
||||
status="published",
|
||||
original_name="exchange-manifest.json",
|
||||
parsed_count=5,
|
||||
published_count=2,
|
||||
quarantined_count=1,
|
||||
metadata={
|
||||
"snapshot_organizations_count": 1,
|
||||
"adapter_counters": {"spb": {"parsed": 3}, "moex": {"parsed": 2}},
|
||||
"inputs": [{"exchange": "spb"}, {"exchange": "moex"}],
|
||||
},
|
||||
)
|
||||
result = SnapshotResult(parsed=5, published=2, quarantined=1, skipped=2)
|
||||
kwargs["on_publish"](artifact, result)
|
||||
return artifact, result
|
||||
|
||||
with patch.object(exchange_snapshot, "refresh_stock_exchange_activity", refresh):
|
||||
result = task.apply(throw=True).result
|
||||
assert result["published_records_count"] == 2
|
||||
assert result["adapter_counters"]["spb"]["parsed"] == 3
|
||||
assert len(result["inputs"]) == 2
|
||||
before = SourceCardService.get_card("stock-exchange-activity")[
|
||||
"latest_success_load"
|
||||
]["batch_id"]
|
||||
|
||||
def fail(**kwargs):
|
||||
ParserSourceArtifact.objects.create(
|
||||
source=SOURCE,
|
||||
load_batch=kwargs["load_batch"],
|
||||
status="rejected",
|
||||
original_name="exchange-manifest.json",
|
||||
metadata={
|
||||
"rejection_reason": "synthetic_failure",
|
||||
"inputs": [{"exchange": "spb"}, {"exchange": "moex"}],
|
||||
},
|
||||
)
|
||||
ParserSourceArtifact.objects.create(
|
||||
source=SOURCE,
|
||||
load_batch=kwargs["load_batch"],
|
||||
status="rejected",
|
||||
original_name="moex.ndjson",
|
||||
metadata={"dataset": "moex", "input_only": True},
|
||||
)
|
||||
raise SnapshotValidationError("synthetic_failure")
|
||||
|
||||
with patch.object(
|
||||
exchange_snapshot, "refresh_stock_exchange_activity", fail
|
||||
), pytest.raises(SnapshotValidationError):
|
||||
task.apply(throw=True)
|
||||
card = SourceCardService.get_card("stock-exchange-activity")
|
||||
assert card["latest_success_load"]["batch_id"] == before
|
||||
job = BackgroundJob.objects.order_by("-created_at").first()
|
||||
assert job.result["rejection_reason"] == "synthetic_failure"
|
||||
assert len(job.result["inputs"]) == 2
|
||||
|
||||
|
||||
def test_exchange_discriminator_and_nullable_metrics_in_generated_contract():
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
document = json.loads(
|
||||
(Path(__file__).parents[3] / "src/core/openapi.json").read_text()
|
||||
)
|
||||
schemas = document["components"]["schemas"]
|
||||
for name in ("OrganizationSourceRecord", "OrganizationSourceRecordList"):
|
||||
mapping = schemas[name]["discriminator"]["mapping"]
|
||||
variant = schemas[mapping[SOURCE].rsplit("/", 1)[1]]
|
||||
assert variant["properties"]["record_type"]["enum"] == ["exchange_instrument"]
|
||||
payload = schemas["ExchangeInstrumentListPayload"]
|
||||
assert payload["properties"]["duration"]["nullable"] is True
|
||||
assert payload["properties"]["yield"]["nullable"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize(
|
||||
"module_name",
|
||||
[
|
||||
"apps.parsers.migrations.0043_fssp_disabled_schedule",
|
||||
"apps.parsers.migrations.0045_checko_disabled_schedules",
|
||||
"apps.parsers.migrations.0047_exchange_disabled_schedule",
|
||||
],
|
||||
)
|
||||
def test_new_schedule_seed_always_disabled(module_name):
|
||||
from django.apps import apps
|
||||
from django_celery_beat.models import PeriodicTask
|
||||
|
||||
module = importlib.import_module(module_name)
|
||||
module.seed(apps, None)
|
||||
tasks = PeriodicTask.objects.filter(
|
||||
task__in=[
|
||||
"parsers.fssp_enforcement_proceedings.refresh",
|
||||
"parsers.checko_leasing.refresh",
|
||||
"parsers.checko_messages.refresh",
|
||||
"parsers.checko_licenses.refresh",
|
||||
"parsers.stock_exchange_activity.refresh",
|
||||
]
|
||||
)
|
||||
assert tasks.exists()
|
||||
assert not tasks.filter(enabled=True).exists()
|
||||
Reference in New Issue
Block a user