feat: add demo report import workflow
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 2m44s
CI/CD Pipeline / Run Tests (push) Successful in 2m50s
CI/CD Pipeline / Build and Push Dev Images (push) Has been skipped
CI/CD Pipeline / Deploy Dev via Compose (push) Has been skipped

This commit is contained in:
2026-07-16 12:59:23 +02:00
parent 46c023f75a
commit 0c052c5a53
58 changed files with 968 additions and 10 deletions

View File

@@ -0,0 +1,107 @@
"""Tests for deleting the fixed Mostovik demonstration companies."""
from __future__ import annotations
import json
from io import StringIO
from apps.core.models import ReportUpload
from apps.core.report_uploads import ReportUploadService
from apps.external_data.models import IndustrialProduct
from apps.form_1.models import FormF1Record
from apps.organization.models import Organization
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import call_command
from django.test import TestCase
class DeleteTestCompaniesCommandTest(TestCase):
"""The cleanup command must be narrow, cascading, and idempotent."""
def test_deletes_only_exact_test_identities_with_related_data(self):
first = Organization.objects.create(
name="Тестовая компания 1",
inn="7709000010",
)
second = Organization.objects.create(
name="Тестовая компания 2",
inn="7709000028",
)
same_pattern_but_not_test_identity = Organization.objects.create(
name="Тестовая компания 99",
inn="7709000099",
)
same_inn_pattern_but_real_name = Organization.objects.create(
name="Производственная компания",
inn="7709000035",
)
FormF1Record.objects.create(
organization=first,
load_batch=1,
report_year=2025,
report_month=9,
)
IndustrialProduct.objects.create(
organization=second,
product_name="Тестовый комплекс",
product_class="Оборудование",
)
demo_upload = ReportUploadService.create(
uploaded_file=SimpleUploadedFile("Ф-1_2025-09.xlsx", b"demo"),
form="f1",
)
demo_file_name = demo_upload.original_file.name
demo_storage = demo_upload.original_file.storage
unrelated_upload = ReportUploadService.create(
uploaded_file=SimpleUploadedFile("Ф-1.xlsx", b"unrelated"),
form="f1",
)
stdout = StringIO()
with self.captureOnCommitCallbacks(execute=True):
result = call_command("delete_test_companies", stdout=stdout)
payload = json.loads(result)
self.assertEqual(payload["organizations_deleted"], 2)
self.assertEqual(payload["related_objects_deleted"], 2)
self.assertEqual(payload["report_uploads_deleted"], 1)
self.assertFalse(Organization.objects.filter(pk__in=[first.pk, second.pk]))
self.assertTrue(
Organization.objects.filter(pk=same_pattern_but_not_test_identity.pk)
)
self.assertTrue(
Organization.objects.filter(pk=same_inn_pattern_but_real_name.pk)
)
self.assertFalse(FormF1Record.objects.exists())
self.assertFalse(IndustrialProduct.objects.exists())
self.assertFalse(ReportUpload.objects.filter(pk=demo_upload.pk).exists())
self.assertFalse(demo_storage.exists(demo_file_name))
self.assertTrue(ReportUpload.objects.filter(pk=unrelated_upload.pk).exists())
repeated = json.loads(
call_command("delete_test_companies", stdout=StringIO())
)
self.assertEqual(repeated["organizations_deleted"], 0)
self.assertEqual(repeated["related_objects_deleted"], 0)
self.assertEqual(repeated["report_uploads_deleted"], 0)
def test_dry_run_preserves_test_companies(self):
organization = Organization.objects.create(
name="Тестовая компания 1",
inn="7709000010",
)
demo_upload = ReportUploadService.create(
uploaded_file=SimpleUploadedFile("Ф-1_2025-09.xlsx", b"demo"),
form="f1",
)
result = call_command(
"delete_test_companies",
dry_run=True,
stdout=StringIO(),
)
self.assertTrue(Organization.objects.filter(pk=organization.pk).exists())
self.assertTrue(ReportUpload.objects.filter(pk=demo_upload.pk).exists())
self.assertTrue(demo_upload.original_file.storage.exists(demo_upload.original_file.name))
self.assertTrue(json.loads(result)["dry_run"])