From b360164dad8b40676eddcdd388c0a67c302d92b6 Mon Sep 17 00:00:00 2001 From: Joshua Date: Thu, 17 Sep 2026 14:03:06 -0400 Subject: [PATCH] test: three-run idempotency with stable instance ids --- tests/test_materialize.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/tests/test_materialize.py b/tests/test_materialize.py index 86dfe31..48c5c06 100644 --- a/tests/test_materialize.py +++ b/tests/test_materialize.py @@ -207,15 +207,33 @@ def test_materialize_is_idempotent(tmp_path) -> None: [child.id], ["morning"], ) + dao = QuestInstancesDao(database) first = await materialize(database, start_iso, end_iso) - second = await materialize(database, start_iso, end_iso) - assert first == second == 7 - # Running twice refreshes the SAME seven rows: exactly one - # physical row per (definition_id, child_id, due_date, window) - # key, never a duplicate. - records = await QuestInstancesDao(database).list_by_date_range( - child.id, start_iso, end_iso + assert first == 7 + first_ids = sorted( + r.id + for r in await dao.list_by_date_range(child.id, start_iso, end_iso) ) + assert len(first_ids) == 7 + assert len(set(first_ids)) == 7 + + # Re-running over the same range refreshes the SAME physical rows: + # the ON CONFLICT path updates the snapshot columns in place and + # never delete + reinserts, so every primary-key id is stable. + for _ in range(2): + count = await materialize(database, start_iso, end_iso) + assert count == 7 + run_ids = sorted( + r.id + for r in await dao.list_by_date_range( + child.id, start_iso, end_iso + ) + ) + assert run_ids == first_ids + + # And the widened key never duplicated: exactly one physical row + # per (definition_id, child_id, due_date, window). + records = await dao.list_by_date_range(child.id, start_iso, end_iso) keys = [ (r.definition_id, r.child_id, r.due_date, r.window) for r in records