Files
mostovik-backend/tests/apps/organizations/test_canonical_search.py
Aleksandr Meshchryakov 49cbfd265c
All checks were successful
Mostovik Backend CI/CD / Tests and lint (push) Successful in 9m37s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Successful in 4m18s
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Mostovik Backend CI/CD / Deploy customer main (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Successful in 1m48s
feat(registries): add SME and budget imports and fix source API workflows
2026-09-13 23:13:47 +02:00

141 lines
5.6 KiB
Python

"""Organization search must use the same canonical identity in every table."""
from django.core.cache import cache
from django.urls import reverse
from organizations.models import (
Organization,
OrganizationSourceRecord,
PlannedInspectionExtension,
)
from rest_framework.test import APITestCase
from tests.apps.user.factories import UserFactory
class CanonicalOrganizationSearchTest(APITestCase):
def setUp(self):
cache.clear()
self.client.force_authenticate(UserFactory.create_user())
self.organization = Organization.objects.create(
name="Росатом",
full_name="Полное наименование Росатома",
short_name="Краткое",
inn="7707083881",
kpp="770701081",
ogrn="1027700132181",
okpo="00003881",
opk_registry_membership=True,
)
extension = PlannedInspectionExtension.objects.create(
organization=self.organization,
title="Проверки",
)
for index in range(2):
OrganizationSourceRecord.objects.create(
extension=extension,
source="inspections",
record_type="inspection",
external_id=f"target-{index}",
status="active",
)
other = Organization.objects.create(name="Другая", opk_registry_membership=True)
other_extension = PlannedInspectionExtension.objects.create(
organization=other,
title="Росатом",
)
OrganizationSourceRecord.objects.create(
extension=other_extension,
source="inspections",
record_type="inspection",
external_id="Росатом",
title="Росатом",
status="active",
url="https://example.test/Росатом",
payload={"raw_text": "Росатом"},
)
self.organizations_url = reverse("api_v2:organizations:organizations-list")
self.records_url = reverse(
"api_v2:organizations:organization-source-records-list"
)
def test_all_canonical_names_and_identifiers_are_searchable_in_both_endpoints(self):
for search in (
"Росатом",
"Полное",
"Краткое",
"7707083881",
"770701081",
"1027700132181",
"00003881",
):
with self.subTest(search=search):
organizations = self.client.get(
self.organizations_url, {"search": search}
)
records = self.client.get(self.records_url, {"search": search})
self.assertEqual(organizations.status_code, 200)
self.assertEqual(records.status_code, 200)
self.assertEqual(
organizations.data["meta"]["pagination"]["total_count"], 1
)
self.assertEqual(records.data["meta"]["pagination"]["total_count"], 2)
self.assertEqual(
organizations.data["data"][0]["uid"], str(self.organization.uid)
)
def test_entrepreneur_identifier_is_searchable_in_both_endpoints(self):
entrepreneur = Organization.objects.create(
name="ИП Иванов",
inn="500100732259",
ogrip="304500116000181",
opk_registry_membership=True,
)
extension = PlannedInspectionExtension.objects.create(
organization=entrepreneur,
title="Проверки",
)
OrganizationSourceRecord.objects.create(
extension=extension,
source="inspections",
record_type="inspection",
external_id="entrepreneur-record",
)
for url in (self.organizations_url, self.records_url):
response = self.client.get(url, {"search": "304500116000181"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["meta"]["pagination"]["total_count"], 1)
def test_search_terms_and_source_filters_are_applied_before_pagination(self):
extension = self.organization.source_extensions.get()
OrganizationSourceRecord.objects.create(
extension=extension,
source="other-source",
record_type="inspection",
external_id="filtered-out",
status="active",
)
params = {
"search": "Росатом 7707083881",
"source_group": "planned_inspections",
"source": "inspections",
"record_type": "inspection",
"status": "active",
"ordering": "external_id",
"page_size": 1,
}
first = self.client.get(self.records_url, {**params, "page": 1})
second = self.client.get(self.records_url, {**params, "page": 2})
self.assertEqual(first.status_code, 200)
self.assertEqual(second.status_code, 200)
self.assertEqual(first.data["meta"]["pagination"]["total_count"], 2)
self.assertEqual(first.data["data"][0]["external_id"], "target-0")
self.assertEqual(second.data["data"][0]["external_id"], "target-1")
def test_technical_identity_is_not_a_search_field(self):
Organization.objects.filter(pk=self.organization.pk).update(
primary_identity="technical-only"
)
response = self.client.get(self.organizations_url, {"search": "technical-only"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["meta"]["pagination"]["total_count"], 0)