fix: hide organizations absent from the current full directory
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Failing after 3m16s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped

This commit is contained in:
Aleksandr Meshchryakov
2026-10-29 18:04:24 +01:00
parent bc9767244d
commit c91cd11c86
8 changed files with 138 additions and 4 deletions

View File

@@ -169,6 +169,90 @@ class OrganizationDirectoryImportServiceTest(TestCase):
self.assertEqual(second.inn, "7701001021")
self.assertEqual(second.integrated_structure, "")
def test_full_snapshot_hides_missing_preserves_history_and_restores_returned(self):
from django.urls import reverse
from rest_framework.test import APIClient
from organizations.models import (
GovernmentSupportExtension,
OrganizationSourceRecord,
)
from tests.apps.user.factories import UserFactory
first = self._workbook_path(
[
self._row(rn="10", opk_registry_membership=".T."),
self._row(
rn="20",
inn="7701001002",
ogrn="1027700100002",
opk_registry_membership=".T.",
),
]
)
OrganizationDirectoryImportService.import_xlsx(first)
missing = Organization.objects.get(rn=20)
extension = GovernmentSupportExtension.objects.create(organization=missing)
record = OrganizationSourceRecord.objects.create(
extension=extension,
source="fns_sme_support_recipients",
record_type="sme_support_measure",
external_id="historical",
payload={"history": "kept"},
)
second = self._workbook_path(
[self._row(rn="10", opk_registry_membership=".T.")]
)
OrganizationDirectoryImportService.import_xlsx(second)
missing.refresh_from_db()
self.assertFalse(missing.directory_is_current)
self.assertTrue(missing.opk_registry_membership)
self.assertEqual(Organization.objects.count(), 2)
record.refresh_from_db()
self.assertEqual(record.payload, {"history": "kept"})
client = APIClient()
client.force_authenticate(UserFactory.create_user())
for route, uid in (
("organizations", missing.uid),
("organization-sources", extension.uid),
("organization-source-records", record.uid),
):
response = client.get(
reverse(f"api_v2:organizations:{route}-detail", args=[uid])
)
self.assertEqual(response.status_code, 404, response.data)
listing = client.get(reverse(f"api_v2:organizations:{route}-list"))
self.assertEqual(listing.status_code, 200, listing.data)
self.assertNotIn(str(uid), str(listing.data))
response = client.get(reverse("api_v2:organizations:organizations-list"))
self.assertNotIn(str(missing.uid), str(response.data))
OrganizationDirectoryImportService.import_xlsx(first)
missing.refresh_from_db()
self.assertTrue(missing.directory_is_current)
self.assertEqual(
client.get(
reverse("api_v2:organizations:organizations-detail", args=[missing.uid])
).status_code,
200,
)
self.assertTrue(OrganizationSourceRecord.objects.filter(pk=record.pk).exists())
def test_invalid_or_empty_snapshot_does_not_remove_or_update_current_rows(self):
original = self._workbook_path(
[self._row(rn="10", opk_registry_membership=".T.")]
)
OrganizationDirectoryImportService.import_xlsx(original)
for rows in (
[],
[self._row(rn="10", full_name="Changed"), self._row(rn="bad")],
):
with self.assertRaises(OrganizationDirectoryImportError):
OrganizationDirectoryImportService.import_xlsx(
self._workbook_path(rows)
)
organization = Organization.objects.get(rn=10)
self.assertTrue(organization.directory_is_current)
self.assertEqual(organization.name, 'ООО "По умолчанию"')
def _workbook_path(
self,
rows: list[dict[str, object]],