Add organizations v2 API and registry enrichment
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import io
|
||||
from datetime import date
|
||||
from unittest.mock import patch
|
||||
|
||||
from apps.registers.services import (
|
||||
ParsedOrganization,
|
||||
@@ -88,6 +89,77 @@ class RegisterImportServiceTest(TestCase):
|
||||
self.assertEqual(organization.in_kpp, 987654321)
|
||||
self.assertEqual(organization.mn_okpo, "87654321")
|
||||
|
||||
def test_update_organization_fields_does_not_clear_existing_kpp_when_missing(self):
|
||||
organization = OrganizationFactory(
|
||||
pn_name="Old",
|
||||
in_kpp=123456789,
|
||||
mn_okpo="12345678",
|
||||
)
|
||||
row = ParsedOrganization(
|
||||
pn_name="Old",
|
||||
mn_ogrn=organization.mn_ogrn,
|
||||
mn_inn=organization.mn_inn,
|
||||
in_kpp=None,
|
||||
mn_okpo="12345678",
|
||||
)
|
||||
|
||||
updated = RegisterImportService._update_organization_fields(
|
||||
organization=organization,
|
||||
row=row,
|
||||
)
|
||||
|
||||
self.assertFalse(updated)
|
||||
organization.refresh_from_db()
|
||||
self.assertEqual(organization.in_kpp, 123456789)
|
||||
|
||||
def test_upsert_organizations_uses_bulk_operations(self):
|
||||
existing = OrganizationFactory(
|
||||
pn_name="Old",
|
||||
mn_ogrn=111,
|
||||
mn_inn=222,
|
||||
in_kpp=333,
|
||||
mn_okpo="12345678",
|
||||
)
|
||||
rows = [
|
||||
ParsedOrganization(
|
||||
pn_name="New",
|
||||
mn_ogrn=111,
|
||||
mn_inn=222,
|
||||
in_kpp=444,
|
||||
mn_okpo="87654321",
|
||||
),
|
||||
ParsedOrganization(
|
||||
pn_name="Created",
|
||||
mn_ogrn=333,
|
||||
mn_inn=444,
|
||||
in_kpp=None,
|
||||
mn_okpo="22222222",
|
||||
),
|
||||
]
|
||||
|
||||
with (
|
||||
patch(
|
||||
"django.db.models.query.QuerySet.get_or_create",
|
||||
side_effect=AssertionError("per-row get_or_create must not be used"),
|
||||
),
|
||||
patch.object(
|
||||
OrganizationFactory._meta.model,
|
||||
"save",
|
||||
side_effect=AssertionError("per-row save must not be used"),
|
||||
),
|
||||
):
|
||||
snapshot_org_ids, created, updated = (
|
||||
RegisterImportService._upsert_organizations(rows)
|
||||
)
|
||||
|
||||
self.assertEqual(created, 1)
|
||||
self.assertEqual(updated, 1)
|
||||
self.assertEqual(len(snapshot_org_ids), 2)
|
||||
existing.refresh_from_db()
|
||||
self.assertEqual(existing.pn_name, "New")
|
||||
self.assertEqual(existing.in_kpp, 444)
|
||||
self.assertEqual(existing.mn_okpo, "87654321")
|
||||
|
||||
def test_get_active_periods_by_org_returns_mapping(self):
|
||||
registry = RegisterFactory()
|
||||
active_period = RegistryMembershipPeriodFactory(
|
||||
|
||||
Reference in New Issue
Block a user