From 56181555032859f79613afe50376e692a53f1eda Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchriakov Date: Sun, 9 Aug 2026 13:10:57 +0200 Subject: [PATCH] fix: mirror current-year test finance lines --- src/organizations/test_companies.py | 29 ++++++++++++++++--- .../test_test_companies_commands.py | 22 ++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/organizations/test_companies.py b/src/organizations/test_companies.py index 09353cc..a31bc98 100644 --- a/src/organizations/test_companies.py +++ b/src/organizations/test_companies.py @@ -10,6 +10,7 @@ from uuid import UUID, uuid5 from apps.parsers.models import ( FinancialReport, + FinancialReportLine, GenericParserRecord, IndustrialCertificateRecord, IndustrialProductRecord, @@ -130,7 +131,11 @@ class TestCompanyDatasetService: record.legacy_pk = str(legacy_record.pk) record.save(update_fields=["legacy_model", "legacy_pk", "updated_at"]) if source == ParserLoadLog.Source.FNS_REPORTS: - cls._refresh_financial_lines(record=record, index=index) + cls._refresh_financial_lines( + record=record, + legacy_report=legacy_record, + index=index, + ) OrganizationSourceRecord.objects.filter( extension__organization=organization, @@ -423,6 +428,7 @@ class TestCompanyDatasetService: @classmethod def _record_defaults(cls, *, source: str, index: int, organization): + report_year = timezone.localdate().year common = { "inn": organization.inn, "ogrn": organization.ogrn, @@ -433,8 +439,11 @@ class TestCompanyDatasetService: url = f"https://example.test/{source}/{index}" values = { ParserLoadLog.Source.FNS_REPORTS: { - "title": f"Бухгалтерская отчетность за 2025 год — {organization.name}", - "record_date": "2025", + "title": ( + f"Бухгалтерская отчетность за {report_year} год — " + f"{organization.name}" + ), + "record_date": str(report_year), "status": "processed", "payload": { **common, @@ -660,7 +669,7 @@ class TestCompanyDatasetService: } @staticmethod - def _refresh_financial_lines(*, record, index: int) -> None: + def _refresh_financial_lines(*, record, legacy_report, index: int) -> None: report_year = timezone.localdate().year expected = { ("1", "1600", "Баланс (актив)", 10_000_000 + index * 100_000), @@ -682,11 +691,23 @@ class TestCompanyDatasetService: "period_end": period_end, }, ) + FinancialReportLine.objects.update_or_create( + report=legacy_report, + form_code=form_code, + line_code=line_code, + year=report_year, + defaults={ + "line_name": line_name, + "period_start": period_end - 100_000, + "period_end": period_end, + }, + ) lines = OrganizationSourceFinancialLine.objects.filter(source_record=record) for line in lines: key = (line.form_code, line.line_code, line.year) if key not in expected_keys: line.delete() + legacy_report.lines.exclude(year=report_year).delete() @staticmethod def _refresh_extension_counters(*, organization) -> None: diff --git a/tests/apps/organizations/test_test_companies_commands.py b/tests/apps/organizations/test_test_companies_commands.py index 84ca974..2158f70 100644 --- a/tests/apps/organizations/test_test_companies_commands.py +++ b/tests/apps/organizations/test_test_companies_commands.py @@ -5,6 +5,7 @@ from io import StringIO from apps.exchange.state_corp_services import StateCorpExchangeService from apps.parsers.models import ( FinancialReport, + FinancialReportLine, GenericParserRecord, IndustrialCertificateRecord, IndustrialProductRecord, @@ -103,6 +104,11 @@ class TestCompaniesCommandsTest(TestCase): self.assertEqual(InspectionRecord.objects.count(), 20) self.assertEqual(ProcurementRecord.objects.count(), 20) self.assertEqual(FinancialReport.objects.count(), 20) + self.assertEqual(FinancialReportLine.objects.count(), 20 * 4) + self.assertEqual( + set(FinancialReportLine.objects.values_list("year", flat=True)), + {timezone.localdate().year}, + ) self.assertEqual(GenericParserRecord.objects.count(), 20 * 9) def test_create_updates_the_fixed_dataset_without_duplicates(self): @@ -151,6 +157,12 @@ class TestCompaniesCommandsTest(TestCase): .values_list("inn", flat=True) ) package = StateCorpExchangeService.build_package(organization_inns=company_inns) + reports = StateCorpExchangeService._serialize_financial_reports( + { + organization.ogrn: organization.inn + for organization in Organization.objects.filter(inn__in=company_inns) + } + ) self.assertEqual( package.payload_counts, @@ -169,6 +181,16 @@ class TestCompaniesCommandsTest(TestCase): "labor_vacancies": 20, }, ) + self.assertEqual(len(reports), 20) + self.assertEqual(sum(len(report["lines"]) for report in reports), 80) + self.assertEqual( + { + line["year"] + for report in reports + for line in report["lines"] + }, + {timezone.localdate().year}, + ) def test_delete_removes_only_the_fixed_twenty_companies(self): untouched = Organization.objects.create(