151 lines
5.8 KiB
Python
151 lines
5.8 KiB
Python
"""Tests for organization source extension models."""
|
||
|
||
from django.db import IntegrityError, transaction
|
||
from django.test import TestCase
|
||
from organizations.models import (
|
||
Organization,
|
||
OrganizationSourceExtension,
|
||
OrganizationSourceFinancialLine,
|
||
OrganizationSourceRecord,
|
||
PlannedInspectionExtension,
|
||
SourceGroup,
|
||
)
|
||
|
||
|
||
class OrganizationIdentityFieldsTest(TestCase):
|
||
"""Checks derived organization identity quality fields."""
|
||
|
||
def test_name_only_organization_is_marked_as_missing_identity(self):
|
||
organization = Organization.objects.create(name="Без реквизитов")
|
||
|
||
self.assertEqual(organization.identity_status, Organization.IdentityStatus.MISSING)
|
||
self.assertTrue(organization.primary_identity.startswith("name:"))
|
||
|
||
def test_inn_and_ogrn_organization_is_marked_as_complete_identity(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Полная"',
|
||
inn="7707083893",
|
||
kpp="770701001",
|
||
ogrn="1027700132195",
|
||
)
|
||
|
||
self.assertEqual(organization.identity_status, Organization.IdentityStatus.COMPLETE)
|
||
self.assertEqual(organization.primary_identity, "inn:7707083893:kpp:770701001")
|
||
|
||
def test_single_identifier_organization_is_marked_as_partial_identity(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Частичная"',
|
||
inn="7707083894",
|
||
)
|
||
|
||
self.assertEqual(organization.identity_status, Organization.IdentityStatus.PARTIAL)
|
||
self.assertEqual(organization.primary_identity, "inn:7707083894")
|
||
|
||
|
||
class OrganizationSourceExtensionModelTest(TestCase):
|
||
"""Checks polymorphic source extension behavior and constraints."""
|
||
|
||
def test_polymorphic_query_returns_concrete_extension_instance(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Проверки"',
|
||
inn="7707083895",
|
||
ogrn="1027700132196",
|
||
)
|
||
extension = PlannedInspectionExtension.objects.create(
|
||
organization=organization,
|
||
title="Плановые проверки Генпрокуратуры России",
|
||
records_count=1,
|
||
last_load_batch=77,
|
||
)
|
||
|
||
loaded = OrganizationSourceExtension.objects.get(uid=extension.uid)
|
||
|
||
self.assertIsInstance(loaded, PlannedInspectionExtension)
|
||
self.assertEqual(loaded.source_group, SourceGroup.PLANNED_INSPECTIONS)
|
||
self.assertEqual(loaded.organization, organization)
|
||
|
||
def test_one_source_group_extension_per_organization(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Дубликат"',
|
||
inn="7707083896",
|
||
ogrn="1027700132197",
|
||
)
|
||
PlannedInspectionExtension.objects.create(
|
||
organization=organization,
|
||
title="Плановые проверки Генпрокуратуры России",
|
||
)
|
||
|
||
with self.assertRaises(IntegrityError), transaction.atomic():
|
||
PlannedInspectionExtension.objects.create(
|
||
organization=organization,
|
||
title="Плановые проверки Генпрокуратуры России",
|
||
)
|
||
|
||
def test_source_records_are_unique_by_legacy_model_and_pk(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Запись"',
|
||
inn="7707083897",
|
||
ogrn="1027700132198",
|
||
)
|
||
extension = PlannedInspectionExtension.objects.create(
|
||
organization=organization,
|
||
title="Плановые проверки Генпрокуратуры России",
|
||
)
|
||
OrganizationSourceRecord.objects.create(
|
||
extension=extension,
|
||
record_type="inspection",
|
||
source="inspections",
|
||
external_id="inspection-1",
|
||
title="Проверка 1",
|
||
legacy_model="apps.parsers.InspectionRecord",
|
||
legacy_pk="1",
|
||
payload={"registration_number": "inspection-1"},
|
||
)
|
||
|
||
with self.assertRaises(IntegrityError), transaction.atomic():
|
||
OrganizationSourceRecord.objects.create(
|
||
extension=extension,
|
||
record_type="inspection",
|
||
source="inspections",
|
||
external_id="inspection-2",
|
||
title="Проверка 2",
|
||
legacy_model="apps.parsers.InspectionRecord",
|
||
legacy_pk="1",
|
||
payload={"registration_number": "inspection-2"},
|
||
)
|
||
|
||
def test_financial_lines_attach_to_source_record(self):
|
||
organization = Organization.objects.create(
|
||
name='ООО "Финансы"',
|
||
inn="7707083898",
|
||
ogrn="1027700132199",
|
||
)
|
||
extension = OrganizationSourceExtension.objects.create(
|
||
organization=organization,
|
||
source_group=SourceGroup.FINANCIAL_INDICATORS,
|
||
title="Финансово-экономические показатели",
|
||
)
|
||
record = OrganizationSourceRecord.objects.create(
|
||
extension=extension,
|
||
record_type="financial_report",
|
||
source="fns_reports",
|
||
external_id="report-1",
|
||
title="Отчетность ФНС",
|
||
legacy_model="apps.parsers.FinancialReport",
|
||
legacy_pk="1",
|
||
payload={"external_id": "report-1"},
|
||
)
|
||
|
||
OrganizationSourceFinancialLine.objects.create(
|
||
source_record=record,
|
||
form_code="1",
|
||
line_code="1100",
|
||
line_name="Нематериальные активы",
|
||
year=2025,
|
||
period_start=100,
|
||
period_end=200,
|
||
)
|
||
|
||
self.assertEqual(record.financial_lines.count(), 1)
|
||
self.assertEqual(record.financial_lines.get().period_end, 200)
|