@@ -0,0 +1,122 @@
|
||||
"""Единый lab import router — все форматы из папки «для разбора»."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from app import create_app, db
|
||||
from app.lab.etl.lab_import_router import detect_source_format, parse_lab_import
|
||||
from app.lab.etl.plinor_pdf_import import is_plinor_sos, is_plinor_zoo
|
||||
from app.services.setup_state import mark_setup_complete
|
||||
from tests.helpers.zootech_test_helpers import ZootechTestConfig
|
||||
|
||||
_REF = Path(__file__).resolve().parents[3] / "для разбора и внедрения "
|
||||
_FIXTURE_XML = Path(__file__).resolve().parent / "fixtures" / "lab" / "agrostar_yama5.xml"
|
||||
|
||||
|
||||
class LabImportRouterTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.app = create_app(ZootechTestConfig)
|
||||
self.ctx = self.app.app_context()
|
||||
self.ctx.push()
|
||||
db.create_all()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
self.ctx.pop()
|
||||
|
||||
def test_detect_formats(self) -> None:
|
||||
self.assertEqual(detect_source_format("a.xml", b"<Standard_XML_Data></Standard_XML_Data>"), "agrostar_xml")
|
||||
self.assertEqual(detect_source_format("b.xlsx", b"PK\x03\x04"), "agrostar_xlsx")
|
||||
self.assertFalse(is_plinor_sos('ПЛИНОР\nТаблица 1.10'))
|
||||
self.assertTrue(is_plinor_zoo('ПЛИНОР\nТаблица 1.10'))
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_detect_pdf_without_extension(self) -> None:
|
||||
pdf = _REF / "сос.pdf"
|
||||
data = pdf.read_bytes()
|
||||
self.assertEqual(detect_source_format("upload.bin", data), "plinor_sos")
|
||||
self.assertEqual(detect_source_format("blob", data), "plinor_sos")
|
||||
zoo = (_REF / "зоо.pdf").read_bytes()
|
||||
self.assertEqual(detect_source_format("", zoo), "plinor_zoo")
|
||||
xml = (_REF / "27-04-2026 4715 АО ПЗ Мельниково.xml").read_bytes()
|
||||
self.assertEqual(detect_source_format("upload.bin", xml), "agrostar_xml")
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_parse_agrostar_xml_bundle(self) -> None:
|
||||
data = (_REF / "27-04-2026 4715 АО ПЗ Мельниково.xml").read_bytes()
|
||||
body = parse_lab_import("bundle.xml", data)
|
||||
self.assertEqual(body["kind"], "lab_samples")
|
||||
self.assertEqual(body["sourceFormat"], "agrostar_xml")
|
||||
self.assertGreaterEqual(body["sampleCount"], 3)
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_parse_agrostar_pdf(self) -> None:
|
||||
pdf = next(_REF.glob("5LDBW*.pdf"), None)
|
||||
if pdf is None:
|
||||
self.skipTest("no agrostar pdf")
|
||||
body = parse_lab_import(pdf.name, pdf.read_bytes())
|
||||
self.assertEqual(body["kind"], "lab_samples")
|
||||
self.assertEqual(body["sourceFormat"], "agrostar_pdf")
|
||||
self.assertEqual(body["sampleCount"], 1)
|
||||
self.assertTrue(body["samples"][0]["nutrients"])
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_parse_agrostar_xlsx(self) -> None:
|
||||
xlsx = _REF / "АО ПЗ Мельниково-Основные корма.xlsx"
|
||||
if not xlsx.is_file():
|
||||
self.skipTest("no xlsx")
|
||||
body = parse_lab_import(xlsx.name, xlsx.read_bytes())
|
||||
self.assertEqual(body["kind"], "lab_samples")
|
||||
self.assertEqual(body["sourceFormat"], "agrostar_xlsx")
|
||||
self.assertGreaterEqual(body["sampleCount"], 3)
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_parse_plinor_sos(self) -> None:
|
||||
body = parse_lab_import("сос.pdf", (_REF / "сос.pdf").read_bytes())
|
||||
self.assertEqual(body["kind"], "ration_composition")
|
||||
self.assertGreaterEqual(body["feedCount"], 10)
|
||||
self.assertTrue(body["feedLines"][0].get("suggestedComponents") is not None)
|
||||
|
||||
@unittest.skipUnless(_REF.is_dir(), "reference folder missing")
|
||||
def test_parse_plinor_zoo(self) -> None:
|
||||
body = parse_lab_import("зоо.pdf", (_REF / "зоо.pdf").read_bytes())
|
||||
self.assertEqual(body["kind"], "ration_indicators")
|
||||
self.assertGreaterEqual(body["indicatorCount"], 5)
|
||||
|
||||
|
||||
class LabImportApiTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.app = create_app(ZootechTestConfig)
|
||||
self.client = self.app.test_client()
|
||||
self.ctx = self.app.app_context()
|
||||
self.ctx.push()
|
||||
db.create_all()
|
||||
mark_setup_complete(self.app)
|
||||
self.client.post(
|
||||
"/api/auth/login",
|
||||
json={"login": "zootech-test-admin", "password": "zootech-test-secret"},
|
||||
)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
self.ctx.pop()
|
||||
|
||||
def test_parse_api_xml(self) -> None:
|
||||
with _FIXTURE_XML.open("rb") as fh:
|
||||
r = self.client.post(
|
||||
"/api/lab/import/parse",
|
||||
data={"file": (fh, "sample.xml")},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
self.assertEqual(r.status_code, 200, r.get_data(as_text=True))
|
||||
body = r.get_json()
|
||||
self.assertEqual(body["kind"], "lab_samples")
|
||||
self.assertIn("preview", body["samples"][0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user