Files
mostovik-backend/tests/apps/parsers/factories.py
Aleksandr Meshchriakov 7d4c54636b
Some checks failed
CI/CD Pipeline / Telegram Notify Success (push) Has been cancelled
CI/CD Pipeline / Run Tests (push) Has been cancelled
CI/CD Pipeline / Code Quality Checks (push) Has been cancelled
feat(parsers): sync RU proxies from proxy-tools
2026-03-23 10:47:34 +01:00

213 lines
7.6 KiB
Python

"""Factories for parsers tests (Faker-based)."""
from __future__ import annotations
from datetime import timedelta
import factory
from apps.parsers.models import (
IndustrialCertificateRecord,
IndustrialProductRecord,
InspectionRecord,
ManufacturerRecord,
ParserLoadLog,
ProcurementRecord,
Proxy,
)
from django.utils import timezone
from faker import Faker
fake = Faker("ru_RU")
# === Хелперы для генерации реалистичных данных ===
def _digits(length: int) -> str:
return "".join(str(fake.random_int(0, 9)) for _ in range(length))
def generate_inn_legal() -> str:
"""Генерация ИНН юридического лица (10 цифр)."""
return _digits(10)
def generate_ogrn() -> str:
"""Генерация ОГРН юридического лица (13 цифр)."""
return _digits(13)
def generate_certificate_number() -> str:
"""Генерация номера сертификата промпроизводства."""
prefix = fake.random_element(["ПП", "СПП", "ЗППП"])
year = fake.random_int(min=2020, max=2025)
number = _digits(5)
return f"{prefix}-{year}-{number}"
def generate_company_name() -> str:
"""Генерация названия компании."""
return fake.company()
def generate_legal_address() -> str:
"""Генерация юридического адреса."""
return fake.address().replace("\n", ", ")
def generate_registry_number() -> str:
"""Генерация регистрационного номера записи."""
return f"MPP-{_digits(8)}"
def generate_proxy_address() -> str:
"""Генерация адреса прокси."""
return f"http://{fake.ipv4()}:{fake.port_number()}"
def generate_registration_number() -> str:
"""Генерация номера регистрации проверки."""
return f"{fake.random_int(min=1, max=99)}{fake.random_int(min=2020, max=2025)}{_digits(6)}"
def generate_control_authority() -> str:
"""Генерация названия контролирующего органа."""
return fake.company()
class ProxyFactory(factory.django.DjangoModelFactory):
"""Factory for Proxy model."""
class Meta:
model = Proxy
address = factory.LazyFunction(generate_proxy_address)
description = factory.LazyAttribute(lambda _: fake.sentence(nb_words=3))
source = "manual"
country_code = "RU"
is_active = True
fail_count = 0
last_used_at = factory.LazyAttribute(
lambda _: timezone.now() - timedelta(hours=fake.random_int(min=1, max=72))
)
class ParserLoadLogFactory(factory.django.DjangoModelFactory):
"""Factory for ParserLoadLog model."""
class Meta:
model = ParserLoadLog
batch_id = factory.Sequence(lambda n: n + 1)
source = factory.LazyAttribute(
lambda _: fake.random_element([s[0] for s in ParserLoadLog.Source.choices])
)
records_count = factory.LazyAttribute(lambda _: fake.random_int(min=0, max=5000))
status = "success"
error_message = ""
class ProcurementRecordFactory(factory.django.DjangoModelFactory):
"""Factory for ProcurementRecord model."""
class Meta:
model = ProcurementRecord
load_batch = factory.Sequence(lambda n: n + 1)
purchase_number = factory.LazyAttribute(
lambda _: str(fake.random_number(digits=19, fix_len=True))
)
purchase_name = factory.LazyAttribute(lambda _: fake.sentence(nb_words=6))
customer_inn = factory.LazyFunction(generate_inn_legal)
customer_kpp = factory.LazyAttribute(
lambda _: str(fake.random_number(digits=9, fix_len=True))
)
customer_ogrn = factory.LazyFunction(generate_ogrn)
customer_name = factory.LazyFunction(generate_company_name)
max_price = factory.LazyAttribute(
lambda _: str(fake.random_int(min=10000, max=10000000))
)
currency_code = "RUB"
placement_method = factory.LazyAttribute(lambda _: fake.word())
publish_date = factory.LazyAttribute(lambda _: str(fake.date()))
end_date = factory.LazyAttribute(lambda _: str(fake.date()))
status = factory.LazyAttribute(lambda _: fake.word())
law_type = factory.LazyAttribute(lambda _: fake.random_element(["44-FZ", "223-FZ"]))
purchase_object_info = factory.LazyAttribute(lambda _: fake.sentence(nb_words=8))
href = factory.LazyAttribute(lambda _: fake.url())
region_code = factory.LazyAttribute(
lambda _: str(fake.random_int(min=1, max=99)).zfill(2)
)
data_year = factory.LazyAttribute(lambda _: fake.random_int(min=2020, max=2026))
data_month = factory.LazyAttribute(lambda _: fake.random_int(min=1, max=12))
class IndustrialCertificateRecordFactory(factory.django.DjangoModelFactory):
"""Factory for IndustrialCertificateRecord model."""
class Meta:
model = IndustrialCertificateRecord
load_batch = factory.Sequence(lambda n: n + 1)
issue_date = factory.LazyAttribute(lambda _: str(fake.date()))
certificate_number = factory.LazyFunction(generate_certificate_number)
expiry_date = factory.LazyAttribute(lambda _: str(fake.date()))
certificate_file_url = factory.LazyAttribute(lambda _: fake.url())
organisation_name = factory.LazyFunction(generate_company_name)
inn = factory.LazyFunction(generate_inn_legal)
ogrn = factory.LazyFunction(generate_ogrn)
class ManufacturerRecordFactory(factory.django.DjangoModelFactory):
"""Factory for ManufacturerRecord model."""
class Meta:
model = ManufacturerRecord
load_batch = factory.Sequence(lambda n: n + 1)
full_legal_name = factory.LazyFunction(generate_company_name)
inn = factory.LazyFunction(generate_inn_legal)
ogrn = factory.LazyFunction(generate_ogrn)
address = factory.LazyFunction(generate_legal_address)
class IndustrialProductRecordFactory(factory.django.DjangoModelFactory):
"""Factory for IndustrialProductRecord model."""
class Meta:
model = IndustrialProductRecord
load_batch = factory.Sequence(lambda n: n + 1)
full_organisation_name = factory.LazyFunction(generate_company_name)
ogrn = factory.LazyFunction(generate_ogrn)
inn = factory.LazyFunction(generate_inn_legal)
registry_number = factory.LazyFunction(generate_registry_number)
product_name = factory.LazyAttribute(lambda _: fake.sentence(nb_words=4))
product_model = factory.LazyAttribute(lambda _: fake.bothify(text="MODEL-###"))
okpd2_code = factory.LazyAttribute(
lambda _: f"{fake.random_int(min=10, max=99)}.{fake.random_int(min=10, max=99)}"
)
tnved_code = factory.LazyAttribute(lambda _: _digits(10))
regulatory_document = factory.LazyAttribute(lambda _: fake.sentence(nb_words=5))
class InspectionRecordFactory(factory.django.DjangoModelFactory):
"""Factory for InspectionRecord model."""
class Meta:
model = InspectionRecord
load_batch = factory.Sequence(lambda n: n + 1)
registration_number = factory.LazyFunction(generate_registration_number)
inn = factory.LazyFunction(generate_inn_legal)
ogrn = factory.LazyFunction(generate_ogrn)
organisation_name = factory.LazyFunction(generate_company_name)
control_authority = factory.LazyFunction(generate_control_authority)
inspection_type = factory.LazyAttribute(lambda _: fake.word())
inspection_form = factory.LazyAttribute(lambda _: fake.word())
start_date = factory.LazyAttribute(lambda _: str(fake.date()))
end_date = factory.LazyAttribute(lambda _: str(fake.date()))
status = factory.LazyAttribute(lambda _: fake.word())
legal_basis = factory.LazyAttribute(lambda _: fake.sentence(nb_words=4))
result = factory.LazyAttribute(lambda _: fake.sentence(nb_words=3))