56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
"""ETL lab: fixtures без PostgreSQL."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import unittest
|
|
|
|
from app import create_app, db
|
|
from app.lab.etl.reference_db_import import (
|
|
apply_animal_profile_rows,
|
|
apply_feed_ingredient_rows,
|
|
)
|
|
from app.lab.models import LabAnimalProfile, LabProfileNorm
|
|
from app.lab.services.component_nutrients import nutrients_api_dict
|
|
from app.services.setup_state import mark_setup_complete
|
|
from tests.helpers.lab_test_helpers import load_fixture, make_component
|
|
from tests.helpers.zootech_test_helpers import ZootechTestConfig
|
|
|
|
|
|
class LabEtlImportTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.app = create_app(ZootechTestConfig)
|
|
self.ctx = self.app.app_context()
|
|
self.ctx.push()
|
|
db.create_all()
|
|
mark_setup_complete(self.app)
|
|
|
|
def tearDown(self) -> None:
|
|
db.session.remove()
|
|
db.drop_all()
|
|
self.ctx.pop()
|
|
|
|
def test_enrich_component_from_fixture(self) -> None:
|
|
comp = make_component()
|
|
stats = apply_feed_ingredient_rows(load_fixture("feed_ingredients.json"))
|
|
db.session.commit()
|
|
db.session.refresh(comp)
|
|
self.assertEqual(stats.components_enriched, 1)
|
|
nutrients = nutrients_api_dict(comp.id)
|
|
self.assertIn("Сыр. Протеин", nutrients)
|
|
|
|
def test_import_animal_profiles_fixture(self) -> None:
|
|
stats = apply_animal_profile_rows(load_fixture("animal_profiles.json"))
|
|
db.session.commit()
|
|
self.assertEqual(stats.animal_profiles, 1)
|
|
profile = LabAnimalProfile.query.get("prof-beef-001")
|
|
self.assertIsNotNone(profile)
|
|
self.assertEqual(profile.profile_key, "beef_grow_400")
|
|
norm = LabProfileNorm.query.filter_by(
|
|
profile_id=profile.id, indicator_key="dry_matter"
|
|
).first()
|
|
self.assertIsNotNone(norm)
|
|
self.assertEqual(norm.min_value, 4000)
|
|
self.assertEqual(norm.max_value, 5000)
|
|
|