"""Reference norm_* profiles: catalog API и CRUD.""" from __future__ import annotations import unittest from app import create_app, db from app.lab.commands.delete_profile import delete_animal_profile from app.lab.commands.import_seed import import_norm_profiles from app.lab.commands.upsert_profile import upsert_animal_profile from app.lab.loaders.profile_loader import list_animal_profiles, load_animal_profile from app.lab.models import LabAnimalProfile from app.lab.seed_paths import norms_dir from app.services.setup_state import mark_setup_complete from tests.helpers.zootech_test_helpers import ZootechTestConfig class ReferenceProfilesTests(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) self.client = self.app.test_client() self.client.post( "/api/auth/login", json={"login": "zootech-test-admin", "password": "zootech-test-secret"}, ) if not (norms_dir() / "Нормы Дойн.csv").is_file(): self.skipTest("seed CSV missing") import_norm_profiles(replace_reference=True) def tearDown(self) -> None: db.session.remove() db.drop_all() self.ctx.pop() def test_list_includes_reference_profiles(self) -> None: profiles = list_animal_profiles("DAIRY") keys = {p["profileKey"] for p in profiles} self.assertIn("norm_dairy_0001", keys) def test_get_reference_profile_by_id(self) -> None: ref = LabAnimalProfile.query.filter_by(profile_key="norm_dairy_0001", is_deleted=False).first() self.assertIsNotNone(ref) loaded = load_animal_profile(ref.id) self.assertEqual(loaded["profileKey"], "norm_dairy_0001") self.assertTrue(loaded["norms"]) def test_can_delete_reference(self) -> None: ref = LabAnimalProfile.query.filter_by(profile_key="norm_dairy_0001", is_deleted=False).first() assert ref is not None delete_animal_profile(ref.id, "tester") gone = LabAnimalProfile.query.filter_by(id=ref.id, is_deleted=False).first() self.assertIsNone(gone) def test_can_update_existing_reference(self) -> None: ref = LabAnimalProfile.query.filter_by(profile_key="norm_dairy_0001", is_deleted=False).first() assert ref is not None upsert_animal_profile( ref.id, { "profileKey": "norm_dairy_0001", "label": "Изменённый справочник", "rationType": "DAIRY", "indicators": {"dry_matter": {"min": 100, "max": 200}}, }, "tester", ) loaded = load_animal_profile(ref.id) self.assertEqual(loaded["norms"]["dry_matter"]["min"], 100) def test_api_animal_profiles_includes_reference(self) -> None: resp = self.client.get("/api/lab/animal-profiles?ration_type=DAIRY") self.assertEqual(resp.status_code, 200) data = resp.get_json() assert data is not None keys = {p["profileKey"] for p in data["profiles"]} self.assertIn("norm_dairy_0001", keys)