"""content_hash пересчитывается перед flush для sync (before_flush).""" import os import tempfile import unittest import uuid from app import create_app, db from app.models import Recipe from app.services.sync_content_hash import compute_content_hash_for_object from config import TestingConfig class ContentHashTestConfig(TestingConfig): _TMP_DIR = tempfile.mkdtemp(prefix="wesp-content-hash-tests-") SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(_TMP_DIR, 'recipes_test.db')}" SQLALCHEMY_BINDS = {"reports": f"sqlite:///{os.path.join(_TMP_DIR, 'reports_test.db')}"} class SyncContentHashTests(unittest.TestCase): def setUp(self) -> None: self.app = create_app(ContentHashTestConfig) 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_recipe_content_hash_set_on_insert(self) -> None: rid = str(uuid.uuid4()) r = Recipe(id=rid, name="Тестовый рацион") db.session.add(r) db.session.flush() self.assertTrue(len(r.content_hash) == 64, r.content_hash) h1 = r.content_hash expected = compute_content_hash_for_object(r) self.assertEqual(h1, expected) def test_recipe_content_hash_updates_when_data_changes(self) -> None: rid = str(uuid.uuid4()) r = Recipe(id=rid, name="До") db.session.add(r) db.session.flush() h_before = r.content_hash r.name = "После" db.session.flush() self.assertNotEqual(h_before, r.content_hash) self.assertEqual(r.content_hash, compute_content_hash_for_object(r))