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_5 app."""

View File

@@ -0,0 +1,44 @@
"""Factories for form_5 app."""
import factory
from apps.form_5.models import FormF5Record
from faker import Faker
from tests.apps.organization.factories import OrganizationFactory
fake = Faker("ru_RU")
class FormF5RecordFactory(factory.django.DjangoModelFactory):
"""Factory for FormF5Record model."""
class Meta:
model = FormF5Record
organization = factory.SubFactory(OrganizationFactory)
load_batch = factory.LazyAttribute(lambda _: fake.uuid4())
# Идентификация
equipment_id = factory.LazyAttribute(lambda _: fake.numerify("EQ-######"))
inventory_number = factory.LazyAttribute(lambda _: fake.numerify("INV-########"))
name = factory.LazyAttribute(lambda _: fake.word().capitalize() + " " + fake.word())
model = factory.LazyAttribute(lambda _: fake.bothify("??-####"))
# Производитель
manufacturer = factory.LazyAttribute(lambda _: fake.company())
country_origin = factory.LazyAttribute(lambda _: fake.country())
is_domestic = factory.LazyAttribute(lambda _: fake.boolean())
# Характеристики
year_manufacture = factory.LazyAttribute(lambda _: fake.random_int(min=1990, max=2024))
has_cnc = factory.LazyAttribute(lambda _: fake.boolean())
equipment_type = factory.LazyAttribute(lambda _: fake.random_element(["Токарный", "Фрезерный", "Шлифовальный", "Сверлильный"]))
# Состояние
utilization_rate = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=100, left_digits=3, right_digits=2))
physical_wear_percent = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=0, max_value=100, left_digits=3, right_digits=2))
is_operational = factory.LazyAttribute(lambda _: fake.boolean(chance_of_getting_true=85))
# Стоимость
initial_cost = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=100000, max_value=100000000, left_digits=12, right_digits=2))
residual_value = factory.LazyAttribute(lambda _: fake.pydecimal(min_value=10000, max_value=50000000, left_digits=12, right_digits=2))

View File

@@ -0,0 +1,48 @@
"""Tests for FormF5 model."""
from django.test import TestCase
from .factories import FormF5RecordFactory
class FormF5RecordModelTest(TestCase):
"""Tests for FormF5Record model."""
def setUp(self):
self.record = FormF5RecordFactory.create()
def test_record_creation(self):
"""Test record creation."""
self.assertIsNotNone(self.record.id)
self.assertIsNotNone(self.record.organization)
self.assertIsNotNone(self.record.equipment_id)
def test_record_str_representation(self):
"""Test string representation."""
expected = f"Ф-5: {self.record.organization} - {self.record.equipment_id}"
self.assertEqual(str(self.record), expected)
def test_organization_relationship(self):
"""Test organization FK relationship."""
self.assertIsNotNone(self.record.organization.inn)
def test_string_fields_max_length(self):
"""Test string fields max length."""
self.assertEqual(self.record._meta.get_field("equipment_id").max_length, 50)
self.assertEqual(self.record._meta.get_field("name").max_length, 500)
def test_boolean_fields(self):
"""Test boolean fields."""
self.assertIsInstance(self.record.is_domestic, bool)
self.assertIsInstance(self.record.has_cnc, bool)
self.assertIsInstance(self.record.is_operational, bool)
def test_decimal_fields_precision(self):
"""Test decimal fields precision."""
field = self.record._meta.get_field("initial_cost")
self.assertEqual(field.max_digits, 18)
self.assertEqual(field.decimal_places, 2)
def test_load_batch_index(self):
"""Test load_batch has db_index."""
self.assertTrue(self.record._meta.get_field("load_batch").db_index)

View File

@@ -0,0 +1,79 @@
"""Tests for FormF5 services."""
from apps.form_5.services import FormF5Parser, FormF5Service
from django.test import TestCase
from tests.apps.organization.factories import OrganizationFactory
from .factories import FormF5RecordFactory
class FormF5ServiceTest(TestCase):
"""Tests for FormF5Service."""
def test_get_by_organization(self):
"""Test getting records by organization."""
org = OrganizationFactory.create()
FormF5RecordFactory.create(organization=org)
FormF5RecordFactory.create(organization=org)
FormF5RecordFactory.create()
results = FormF5Service.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-f5"
FormF5RecordFactory.create(load_batch=batch_id)
FormF5RecordFactory.create(load_batch=batch_id)
results = FormF5Service.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-f5"
FormF5RecordFactory.create(load_batch=batch_id)
FormF5RecordFactory.create(load_batch=batch_id)
other = FormF5RecordFactory.create(load_batch="keep-batch")
count = FormF5Service.delete_by_load_batch(batch_id)
self.assertEqual(count, 2)
from apps.form_5.models import FormF5Record
self.assertTrue(FormF5Record.objects.filter(pk=other.pk).exists())
class FormF5ParserTest(TestCase):
"""Tests for FormF5Parser."""
def test_get_column_mappings_returns_mappings(self):
"""Test get_column_mappings returns correct mappings."""
parser = FormF5Parser()
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("equipment_id", field_names)
self.assertIn("name", field_names)
def test_create_record_creates_organization(self):
"""Test create_record creates organization if not exists."""
parser = FormF5Parser()
parser.load_batch = "test-batch"
row_data = {
"inn": "5234567890",
"name": "Тестовая организация Ф-5",
"equipment_id": "EQ-001",
"equipment_name": "Токарный станок",
}
record = parser.create_record(row_data)
self.assertIsNotNone(record)
self.assertEqual(record.organization.inn, "5234567890")