fix: create organizations from form uploads
All checks were successful
All checks were successful
This commit is contained in:
82
tests/apps/form_1/test_api.py
Normal file
82
tests/apps/form_1/test_api.py
Normal file
@@ -0,0 +1,82 @@
|
||||
"""API tests for form_1 records."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db import connection
|
||||
from django.test import override_settings
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
from tests.apps.form_1.factories import FormF1RecordFactory
|
||||
from tests.apps.organization.factories import OrganizationFactory
|
||||
from tests.apps.user.factories import UserFactory
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="core.urls")
|
||||
class FormF1RecordApiOrderingTest(APITestCase):
|
||||
"""Ordering behavior for monthly F-1 records."""
|
||||
|
||||
def setUp(self):
|
||||
self.user = UserFactory.create_user()
|
||||
self.client.force_authenticate(self.user)
|
||||
self.organization = OrganizationFactory.create(inn="1111111111")
|
||||
|
||||
def test_ordering_appends_report_month_tiebreaker_for_monthly_records(self):
|
||||
FormF1RecordFactory.create(
|
||||
organization=self.organization,
|
||||
report_year=2025,
|
||||
report_month=9,
|
||||
report_quarter=None,
|
||||
)
|
||||
FormF1RecordFactory.create(
|
||||
organization=self.organization,
|
||||
report_year=2025,
|
||||
report_month=12,
|
||||
report_quarter=None,
|
||||
)
|
||||
|
||||
with CaptureQueriesContext(connection) as queries:
|
||||
response = self.client.get(
|
||||
"/api/v1/forms/f1/records/",
|
||||
{
|
||||
"organization_inn": self.organization.inn,
|
||||
"ordering": "-report_year,-report_quarter",
|
||||
"page_size": 100,
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(
|
||||
[record["report_month"] for record in response.data["data"]],
|
||||
[12, 9],
|
||||
)
|
||||
list_query = next(
|
||||
query["sql"]
|
||||
for query in queries.captured_queries
|
||||
if 'FROM "form_1_formf1record"' in query["sql"]
|
||||
and "ORDER BY" in query["sql"]
|
||||
)
|
||||
self.assertIn('"form_1_formf1record"."report_month" DESC', list_query)
|
||||
|
||||
def test_read_returns_form_f1_record_payload_without_response_envelope(self):
|
||||
record = FormF1RecordFactory.create(
|
||||
organization=self.organization,
|
||||
report_year=2025,
|
||||
report_month=12,
|
||||
report_quarter=None,
|
||||
avg_employees=Decimal("785.00"),
|
||||
avg_payroll_employees=Decimal("787.00"),
|
||||
payroll_fund=Decimal("266614.00"),
|
||||
)
|
||||
|
||||
response = self.client.get(f"/api/v1/forms/f1/records/{record.id}/")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data["id"], str(record.id))
|
||||
self.assertEqual(response.data["avg_employees"], "785.00")
|
||||
self.assertEqual(response.data["avg_payroll_employees"], "787.00")
|
||||
self.assertEqual(response.data["payroll_fund"], "266614.00")
|
||||
self.assertNotIn("data", response.data)
|
||||
@@ -92,7 +92,7 @@ class FormF1ParserTest(TestCase):
|
||||
self.assertIn("civilian_output_actual", field_names)
|
||||
|
||||
def test_create_record_uses_existing_organization(self):
|
||||
"""Report imports must attach rows only to preloaded Mostovik organizations."""
|
||||
"""Report imports reuse existing organizations when available."""
|
||||
parser = FormF1Parser(report_year=2026, report_month=9)
|
||||
parser.load_batch = 505
|
||||
organization = OrganizationFactory.create(inn="1234567890")
|
||||
|
||||
Reference in New Issue
Block a user