126 lines
3.8 KiB
Python
126 lines
3.8 KiB
Python
"""Tests for organization directory resolver."""
|
|
|
|
from django.test import TestCase
|
|
from django.utils import timezone
|
|
from organizations.models import Organization
|
|
from organizations.resolver import OrganizationDirectoryResolver
|
|
|
|
|
|
class OrganizationDirectoryResolverTest(TestCase):
|
|
"""Checks matching parser rows to the organization directory."""
|
|
|
|
@staticmethod
|
|
def _directory_organization(**kwargs) -> Organization:
|
|
kwargs.setdefault("directory_imported_at", timezone.now())
|
|
return Organization.objects.create(**kwargs)
|
|
|
|
def test_resolves_by_rn_okpo_and_exact_pairs(self):
|
|
by_rn = self._directory_organization(name="RN", rn=100)
|
|
by_okpo = self._directory_organization(name="OKPO", okpo="001")
|
|
by_inn_kpp = self._directory_organization(
|
|
name="INN KPP",
|
|
inn="7701001001",
|
|
kpp="770101001",
|
|
)
|
|
by_ogrn_kpp = self._directory_organization(
|
|
name="OGRN KPP",
|
|
ogrn="1027700100001",
|
|
kpp="770101002",
|
|
)
|
|
|
|
self.assertEqual(
|
|
OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(rn="100")
|
|
).organization,
|
|
by_rn,
|
|
)
|
|
self.assertEqual(
|
|
OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(okpo="001")
|
|
).organization,
|
|
by_okpo,
|
|
)
|
|
self.assertEqual(
|
|
OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(
|
|
inn="7701001001",
|
|
kpp="770101001",
|
|
)
|
|
).organization,
|
|
by_inn_kpp,
|
|
)
|
|
self.assertEqual(
|
|
OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(
|
|
ogrn="1027700100001",
|
|
kpp="770101002",
|
|
)
|
|
).organization,
|
|
by_ogrn_kpp,
|
|
)
|
|
|
|
def test_duplicate_inn_ogrn_uses_single_head(self):
|
|
head = self._directory_organization(
|
|
name="Head",
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
is_branch=False,
|
|
)
|
|
self._directory_organization(
|
|
name="Branch",
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
is_branch=True,
|
|
)
|
|
|
|
result = OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
)
|
|
)
|
|
|
|
self.assertEqual(result.status, "matched")
|
|
self.assertEqual(result.organization, head)
|
|
|
|
def test_duplicate_inn_ogrn_without_single_head_is_ambiguous(self):
|
|
self._directory_organization(
|
|
name="Head 1",
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
is_branch=False,
|
|
)
|
|
self._directory_organization(
|
|
name="Head 2",
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
is_branch=False,
|
|
)
|
|
|
|
result = OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(
|
|
inn="7701001001",
|
|
ogrn="1027700100001",
|
|
)
|
|
)
|
|
|
|
self.assertEqual(result.status, "ambiguous")
|
|
self.assertIsNone(result.organization)
|
|
|
|
def test_ignores_legacy_organization_matches(self):
|
|
Organization.objects.create(
|
|
name="Legacy",
|
|
inn="7701001001",
|
|
kpp="770101001",
|
|
)
|
|
|
|
result = OrganizationDirectoryResolver.resolve(
|
|
OrganizationDirectoryResolver.identity(
|
|
inn="7701001001",
|
|
kpp="770101001",
|
|
)
|
|
)
|
|
|
|
self.assertEqual(result.status, "unmatched")
|
|
self.assertIsNone(result.organization)
|