fix: group JSON source exports by organization
All checks were successful
CI/CD Pipeline / Quality Gate (pull_request) Successful in 1m17s
CI/CD Pipeline / Build and Push Images (pull_request) Successful in 0s
CI/CD Pipeline / Internal Notify (pull_request) Successful in 0s
CI/CD Pipeline / Deploy Dev via Compose (pull_request) Successful in 1s
All checks were successful
CI/CD Pipeline / Quality Gate (pull_request) Successful in 1m17s
CI/CD Pipeline / Build and Push Images (pull_request) Successful in 0s
CI/CD Pipeline / Internal Notify (pull_request) Successful in 0s
CI/CD Pipeline / Deploy Dev via Compose (pull_request) Successful in 1s
This commit is contained in:
@@ -38,6 +38,10 @@ from rest_framework.test import APITestCase
|
||||
from tests.apps.user.factories import UserFactory
|
||||
|
||||
|
||||
def _source_records(groups, source_group):
|
||||
return [record for group in groups for record in group[source_group]]
|
||||
|
||||
|
||||
class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
"""Checks admin-only source-record export contract."""
|
||||
|
||||
@@ -95,6 +99,17 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertEqual(generation.export_year, timezone.localdate().year)
|
||||
self.assertIn('"artifacts_count": 25', command_output.getvalue())
|
||||
|
||||
def test_previous_json_generation_requires_rebuild(self):
|
||||
build_source_record_export_artifacts()
|
||||
manifest_path = Path(self.export_directory.name) / "current.json"
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(manifest["version"], 3)
|
||||
manifest["version"] = 2
|
||||
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
||||
|
||||
with self.assertRaises(SourceRecordExportArtifactsUnavailable):
|
||||
load_current_source_record_export_generation()
|
||||
|
||||
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)
|
||||
@@ -174,7 +189,10 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
if artifact.source_group == SourceGroup.PLANNED_INSPECTIONS.value
|
||||
and artifact.file_format == "json"
|
||||
)
|
||||
inspection_rows = json.loads(inspection_path.read_text(encoding="utf-8"))
|
||||
inspection_rows = _source_records(
|
||||
json.loads(inspection_path.read_text(encoding="utf-8")),
|
||||
SourceGroup.PLANNED_INSPECTIONS.value,
|
||||
)
|
||||
self.assertEqual(
|
||||
{row["uid"] for row in inspection_rows},
|
||||
{str(current_record.pk), str(dateless_current_record.pk)},
|
||||
@@ -185,7 +203,10 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
for artifact in generation.artifacts
|
||||
if artifact.source_group == SourceGroup.FINANCIAL_INDICATORS.value
|
||||
)
|
||||
financial_rows = json.loads(financial_path.read_text(encoding="utf-8"))
|
||||
financial_rows = _source_records(
|
||||
json.loads(financial_path.read_text(encoding="utf-8")),
|
||||
SourceGroup.FINANCIAL_INDICATORS.value,
|
||||
)
|
||||
self.assertEqual(
|
||||
[row["uid"] for row in financial_rows],
|
||||
[str(current_financial_record.pk)],
|
||||
@@ -308,8 +329,12 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertIn("payload.risk.score", rows[0])
|
||||
self.assertIn("payload.tags", rows[0])
|
||||
|
||||
financial_rows = json.loads(
|
||||
archive.read("financial-indicators.json").decode("utf-8"),
|
||||
financial_groups = json.loads(
|
||||
archive.read("financial-indicators.json").decode("utf-8")
|
||||
)
|
||||
self.assertEqual(financial_groups[0]["okpo"], "12345678")
|
||||
financial_rows = _source_records(
|
||||
financial_groups, SourceGroup.FINANCIAL_INDICATORS.value
|
||||
)
|
||||
self.assertEqual(financial_rows[0]["Наименование"], organization.full_name)
|
||||
self.assertEqual(
|
||||
@@ -582,6 +607,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
for file_format in ("json", "csv", "xlsx"):
|
||||
artifact_path = export_directory / f"artifact.{file_format}"
|
||||
_render_source_group_artifact(
|
||||
source_group=SourceGroup.PLANNED_INSPECTIONS.value,
|
||||
row_spool_path=spool_path,
|
||||
output_path=artifact_path,
|
||||
headers=headers,
|
||||
@@ -623,18 +649,30 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
self.assertFalse(queryset.ordered)
|
||||
self.assertFalse(queryset.query.default_ordering)
|
||||
|
||||
def test_json_artifact_reuses_valid_canonical_spool_without_copying_it(self):
|
||||
def test_json_artifact_groups_nonadjacent_records_by_okpo(self):
|
||||
organization = Organization.objects.create(
|
||||
name='ООО "JSON без копии"',
|
||||
inn="7707083813",
|
||||
okpo="00123456",
|
||||
)
|
||||
other_organization = Organization.objects.create(
|
||||
name='ООО "Вторая организация"',
|
||||
inn="7707083814",
|
||||
okpo="87654321",
|
||||
)
|
||||
extension = PlannedInspectionExtension.objects.create(
|
||||
organization=organization,
|
||||
title="Плановые проверки Генпрокуратуры России",
|
||||
)
|
||||
for index in range(2):
|
||||
other_extension = PlannedInspectionExtension.objects.create(
|
||||
organization=other_organization,
|
||||
title="Плановые проверки Генпрокуратуры России",
|
||||
)
|
||||
for index, record_extension in enumerate(
|
||||
(extension, other_extension, extension)
|
||||
):
|
||||
OrganizationSourceRecord.objects.create(
|
||||
extension=extension,
|
||||
extension=record_extension,
|
||||
record_type="inspection",
|
||||
source="inspections",
|
||||
external_id=f"INSP-JSON-{index}",
|
||||
@@ -652,6 +690,7 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
)
|
||||
|
||||
_render_source_group_artifact(
|
||||
source_group=SourceGroup.PLANNED_INSPECTIONS.value,
|
||||
row_spool_path=spool_path,
|
||||
output_path=artifact_path,
|
||||
headers=headers,
|
||||
@@ -659,9 +698,21 @@ class OrganizationSourceRecordExportApiV2Test(APITestCase):
|
||||
records_count=records_count,
|
||||
)
|
||||
|
||||
self.assertEqual(records_count, 2)
|
||||
self.assertEqual(len(json.loads(spool_path.read_text())), 2)
|
||||
self.assertTrue(os.path.samefile(spool_path, artifact_path))
|
||||
self.assertEqual(records_count, 3)
|
||||
self.assertEqual(len(json.loads(spool_path.read_text())), 3)
|
||||
groups = json.loads(artifact_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(
|
||||
{group["okpo"] for group in groups}, {"00123456", "87654321"}
|
||||
)
|
||||
grouped = {
|
||||
group["okpo"]: group[SourceGroup.PLANNED_INSPECTIONS.value]
|
||||
for group in groups
|
||||
}
|
||||
self.assertEqual(len(grouped["00123456"]), 2)
|
||||
self.assertEqual(len(grouped["87654321"]), 1)
|
||||
self.assertNotIn("ОКПО", grouped["00123456"][0])
|
||||
self.assertFalse(os.path.samefile(spool_path, artifact_path))
|
||||
self.assertFalse(list(Path(temporary_directory).glob("*.sqlite3")))
|
||||
|
||||
def test_generation_publishes_complete_matrix_and_keeps_previous_on_failure(self):
|
||||
first_generation = build_source_record_export_artifacts()
|
||||
|
||||
Reference in New Issue
Block a user