Add initial implementations for forms and organization apps with serializers, factories, and admin configurations
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 5m5s
CI/CD Pipeline / Run Tests (push) Failing after 5m5s
CI/CD Pipeline / Build Docker Images (push) Has been skipped
CI/CD Pipeline / Push to Gitea Registry (push) Has been skipped
CI/CD Pipeline / Deploy to Server (push) Has been skipped

This commit is contained in:
2026-02-17 09:26:08 +01:00
parent fd2adf9ab4
commit 8ed3e1175c
119 changed files with 9091 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for form_1 app."""

View File

@@ -0,0 +1,34 @@
"""Factories for form_1 app."""
import factory
from apps.form_1.models import FormF1Record
from faker import Faker
from tests.apps.organization.factories import OrganizationFactory
fake = Faker("ru_RU")
class FormF1RecordFactory(factory.django.DjangoModelFactory):
"""Factory for FormF1Record model."""
class Meta:
model = FormF1Record
organization = factory.SubFactory(OrganizationFactory)
load_batch = factory.LazyAttribute(lambda _: fake.uuid4())
# Выпуск военной продукции (факт. цены)
military_output_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
military_domestic_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
military_export_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
# Выпуск гражданской продукции (факт. цены)
civilian_output_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
civilian_domestic_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
civilian_export_actual = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=1000000, left_digits=10, right_digits=2))
# Кадры
avg_employees = factory.LazyAttribute(lambda _: fake.random_int(min=10, max=10000))
avg_payroll_employees = factory.LazyAttribute(lambda _: fake.random_int(min=10, max=10000))
payroll_fund = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=10000000, left_digits=12, right_digits=2))

View File

@@ -0,0 +1,48 @@
"""Tests for FormF1 model."""
from django.test import TestCase
from .factories import FormF1RecordFactory
class FormF1RecordModelTest(TestCase):
"""Tests for FormF1Record model."""
def setUp(self):
self.record = FormF1RecordFactory.create()
def test_record_creation(self):
"""Test record creation."""
self.assertIsNotNone(self.record.id)
self.assertIsNotNone(self.record.organization)
self.assertIsNotNone(self.record.load_batch)
def test_record_str_representation(self):
"""Test string representation."""
expected = f"Ф-1: {self.record.organization} ({self.record.load_batch})"
self.assertEqual(str(self.record), expected)
def test_organization_relationship(self):
"""Test organization FK relationship."""
self.assertIsNotNone(self.record.organization.inn)
self.assertIsNotNone(self.record.organization.name)
def test_decimal_fields_precision(self):
"""Test decimal fields precision."""
field = self.record._meta.get_field("military_output_actual")
self.assertEqual(field.max_digits, 18)
self.assertEqual(field.decimal_places, 2)
def test_integer_fields(self):
"""Test integer fields."""
self.assertIsInstance(self.record.avg_employees, int)
self.assertIsInstance(self.record.avg_payroll_employees, int)
def test_load_batch_index(self):
"""Test load_batch has db_index."""
self.assertTrue(self.record._meta.get_field("load_batch").db_index)
def test_timestamps_auto_created(self):
"""Test timestamps are auto-created."""
self.assertIsNotNone(self.record.created_at)
self.assertIsNotNone(self.record.updated_at)

View File

@@ -0,0 +1,87 @@
"""Tests for FormF1 services."""
from io import BytesIO
from unittest.mock import MagicMock, patch
from apps.form_1.services import FormF1Parser, FormF1Service
from django.test import TestCase
from tests.apps.organization.factories import OrganizationFactory
from .factories import FormF1RecordFactory
class FormF1ServiceTest(TestCase):
"""Tests for FormF1Service."""
def test_get_by_organization(self):
"""Test getting records by organization."""
org = OrganizationFactory.create()
FormF1RecordFactory.create(organization=org)
FormF1RecordFactory.create(organization=org)
FormF1RecordFactory.create() # Другая организация
results = FormF1Service.get_by_organization(org.id)
self.assertEqual(results.count(), 2)
def test_get_by_load_batch(self):
"""Test getting records by load batch."""
batch_id = "test-batch-123"
FormF1RecordFactory.create(load_batch=batch_id)
FormF1RecordFactory.create(load_batch=batch_id)
FormF1RecordFactory.create(load_batch="other-batch")
results = FormF1Service.get_by_load_batch(batch_id)
self.assertEqual(results.count(), 2)
def test_delete_by_load_batch(self):
"""Test deleting records by load batch."""
batch_id = "delete-batch"
FormF1RecordFactory.create(load_batch=batch_id)
FormF1RecordFactory.create(load_batch=batch_id)
other = FormF1RecordFactory.create(load_batch="keep-batch")
count = FormF1Service.delete_by_load_batch(batch_id)
self.assertEqual(count, 2)
# Проверяем что другие записи остались
from apps.form_1.models import FormF1Record
self.assertTrue(FormF1Record.objects.filter(pk=other.pk).exists())
class FormF1ParserTest(TestCase):
"""Tests for FormF1Parser."""
def test_get_column_mappings_returns_mappings(self):
"""Test get_column_mappings returns correct mappings."""
parser = FormF1Parser()
mappings = parser.get_column_mappings()
self.assertIsInstance(mappings, list)
self.assertTrue(len(mappings) > 0)
# Проверяем наличие обязательных полей
field_names = [m.field_name for m in mappings]
self.assertIn("inn", field_names)
self.assertIn("military_output_actual", field_names)
self.assertIn("civilian_output_actual", field_names)
def test_create_record_creates_organization(self):
"""Test create_record creates organization if not exists."""
parser = FormF1Parser()
parser.load_batch = "test-batch"
row_data = {
"inn": "1234567890",
"name": "Тестовая организация",
"military_output_actual": 100.0,
"civilian_output_actual": 200.0,
"avg_employees": 50,
}
record = parser.create_record(row_data)
self.assertIsNotNone(record)
self.assertEqual(record.organization.inn, "1234567890")
self.assertEqual(record.military_output_actual, 100.0)