fix: restore code-less FNS financial reports
Some checks failed
CI/CD Pipeline / Quality Gate (push) Failing after 39s
CI/CD Pipeline / Build and Push Images (push) Has been skipped
CI/CD Pipeline / Internal Notify (push) Successful in 0s
CI/CD Pipeline / Deploy Dev via Compose (push) Has been skipped

This commit is contained in:
2026-08-04 23:25:31 +02:00
parent ef92cc4610
commit 1bc98796ab
6 changed files with 818 additions and 39 deletions

View File

@@ -0,0 +1,95 @@
"""Tests for restoring normalized FNS financial lines."""
from __future__ import annotations
from pathlib import Path
from tempfile import TemporaryDirectory
from apps.parsers.models import ParserLoadLog
from django.core.management import call_command
from django.test import TestCase, override_settings
from openpyxl import Workbook
from organizations.models import (
FinancialIndicatorsExtension,
OrganizationSourceFinancialLine,
OrganizationSourceRecord,
)
from tests.apps.parsers.organization_helpers import create_directory_organization
class RestoreFNSFinancialLinesCommandTests(TestCase):
"""Restore only empty reports and keep reruns idempotent."""
def test_dry_run_then_restores_codeless_processed_workbook(self):
ogrn = "1187700006273"
external_id = "0130160"
organization = create_directory_organization(
name="Тестовая организация",
inn="7700000001",
kpp="770001001",
okpo="12345678",
ogrn=ogrn,
)
extension = FinancialIndicatorsExtension.objects.create(
organization=organization,
title="Финансово-экономические показатели",
)
file_name = f"fin_{external_id}_{ogrn}.xlsx"
record = OrganizationSourceRecord.objects.create(
extension=extension,
source=ParserLoadLog.Source.FNS_REPORTS,
record_type="financial_report",
external_id=external_id,
title=file_name,
status="success",
payload={
"external_id": external_id,
"file_name": file_name,
"lines_count": 0,
"ogrn": ogrn,
},
)
with TemporaryDirectory() as temp_directory:
workbook = Workbook()
worksheet = workbook.active
worksheet.append(["Форма №1", 2020, None, 2021, None])
worksheet.append(
["Бухгалтерский баланс", "Начало", "Конец", "Начало", "Конец"]
)
worksheet.append(["Баланс", 60_232, 234_841, 234_841, 244_479])
workbook.save(Path(temp_directory) / file_name)
with override_settings(FNS_PROCESSED_DIRECTORY=temp_directory):
call_command(
"restore_fns_financial_lines",
"--ogrn",
ogrn,
"--dry-run",
"--silent",
)
self.assertFalse(
OrganizationSourceFinancialLine.objects.filter(
source_record=record
).exists()
)
call_command(
"restore_fns_financial_lines",
"--ogrn",
ogrn,
"--silent",
)
call_command(
"restore_fns_financial_lines",
"--ogrn",
ogrn,
"--silent",
)
lines = OrganizationSourceFinancialLine.objects.filter(source_record=record)
self.assertEqual(lines.count(), 2)
self.assertEqual(set(lines.values_list("year", flat=True)), {2020, 2021})
record.refresh_from_db()
self.assertEqual(record.payload["lines_count"], 2)