"""Fixture builders for integration tests (Faker-based).""" from __future__ import annotations import io import zipfile from dataclasses import dataclass from typing import Iterable from faker import Faker from openpyxl import Workbook fake = Faker("ru_RU") @dataclass(frozen=True) class CertificateRow: issue_date: str certificate_number: str expiry_date: str certificate_file_url: str organisation_name: str inn: str ogrn: str @dataclass(frozen=True) class ManufacturerRow: full_legal_name: str inn: str ogrn: str address: str @dataclass(frozen=True) class InspectionRow: registration_number: str inn: str ogrn: str organisation_name: str control_authority: str inspection_type: str inspection_form: str start_date: str end_date: str status: str legal_basis: str result: str @dataclass(frozen=True) class ProcurementRow: purchase_number: str purchase_name: str customer_inn: str customer_kpp: str customer_ogrn: str customer_name: str max_price: str publish_date: str end_date: str status: str href: str def _digits(length: int) -> str: return "".join(str(fake.random_int(0, 9)) for _ in range(length)) def build_minpromtorg_certificates_excel(count: int = 5) -> tuple[bytes, list[CertificateRow]]: wb = Workbook() ws = wb.active ws.append( [ "issue_date", "certificate_number", "expiry_date", "certificate_file_url", "organisation_name", "inn", "ogrn", ] ) rows: list[CertificateRow] = [] for _ in range(count): row = CertificateRow( issue_date=str(fake.date()), certificate_number=f"{fake.bothify(text='??-####-#####')}", expiry_date=str(fake.date()), certificate_file_url=fake.url(), organisation_name=fake.company(), inn=_digits(10), ogrn=_digits(13), ) rows.append(row) ws.append( [ row.issue_date, row.certificate_number, row.expiry_date, row.certificate_file_url, row.organisation_name, row.inn, row.ogrn, ] ) buf = io.BytesIO() wb.save(buf) wb.close() return buf.getvalue(), rows def build_minpromtorg_manufacturers_excel( count: int = 5, ) -> tuple[bytes, list[ManufacturerRow]]: wb = Workbook() ws = wb.active ws.append(["full_legal_name", "inn", "ogrn", "address"]) rows: list[ManufacturerRow] = [] for _ in range(count): row = ManufacturerRow( full_legal_name=fake.company(), inn=_digits(10), ogrn=_digits(13), address=fake.address().replace("\n", ", "), ) rows.append(row) ws.append([row.full_legal_name, row.inn, row.ogrn, row.address]) buf = io.BytesIO() wb.save(buf) wb.close() return buf.getvalue(), rows def build_proverki_xml(count: int = 3) -> tuple[bytes, list[InspectionRow]]: rows: list[InspectionRow] = [] parts = ["", ""] for _ in range(count): row = InspectionRow( registration_number=_digits(12), inn=_digits(10), ogrn=_digits(13), organisation_name=fake.company(), control_authority=fake.company(), inspection_type=fake.word(), inspection_form=fake.word(), start_date=str(fake.date()), end_date=str(fake.date()), status=fake.word(), legal_basis=fake.sentence(nb_words=4), result=fake.sentence(nb_words=3), ) rows.append(row) parts.append( "" ) parts.append("") xml = "".join(parts).encode("utf-8") return xml, rows def build_zakupki_xml(count: int = 3) -> tuple[bytes, list[ProcurementRow]]: rows: list[ProcurementRow] = [] parts = ["", ""] for _ in range(count): row = ProcurementRow( purchase_number=_digits(19), purchase_name=fake.sentence(nb_words=6), customer_inn=_digits(10), customer_kpp=_digits(9), customer_ogrn=_digits(13), customer_name=fake.company(), max_price=str(fake.pydecimal(left_digits=7, right_digits=2, positive=True)), publish_date=str(fake.date()), end_date=str(fake.date()), status=fake.word(), href=fake.url(), ) rows.append(row) parts.append( "" f"{row.purchase_number}" f"{row.purchase_name}" "" f"{row.customer_inn}" f"{row.customer_kpp}" f"{row.customer_ogrn}" f"{row.customer_name}" "" f"{row.max_price}" f"{row.publish_date}" f"{row.end_date}" f"{row.status}" f"{row.href}" "" ) parts.append("") xml = "".join(parts).encode("utf-8") return xml, rows def build_zip(files: Iterable[tuple[str, bytes]]) -> bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: for name, content in files: zf.writestr(name, content) return buf.getvalue() __all__ = [ "fake", "CertificateRow", "ManufacturerRow", "InspectionRow", "ProcurementRow", "build_minpromtorg_certificates_excel", "build_minpromtorg_manufacturers_excel", "build_proverki_xml", "build_zakupki_xml", "build_zip", ]