"""Real-Postgres tests for `rules.enforcement` and `rules.salience` (migration 0128). The CHECKs are what a mock cannot show: an unknown value refused, a preference refused anything but `inform`, and a row written without either reading back as the defaults every existing rule took on upgrade. """ import pytest import pytest_asyncio from sqlalchemy import select from sqlalchemy.exc import IntegrityError from grimoire.models import async_session from grimoire.models.rulebook import Rule, Rulebook from grimoire.services import rulebooks as rulebooks_svc from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] OWNER_USERNAME = "rule_enforcement_owner" @pytest_asyncio.fixture async def topic_id(): # Purged at setup, not teardown: the write path's detached embedding task # would deadlock against a teardown deleting the rulebook. async with async_session() as s: owner = await ensure_user(s, OWNER_USERNAME) uid = owner.id await s.commit() for book in (await s.execute( select(Rulebook).where(Rulebook.owner_user_id == uid) )).scalars().all(): await s.delete(book) await s.commit() book = await rulebooks_svc.create_rulebook(uid, "Enforcement fixtures") topic = await rulebooks_svc.create_topic(book.id, uid, "conventions") return uid, topic.id async def _write_raw(topic: int, **kw) -> int: """Straight to the model, around the service's own checks, to reach the CHECKs.""" async with async_session() as s: rule = Rule(topic_id=topic, title=kw.pop("title", "t"), statement="s", **kw) s.add(rule) await s.commit() return rule.id async def _read(rule_id: int) -> Rule: async with async_session() as s: return (await s.execute(select(Rule).where(Rule.id == rule_id))).scalar_one() async def test_a_rule_written_without_either_reads_back_as_the_defaults(topic_id): _uid, topic = topic_id rule = await _read(await _write_raw(topic)) assert (rule.enforcement, rule.salience) == ("inform", "normal") async def test_an_unknown_enforcement_is_refused_by_the_database(topic_id): _uid, topic = topic_id with pytest.raises(IntegrityError): await _write_raw(topic, enforcement="block") async def test_an_unknown_salience_is_refused_by_the_database(topic_id): _uid, topic = topic_id with pytest.raises(IntegrityError): await _write_raw(topic, salience="urgent") async def test_a_preference_that_holds_is_refused_by_the_database(topic_id): _uid, topic = topic_id with pytest.raises(IntegrityError): await _write_raw(topic, kind="preference", enforcement="hold") async def test_the_service_writes_a_declared_hold_and_reads_it_back(topic_id): uid, topic = topic_id rule = await rulebooks_svc.create_rule( topic, uid, "Holds", "Read before replying", when_to_apply="about to reply", enforcement="hold", salience="high", ) back = await _read(rule.id) assert (back.enforcement, back.salience) == ("hold", "high") assert rulebooks_svc.rule_brief(back)["enforcement"] == "hold" assert rulebooks_svc.rule_brief(back)["salience"] == "high" async def test_the_service_refuses_a_preference_that_holds(topic_id): uid, topic = topic_id with pytest.raises(ValueError, match="always `inform`"): await rulebooks_svc.create_rule( topic, uid, "Pref", "Prefer x", when_to_apply="when x", kind="preference", enforcement="hold", ) async def test_a_rule_turned_into_a_preference_drops_to_inform(topic_id): uid, topic = topic_id rule = await rulebooks_svc.create_rule( topic, uid, "Becomes a preference", "Do x", when_to_apply="when x", enforcement="enforce", ) updated = await rulebooks_svc.update_rule(rule.id, uid, kind="preference") assert (updated.kind, updated.enforcement) == ("preference", "inform") async def test_naming_a_hold_alongside_the_preference_kind_is_refused(topic_id): uid, topic = topic_id rule = await rulebooks_svc.create_rule( topic, uid, "Stays a rule", "Do y", when_to_apply="when y", ) with pytest.raises(ValueError, match="always `inform`"): await rulebooks_svc.update_rule(rule.id, uid, kind="preference", enforcement="hold") assert (await _read(rule.id)).kind == "rule"