Files
mostovik-backend/tests/apps/organizations/test_source_export_postgres.py
Aleksandr Meshchryakov f0a0266495
All checks were successful
Mostovik Backend CI/CD / Tests and lint (push) Successful in 4m1s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Successful in 4m9s
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Successful in 3m5s
feat: configure export year window and publish ZIP archives under 99 MB
2026-11-03 10:02:04 +01:00

127 lines
4.6 KiB
Python

"""Run against settings pointing to an isolated PostgreSQL test database."""
from concurrent.futures import ThreadPoolExecutor
from threading import Barrier
from uuid import uuid4
import pytest
from django.db import close_old_connections, connection
from django.test import override_settings
from organizations.models import (
GovernmentProcurementExtension,
Organization,
OrganizationSourceRecord,
)
from organizations.source_export_catalog import build_source, issue_ticket
from rest_framework.test import APIClient
from tests.apps.user.factories import UserFactory
@pytest.mark.parametrize("selection_kind", ["file", "source_archive"])
@pytest.mark.django_db(transaction=True)
@pytest.mark.skipif(
connection.vendor != "postgresql",
reason="Requires PostgreSQL row locks and independent connections",
)
def test_two_simultaneous_native_posts_one_ticket(tmp_path, selection_kind):
with override_settings(SOURCE_EXPORT_DIRECTORY=str(tmp_path)):
user = UserFactory.create_superuser()
organization = Organization.objects.create(
name="Synthetic race fixture", inn="0012345678", ogrn="1027700132195"
)
extension = GovernmentProcurementExtension.objects.create(
organization=organization
)
OrganizationSourceRecord.objects.create(
extension=extension,
source="procurements_44fz",
record_type="procurement",
external_id=str(uuid4()),
)
generation = build_source("government_procurements")
ticket = issue_ticket(
user,
{
"selection_kind": selection_kind,
"source_group": "government_procurements",
"generation_id": str(generation.pk),
**(
{"part_id": generation.manifest["parts"][0]["part_id"]}
if selection_kind == "file"
else {
"archive_id": next(
a["archive_id"]
for a in generation.manifest["archives"]
if a["format"] == "json"
)
}
),
"format": "json",
},
)
barrier = Barrier(2)
def post():
close_old_connections()
try:
barrier.wait(timeout=10)
response = APIClient().post(
"/api/v2/organization-source-records/export-download/",
"ticket=" + ticket["ticket"],
content_type="application/x-www-form-urlencoded",
)
if response.streaming:
assert sum(len(chunk) for chunk in response.streaming_content) > 0
code = response.status_code
response.close()
return code
finally:
connection.close()
with ThreadPoolExecutor(max_workers=2) as workers:
futures = [workers.submit(post) for _ in range(2)]
assert sorted(f.result(timeout=30) for f in futures) == [200, 410]
@pytest.mark.django_db(transaction=True)
@pytest.mark.skipif(
connection.vendor != "postgresql",
reason="Requires independent PostgreSQL connections",
)
def test_stream_lease_renewal_and_loss_use_independent_database_connection():
import time
from datetime import timedelta
from unittest.mock import patch
from django.utils import timezone
from organizations.models import SourceExportGeneration, SourceExportLease
from organizations.source_export_catalog import _heartbeat
generation = SourceExportGeneration.objects.create(
source_group="arbitration", data_revision="test"
)
expires = timezone.now() + timedelta(minutes=2)
lease = SourceExportLease.objects.create(
generation=generation, issuer_id="test", kind="stream", expires_at=expires
)
def renew():
return SourceExportLease.objects.filter(
pk=lease.pk, expires_at__gt=timezone.now()
).update(expires_at=timezone.now() + timedelta(minutes=10))
# Accelerate only the timer; connection, SQL and expiry/ownership checks are real.
with patch(
"organizations.source_export_catalog.HEARTBEAT_RENEW_SECONDS", 0.05
), _heartbeat(renew) as lost:
deadline = time.monotonic() + 5
while time.monotonic() < deadline:
lease.refresh_from_db()
if lease.expires_at > expires:
break
time.sleep(0.02)
assert lease.expires_at > expires
assert not lost.is_set()
lease.delete()
assert lost.wait(timeout=5)