feat: limit source exports to current year
All checks were successful
All checks were successful
This commit is contained in:
@@ -8,6 +8,7 @@ from django.apps import apps as django_apps
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
from django_celery_beat.models import PeriodicTask
|
||||
|
||||
|
||||
@@ -36,6 +37,7 @@ class SourceRecordExportArtifactsTaskTest(TestCase):
|
||||
|
||||
self.assertEqual(result["status"], "success")
|
||||
self.assertEqual(result["artifacts_count"], 25)
|
||||
self.assertEqual(result["export_year"], timezone.localdate().year)
|
||||
self.assertIsNone(cache.get(settings.SOURCE_RECORD_EXPORT_LOCK_KEY))
|
||||
|
||||
def test_refresh_task_skips_when_generation_lock_is_held(self):
|
||||
|
||||
@@ -2,19 +2,24 @@
|
||||
|
||||
import json
|
||||
import zipfile
|
||||
from datetime import UTC, datetime
|
||||
from io import BytesIO, StringIO
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from apps.external_data.source_record_export import (
|
||||
ORGANIZATION_EXPORT_FIELDS,
|
||||
SOURCE_GROUP_EXPORT_SPECS,
|
||||
SOURCE_RECORD_EXPORT_FIELDS,
|
||||
SourceRecordExportArtifactsUnavailable,
|
||||
_source_group_headers,
|
||||
build_source_record_export_artifacts,
|
||||
build_source_records_export_archive,
|
||||
load_current_source_record_export_generation,
|
||||
)
|
||||
from django.core.management import call_command
|
||||
from django.test import override_settings
|
||||
from django.utils import timezone
|
||||
from openpyxl import load_workbook
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
@@ -28,6 +33,7 @@ from tests.apps.external_data.factories import (
|
||||
IndustrialProductFactory,
|
||||
ManufacturerRegistryEntryFactory,
|
||||
ProsecutorCheckFactory,
|
||||
PublicProcurementFactory,
|
||||
)
|
||||
from tests.apps.organization.factories import OrganizationFactory
|
||||
from tests.apps.user.factories import UserFactory
|
||||
@@ -71,23 +77,38 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
self.assertEqual(response["Retry-After"], "3600")
|
||||
|
||||
def test_generation_builds_full_matrix_from_normalized_tables(self):
|
||||
current_date = timezone.localdate()
|
||||
organization = OrganizationFactory.create(
|
||||
full_name='Акционерное общество "Экспорт"',
|
||||
okpo="12345678",
|
||||
)
|
||||
IndustrialProductFactory.create(organization=organization)
|
||||
IndustrialCertificateFactory.create(organization=organization)
|
||||
IndustrialCertificateFactory.create(
|
||||
organization=organization,
|
||||
issue_date=current_date,
|
||||
)
|
||||
ManufacturerRegistryEntryFactory.create(organization=organization)
|
||||
ProsecutorCheckFactory.create(organization=organization)
|
||||
arbitration_case = ArbitrationCaseFactory.create(organization=organization)
|
||||
ProsecutorCheckFactory.create(
|
||||
organization=organization,
|
||||
start_date=current_date,
|
||||
)
|
||||
arbitration_case = ArbitrationCaseFactory.create(
|
||||
organization=organization,
|
||||
decision_date=current_date,
|
||||
)
|
||||
report = FinancialReportFactory.create(organization=organization)
|
||||
FinancialReportLineFactory.create(report=report, line_code="1600")
|
||||
FinancialReportLineFactory.create(
|
||||
report=report,
|
||||
line_code="1600",
|
||||
year=current_date.year,
|
||||
)
|
||||
|
||||
generation = build_source_record_export_artifacts()
|
||||
|
||||
self.assertEqual(generation.artifacts_count, 25)
|
||||
self.assertEqual(generation.files_count, 25)
|
||||
self.assertEqual(generation.records_count, 6)
|
||||
self.assertEqual(generation.export_year, current_date.year)
|
||||
expected_prefix = [*ORGANIZATION_EXPORT_FIELDS, *SOURCE_RECORD_EXPORT_FIELDS]
|
||||
for source_spec in SOURCE_GROUP_EXPORT_SPECS.values():
|
||||
self.assertEqual(
|
||||
@@ -229,14 +250,104 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
|
||||
generation = load_current_source_record_export_generation()
|
||||
self.assertEqual(generation.artifacts_count, 25)
|
||||
self.assertEqual(generation.export_year, timezone.localdate().year)
|
||||
self.assertIn('"artifacts_count": 25', command_output.getvalue())
|
||||
|
||||
def test_generation_contains_only_records_from_its_calendar_year(self):
|
||||
export_year = 2026
|
||||
generated_at = datetime(export_year, 8, 4, 6, 0, tzinfo=UTC)
|
||||
organization = OrganizationFactory.create()
|
||||
current_procurement = PublicProcurementFactory.create(
|
||||
organization=organization,
|
||||
contract_date=datetime(2026, 3, 1, tzinfo=UTC).date(),
|
||||
)
|
||||
PublicProcurementFactory.create(
|
||||
organization=organization,
|
||||
contract_date=datetime(2025, 3, 1, tzinfo=UTC).date(),
|
||||
)
|
||||
current_product = IndustrialProductFactory.create(organization=organization)
|
||||
old_product = IndustrialProductFactory.create(organization=organization)
|
||||
current_product.__class__.objects.filter(pk=current_product.pk).update(
|
||||
created_at=generated_at
|
||||
)
|
||||
old_product.__class__.objects.filter(pk=old_product.pk).update(
|
||||
created_at=datetime(2025, 8, 4, 6, 0, tzinfo=UTC)
|
||||
)
|
||||
|
||||
current_report = FinancialReportFactory.create(organization=organization)
|
||||
old_report = FinancialReportFactory.create(organization=organization)
|
||||
FinancialReportLineFactory.create(report=current_report, year=2025)
|
||||
FinancialReportLineFactory.create(
|
||||
report=current_report,
|
||||
year=export_year,
|
||||
line_code="1601",
|
||||
)
|
||||
FinancialReportLineFactory.create(report=old_report, year=2025)
|
||||
|
||||
generation = build_source_record_export_artifacts(now=generated_at)
|
||||
|
||||
self.assertEqual(generation.export_year, export_year)
|
||||
procurements_path = next(
|
||||
artifact.path
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == "government_procurements"
|
||||
and artifact.file_format == "json"
|
||||
)
|
||||
procurement_rows = json.loads(procurements_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
[row["uid"] for row in procurement_rows],
|
||||
[str(current_procurement.pk)],
|
||||
)
|
||||
|
||||
industrial_path = next(
|
||||
artifact.path
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == "industrial_production"
|
||||
and artifact.file_format == "json"
|
||||
)
|
||||
industrial_rows = json.loads(industrial_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
[row["uid"] for row in industrial_rows],
|
||||
[str(current_product.pk)],
|
||||
)
|
||||
|
||||
financial_path = next(
|
||||
artifact.path
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == "financial_indicators"
|
||||
)
|
||||
financial_rows = json.loads(financial_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
[row["uid"] for row in financial_rows],
|
||||
[str(current_report.pk)],
|
||||
)
|
||||
self.assertEqual(
|
||||
{line["year"] for line in financial_rows[0]["financial_lines"]},
|
||||
{export_year},
|
||||
)
|
||||
|
||||
def test_new_calendar_year_requires_a_new_prepared_generation(self):
|
||||
build_source_record_export_artifacts(
|
||||
now=datetime(2026, 12, 31, 23, 59, tzinfo=UTC)
|
||||
)
|
||||
|
||||
with self.assertRaises(SourceRecordExportArtifactsUnavailable):
|
||||
build_source_records_export_archive(
|
||||
source_groups=["planned_inspections"],
|
||||
export_format="json",
|
||||
requested_at=datetime(2027, 1, 1, tzinfo=UTC),
|
||||
)
|
||||
|
||||
def test_admin_streams_selected_prepared_files_without_database_queries(self):
|
||||
self.client.force_authenticate(UserFactory.create_superuser())
|
||||
organization = OrganizationFactory.create(okpo="87654321")
|
||||
ProsecutorCheckFactory.create(organization=organization)
|
||||
current_date = timezone.localdate()
|
||||
ProsecutorCheckFactory.create(
|
||||
organization=organization,
|
||||
start_date=current_date,
|
||||
)
|
||||
report = FinancialReportFactory.create(organization=organization)
|
||||
FinancialReportLineFactory.create(report=report)
|
||||
FinancialReportLineFactory.create(report=report, year=current_date.year)
|
||||
generation = build_source_record_export_artifacts()
|
||||
|
||||
with self.assertNumQueries(0):
|
||||
@@ -274,7 +385,8 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
self.assertEqual(rows[1][4], "87654321")
|
||||
|
||||
def test_ticket_is_admin_only_and_can_be_consumed_once_without_auth(self):
|
||||
build_source_record_export_artifacts()
|
||||
requested_at = datetime(2026, 8, 4, 12, 34, 56, tzinfo=UTC)
|
||||
build_source_record_export_artifacts(now=requested_at)
|
||||
regular_user = UserFactory.create_user()
|
||||
self.client.force_authenticate(regular_user)
|
||||
forbidden_response = self.client.post(
|
||||
@@ -285,7 +397,13 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
self.assertEqual(forbidden_response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
self.client.force_authenticate(UserFactory.create_superuser())
|
||||
with self.assertNumQueries(0):
|
||||
with (
|
||||
patch(
|
||||
"apps.external_data.source_record_export.timezone.now",
|
||||
return_value=requested_at,
|
||||
),
|
||||
self.assertNumQueries(0),
|
||||
):
|
||||
ticket_response = self.client.post(
|
||||
self.ticket_url,
|
||||
{"sources": ["bankruptcy"], "format": "json"},
|
||||
@@ -294,6 +412,10 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
self.assertEqual(ticket_response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertRegex(ticket_response.data["ticket"], r"^[A-Za-z0-9_-]{43}$")
|
||||
self.assertEqual(ticket_response.data["expires_in"], 300)
|
||||
self.assertEqual(
|
||||
ticket_response.data["file_name"],
|
||||
"bankruptcy-procedures_20260804_123456.zip",
|
||||
)
|
||||
|
||||
self.client.force_authenticate(user=None)
|
||||
with self.assertNumQueries(0):
|
||||
@@ -303,6 +425,10 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
format="multipart",
|
||||
)
|
||||
self.assertEqual(download_response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(
|
||||
download_response["Content-Disposition"],
|
||||
'attachment; filename="bankruptcy-procedures_20260804_123456.zip"',
|
||||
)
|
||||
with zipfile.ZipFile(
|
||||
BytesIO(self._response_body(download_response))
|
||||
) as archive:
|
||||
@@ -319,7 +445,11 @@ class SourceRecordExportApiTest(APITestCase):
|
||||
@override_settings(SOURCE_RECORD_EXPORT_XLSX_ROWS_PER_FILE=2)
|
||||
def test_xlsx_is_split_into_bounded_workbook_parts(self):
|
||||
organization = OrganizationFactory.create()
|
||||
ProsecutorCheckFactory.create_batch(3, organization=organization)
|
||||
ProsecutorCheckFactory.create_batch(
|
||||
3,
|
||||
organization=organization,
|
||||
start_date=timezone.localdate(),
|
||||
)
|
||||
|
||||
generation = build_source_record_export_artifacts()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user