feat: limit source exports to current year
This commit is contained in:
@@ -4,6 +4,7 @@ import csv
|
||||
import json
|
||||
import os
|
||||
import zipfile
|
||||
from datetime import UTC, datetime
|
||||
from io import BytesIO, StringIO
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
@@ -12,6 +13,7 @@ from unittest.mock import patch
|
||||
from django.core.management import call_command
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from openpyxl import load_workbook
|
||||
from organizations.models import (
|
||||
FinancialIndicatorsExtension,
|
||||
@@ -22,6 +24,7 @@ from organizations.models import (
|
||||
SourceGroup,
|
||||
)
|
||||
from organizations.source_record_export import (
|
||||
SourceRecordExportArtifactsUnavailable,
|
||||
_render_source_group_artifact,
|
||||
_source_group_queryset,
|
||||
_spool_source_group_rows,
|
||||
@@ -89,8 +92,120 @@ class OrganizationSourceRecordExportApiV2Test(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 = Organization.objects.create(
|
||||
name='ООО "Годовая выгрузка"',
|
||||
inn="7707083899",
|
||||
)
|
||||
inspection_extension = PlannedInspectionExtension.objects.create(
|
||||
organization=organization,
|
||||
title="Плановые проверки Генпрокуратуры России",
|
||||
)
|
||||
current_record = OrganizationSourceRecord.objects.create(
|
||||
extension=inspection_extension,
|
||||
record_type="inspection",
|
||||
source="inspections",
|
||||
external_id="INSP-2026",
|
||||
title="Текущая проверка",
|
||||
record_date="15.02.2026",
|
||||
)
|
||||
OrganizationSourceRecord.objects.create(
|
||||
extension=inspection_extension,
|
||||
record_type="inspection",
|
||||
source="inspections",
|
||||
external_id="INSP-2025",
|
||||
title="Прошлогодняя проверка",
|
||||
record_date="15.02.2025",
|
||||
)
|
||||
dateless_current_record = OrganizationSourceRecord.objects.create(
|
||||
extension=inspection_extension,
|
||||
record_type="inspection",
|
||||
source="inspections",
|
||||
external_id="INSP-DATELESS-2026",
|
||||
title="Запись без предметной даты",
|
||||
)
|
||||
OrganizationSourceRecord.objects.filter(pk=dateless_current_record.pk).update(
|
||||
created_at=generated_at
|
||||
)
|
||||
|
||||
financial_extension = FinancialIndicatorsExtension.objects.create(
|
||||
organization=organization,
|
||||
title="Финансово-экономические показатели",
|
||||
)
|
||||
current_financial_record = OrganizationSourceRecord.objects.create(
|
||||
extension=financial_extension,
|
||||
record_type="financial_report",
|
||||
source="fns_reports",
|
||||
external_id="FIN-CURRENT",
|
||||
title="Отчёт с текущим годом",
|
||||
)
|
||||
old_financial_record = OrganizationSourceRecord.objects.create(
|
||||
extension=financial_extension,
|
||||
record_type="financial_report",
|
||||
source="fns_reports",
|
||||
external_id="FIN-OLD",
|
||||
title="Старый отчёт",
|
||||
)
|
||||
for source_record, year in (
|
||||
(current_financial_record, 2025),
|
||||
(current_financial_record, 2026),
|
||||
(old_financial_record, 2025),
|
||||
):
|
||||
OrganizationSourceFinancialLine.objects.create(
|
||||
source_record=source_record,
|
||||
form_code="1",
|
||||
line_code=str(year),
|
||||
line_name=f"Строка {year}",
|
||||
year=year,
|
||||
period_end=year,
|
||||
)
|
||||
|
||||
generation = build_source_record_export_artifacts(now=generated_at)
|
||||
|
||||
self.assertEqual(generation.export_year, export_year)
|
||||
inspection_path = next(
|
||||
artifact.path
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == SourceGroup.PLANNED_INSPECTIONS.value
|
||||
and artifact.file_format == "json"
|
||||
)
|
||||
inspection_rows = json.loads(inspection_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
{row["uid"] for row in inspection_rows},
|
||||
{str(current_record.pk), str(dateless_current_record.pk)},
|
||||
)
|
||||
|
||||
financial_path = next(
|
||||
artifact.path
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == SourceGroup.FINANCIAL_INDICATORS.value
|
||||
)
|
||||
financial_rows = json.loads(financial_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
[row["uid"] for row in financial_rows],
|
||||
[str(current_financial_record.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):
|
||||
generated_at = datetime(2026, 12, 31, 23, 59, tzinfo=UTC)
|
||||
build_source_record_export_artifacts(now=generated_at)
|
||||
|
||||
with self.assertRaises(SourceRecordExportArtifactsUnavailable):
|
||||
build_source_records_export_archive(
|
||||
source_groups=[SourceGroup.PLANNED_INSPECTIONS.value],
|
||||
export_format="json",
|
||||
requested_at=datetime(2027, 1, 1, tzinfo=UTC),
|
||||
)
|
||||
|
||||
def test_admin_exports_selected_sources_to_zip(self):
|
||||
self.client.force_authenticate(UserFactory.create_superuser())
|
||||
organization = Organization.objects.create(
|
||||
@@ -133,7 +248,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
form_code="1",
|
||||
line_code="1600",
|
||||
line_name="Баланс",
|
||||
year=2025,
|
||||
year=timezone.localdate().year,
|
||||
period_start=100,
|
||||
period_end=200,
|
||||
)
|
||||
@@ -160,7 +275,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
)
|
||||
self.assertEqual(generation.artifacts_count, 25)
|
||||
self.assertIn(
|
||||
'filename="organization_source_records_export_',
|
||||
'filename="planned-inspections__financial-indicators_',
|
||||
response["Content-Disposition"],
|
||||
)
|
||||
|
||||
@@ -204,9 +319,16 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
|
||||
def test_admin_uses_one_time_ticket_for_native_zero_sql_download(self):
|
||||
self.client.force_authenticate(UserFactory.create_superuser())
|
||||
build_source_record_export_artifacts()
|
||||
requested_at = datetime(2026, 8, 4, 12, 34, 56, tzinfo=UTC)
|
||||
build_source_record_export_artifacts(now=requested_at)
|
||||
|
||||
with self.assertNumQueries(0):
|
||||
with (
|
||||
patch(
|
||||
"organizations.source_record_export.timezone.now",
|
||||
return_value=requested_at,
|
||||
),
|
||||
self.assertNumQueries(0),
|
||||
):
|
||||
ticket_response = self.client.post(
|
||||
self.ticket_url,
|
||||
{
|
||||
@@ -220,6 +342,10 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertEqual(ticket_response.data["expires_in"], 300)
|
||||
self.assertRegex(ticket_response.data["ticket"], r"^[A-Za-z0-9_-]{43}$")
|
||||
self.assertNotIn("download_url", ticket_response.data)
|
||||
self.assertEqual(
|
||||
ticket_response.data["file_name"],
|
||||
"planned-inspections_20260804_123456.zip",
|
||||
)
|
||||
|
||||
self.client.force_authenticate(user=None)
|
||||
with self.assertNumQueries(0):
|
||||
@@ -233,6 +359,10 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertTrue(download_response.streaming)
|
||||
self.assertEqual(download_response["Content-Type"], "application/zip")
|
||||
self.assertNotIn("Content-Length", download_response)
|
||||
self.assertEqual(
|
||||
download_response["Content-Disposition"],
|
||||
'attachment; filename="planned-inspections_20260804_123456.zip"',
|
||||
)
|
||||
with zipfile.ZipFile(
|
||||
BytesIO(self._response_body(download_response))
|
||||
) as archive:
|
||||
@@ -430,6 +560,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
headers, records_count = _spool_source_group_rows(
|
||||
source_group=SourceGroup.PLANNED_INSPECTIONS.value,
|
||||
output_path=spool_path,
|
||||
export_year=timezone.localdate().year,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
@@ -484,7 +615,10 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertEqual(source_record.payload["provider"], "Checko")
|
||||
|
||||
def test_nightly_export_clears_model_ordering_to_avoid_multi_million_row_sort(self):
|
||||
queryset = _source_group_queryset(SourceGroup.GOVERNMENT_PROCUREMENTS.value)
|
||||
queryset = _source_group_queryset(
|
||||
SourceGroup.GOVERNMENT_PROCUREMENTS.value,
|
||||
export_year=timezone.localdate().year,
|
||||
)
|
||||
|
||||
self.assertFalse(queryset.ordered)
|
||||
self.assertFalse(queryset.query.default_ordering)
|
||||
@@ -514,6 +648,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
headers, records_count = _spool_source_group_rows(
|
||||
source_group=SourceGroup.PLANNED_INSPECTIONS.value,
|
||||
output_path=spool_path,
|
||||
export_year=timezone.localdate().year,
|
||||
)
|
||||
|
||||
_render_source_group_artifact(
|
||||
|
||||
@@ -146,6 +146,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_another_generation_holds_lock(self):
|
||||
|
||||
Reference in New Issue
Block a user