Files
site/WESP_REL/tests/test_cleanup_legacy_profiles.py
T
2026-07-17 12:57:18 +03:00

91 lines
2.9 KiB
Python

"""cleanup_legacy_profiles command."""
from __future__ import annotations
import unittest
from app import create_app, db
from app.lab.commands.cleanup_legacy_profiles import cleanup_legacy_profiles
from app.lab.models import LabAnimalProfile, LabRecipeRation
from app.models.base import default_uuid
from app.services.setup_state import mark_setup_complete
from tests.helpers.zootech_test_helpers import ZootechTestConfig
class CleanupLegacyProfilesTests(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_dry_run_lists_legacy_and_fp(self) -> None:
legacy = LabAnimalProfile(
id=default_uuid(),
profile_key="dairy-1",
label="Legacy",
ration_type="DAIRY",
created_by="test",
updated_by="test",
)
fp = LabAnimalProfile(
id=default_uuid(),
profile_key="fp_dairy_0001",
label="FP",
ration_type="DAIRY",
created_by="test",
updated_by="test",
)
math = LabAnimalProfile(
id=default_uuid(),
profile_key="lab_math_dairy_01",
label="Math",
ration_type="DAIRY",
created_by="test",
updated_by="test",
)
from app.models import Recipe
recipe = Recipe(id=default_uuid(), name="T", heads_per_trip=1, mixing_time=1)
db.session.add(recipe)
db.session.add_all([legacy, fp, math])
db.session.flush()
db.session.add(
LabRecipeRation(
recipe_id=recipe.id,
animal_profile_id=legacy.id,
created_by="test",
updated_by="test",
)
)
db.session.commit()
report = cleanup_legacy_profiles(dry_run=True)
self.assertIn("dairy-1", report.profiles_to_delete)
self.assertIn("fp_dairy_0001", report.profiles_to_delete)
self.assertNotIn("lab_math_dairy_01", report.profiles_to_delete)
self.assertEqual(report.profiles_deleted, 0)
def test_apply_deletes_legacy(self) -> None:
legacy = LabAnimalProfile(
id=default_uuid(),
profile_key="beef-2",
label="Legacy",
ration_type="BEEF",
created_by="test",
updated_by="test",
)
db.session.add(legacy)
db.session.commit()
report = cleanup_legacy_profiles(dry_run=False)
self.assertEqual(report.profiles_deleted, 1)
remaining = LabAnimalProfile.query.filter_by(is_deleted=False).count()
self.assertEqual(remaining, 0)