From 27768edcea35e92348d0e32d000f1877c086cca8 Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchriakov Date: Sun, 9 Aug 2026 13:47:25 +0200 Subject: [PATCH] feat: add test finance history --- src/organizations/test_companies.py | 100 ++++++++++++------ .../test_test_companies_commands.py | 70 ++++++++++-- 2 files changed, 132 insertions(+), 38 deletions(-) diff --git a/src/organizations/test_companies.py b/src/organizations/test_companies.py index 787589f..fbd8de7 100644 --- a/src/organizations/test_companies.py +++ b/src/organizations/test_companies.py @@ -33,6 +33,7 @@ from organizations.source_cache import invalidate_source_data_cache from organizations.source_groups import SOURCE_GROUP_DESCRIPTORS TEST_COMPANY_COUNT = 20 +TEST_FINANCIAL_HISTORY_YEARS = 4 TEST_COMPANY_NAMESPACE = UUID("59b36ae9-bcf8-4b7c-b77a-77578f485a01") TEST_RECORD_PREFIX = "mostovik-test-company" @@ -670,44 +671,81 @@ class TestCompanyDatasetService: @staticmethod def _refresh_financial_lines(*, record, legacy_report, index: int) -> None: - report_year = timezone.localdate().year - expected = { - ("1", "1600", "Баланс (актив)", 10_000_000 + index * 100_000), - ("1", "1300", "Капитал и резервы", 4_000_000 + index * 50_000), - ("2", "2110", "Выручка", 20_000_000 + index * 200_000), - ("2", "2400", "Чистая прибыль", 2_000_000 + index * 25_000), - } - expected_keys = [] - for form_code, line_code, line_name, period_end in expected: - expected_keys.append((form_code, line_code, report_year)) - OrganizationSourceFinancialLine.objects.update_or_create( - source_record=record, - form_code=form_code, - line_code=line_code, - year=report_year, - defaults={ + current_year = timezone.localdate().year + report_years = range( + current_year - TEST_FINANCIAL_HISTORY_YEARS + 1, + current_year + 1, + ) + expected = ( + ( + "1", + "1600", + "Баланс (актив)", + 10_000_000 + index * 100_000, + 300_000 + index * 2_000, + ), + ( + "1", + "1300", + "Капитал и резервы", + 4_000_000 + index * 50_000, + 120_000 + index * 1_000, + ), + ( + "2", + "2110", + "Выручка", + 20_000_000 + index * 200_000, + 750_000 + index * 5_000, + ), + ( + "2", + "2400", + "Чистая прибыль", + 2_000_000 + index * 25_000, + 80_000 + index * 500, + ), + ) + expected_keys = set() + for report_year in report_years: + years_before_current = current_year - report_year + for ( + form_code, + line_code, + line_name, + current_period_end, + annual_change, + ) in expected: + period_end = current_period_end - annual_change * years_before_current + expected_keys.add((form_code, line_code, report_year)) + defaults = { "line_name": line_name, - "period_start": period_end - 100_000, + "period_start": period_end - annual_change, "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, - }, - ) + } + OrganizationSourceFinancialLine.objects.update_or_create( + source_record=record, + form_code=form_code, + line_code=line_code, + year=report_year, + defaults=defaults, + ) + FinancialReportLine.objects.update_or_create( + report=legacy_report, + form_code=form_code, + line_code=line_code, + year=report_year, + defaults=defaults, + ) 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() + for line in legacy_report.lines.all(): + key = (line.form_code, line.line_code, line.year) + if key not in expected_keys: + line.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 7ff37cf..3cd31f2 100644 --- a/tests/apps/organizations/test_test_companies_commands.py +++ b/tests/apps/organizations/test_test_companies_commands.py @@ -24,7 +24,10 @@ from organizations.models import ( OrganizationSourceRecord, SourceGroup, ) -from organizations.test_companies import TEST_RECORD_PREFIX +from organizations.test_companies import ( + TEST_FINANCIAL_HISTORY_YEARS, + TEST_RECORD_PREFIX, +) TEST_EXCHANGE_TOKEN = "test-exchange-token" # noqa: S105 @@ -89,7 +92,14 @@ class TestCompaniesCommandsTest(TestCase): OrganizationSourceFinancialLine.objects.filter( source_record__extension__organization__in=companies ).count(), - 20 * 4, + 20 * 4 * TEST_FINANCIAL_HISTORY_YEARS, + ) + current_year = timezone.localdate().year + expected_financial_years = set( + range( + current_year - TEST_FINANCIAL_HISTORY_YEARS + 1, + current_year + 1, + ) ) self.assertEqual( set( @@ -97,7 +107,7 @@ class TestCompaniesCommandsTest(TestCase): source_record__extension__organization__in=companies ).values_list("year", flat=True) ), - {timezone.localdate().year}, + expected_financial_years, ) self.assertEqual(IndustrialCertificateRecord.objects.count(), 20) self.assertEqual(IndustrialProductRecord.objects.count(), 20) @@ -112,10 +122,13 @@ class TestCompaniesCommandsTest(TestCase): for index in range(1, 21) }, ) - self.assertEqual(FinancialReportLine.objects.count(), 20 * 4) + self.assertEqual( + FinancialReportLine.objects.count(), + 20 * 4 * TEST_FINANCIAL_HISTORY_YEARS, + ) self.assertEqual( set(FinancialReportLine.objects.values_list("year", flat=True)), - {timezone.localdate().year}, + expected_financial_years, ) self.assertEqual(GenericParserRecord.objects.count(), 20 * 9) @@ -142,6 +155,29 @@ class TestCompaniesCommandsTest(TestCase): ) financial_report.external_id = "test-company-01" financial_report.save(update_fields=["external_id"]) + fns_record = OrganizationSourceRecord.objects.get( + extension__organization=first_company, + source=ParserLoadLog.Source.FNS_REPORTS, + ) + stale_year = timezone.localdate().year - TEST_FINANCIAL_HISTORY_YEARS + OrganizationSourceFinancialLine.objects.create( + source_record=fns_record, + form_code="1", + line_code="1600", + line_name="Устаревший баланс", + year=stale_year, + period_start=1, + period_end=2, + ) + FinancialReportLine.objects.create( + report=financial_report, + form_code="1", + line_code="1600", + line_name="Устаревший баланс", + year=stale_year, + period_start=1, + period_end=2, + ) call_command("create_test_companies", stdout=StringIO()) @@ -163,6 +199,18 @@ class TestCompaniesCommandsTest(TestCase): financial_report.external_id, f"{TEST_RECORD_PREFIX}:01:{ParserLoadLog.Source.FNS_REPORTS}", ) + self.assertFalse( + OrganizationSourceFinancialLine.objects.filter( + source_record=fns_record, + year=stale_year, + ).exists() + ) + self.assertFalse( + FinancialReportLine.objects.filter( + report=financial_report, + year=stale_year, + ).exists() + ) self.assertEqual(OrganizationSourceRecord.objects.count(), 20 * 15) @override_settings(STATE_CORP_EXCHANGE_TOKEN=TEST_EXCHANGE_TOKEN) @@ -200,14 +248,22 @@ class TestCompaniesCommandsTest(TestCase): }, ) self.assertEqual(len(reports), 20) - self.assertEqual(sum(len(report["lines"]) for report in reports), 80) + self.assertEqual( + sum(len(report["lines"]) for report in reports), + 20 * 4 * TEST_FINANCIAL_HISTORY_YEARS, + ) self.assertEqual( { line["year"] for report in reports for line in report["lines"] }, - {timezone.localdate().year}, + set( + range( + timezone.localdate().year - TEST_FINANCIAL_HISTORY_YEARS + 1, + timezone.localdate().year + 1, + ) + ), ) def test_delete_removes_only_the_fixed_twenty_companies(self):