fix: ascending id order in list_definitions_for_child (NQ-71-001)

This commit is contained in:
2026-09-17 09:48:27 -04:00
parent 7a9180dc23
commit 1727e07a02
2 changed files with 14 additions and 12 deletions
@@ -511,19 +511,20 @@ async def list_definitions_for_child(
"""Return the definitions assigned to ``child_id`` (rich view). """Return the definitions assigned to ``child_id`` (rich view).
``child_id`` must be a plain int (bools and floats rejected, since ``child_id`` must be a plain int (bools and floats rejected, since
SQLite would bind ``True`` onto child 1). Ordering follows the DAO's SQLite would bind ``True`` onto child 1). Results are ordered by
``list_by_child`` (newest definition first), so the result is rising definition id, matching ``list_active_definitions`` and
deterministic. Each entry is a :class:`CreatedQuestDefinition` ``list_definitions_firing_on``. Each entry is a
snapshot bundling the definition with its decoded rule, assignees and :class:`CreatedQuestDefinition` snapshot bundling the definition with
windows. its decoded rule, assignees and windows.
""" """
_validate_child_id(child_id) _validate_child_id(child_id)
dao = QuestDefinitionsDao(database) dao = QuestDefinitionsDao(database)
definitions = await dao.list_by_child(child_id) definitions = await dao.list_by_child(child_id)
return [ bundles = [
await _bundle_definition(database, dao, definition) await _bundle_definition(database, dao, definition)
for definition in definitions for definition in definitions
] ]
return sorted(bundles, key=lambda bundle: bundle.definition.id)
async def list_definitions_firing_on( async def list_definitions_firing_on(
+7 -6
View File
@@ -1505,20 +1505,21 @@ def test_list_definitions_for_child_returns_only_that_child(tmp_path) -> None:
database, "Walk dog", _daily_rule(), [bo.id], ["afternoon"] database, "Walk dog", _daily_rule(), [bo.id], ["afternoon"]
) )
# DAO list_by_child orders newest-first (id DESC), active or not. # The helper returns the given child's definitions in ascending
# id order, consistent with list_active_definitions.
result = await list_definitions_for_child(database, child.id) result = await list_definitions_for_child(database, child.id)
assert [bundle.definition.id for bundle in result] == [ assert [bundle.definition.id for bundle in result] == [
both.definition.id,
ada_only.definition.id, ada_only.definition.id,
both.definition.id,
] ]
assert result[1].rule == _daily_rule() assert result[0].rule == _daily_rule()
assert [c.id for c in result[1].assignees] == [child.id] assert [c.id for c in result[0].assignees] == [child.id]
assert [w.window for w in result[1].windows] == ["morning"] assert [w.window for w in result[0].windows] == ["morning"]
bo_result = await list_definitions_for_child(database, bo.id) bo_result = await list_definitions_for_child(database, bo.id)
assert [bundle.definition.id for bundle in bo_result] == [ assert [bundle.definition.id for bundle in bo_result] == [
bo_only.definition.id,
both.definition.id, both.definition.id,
bo_only.definition.id,
] ]
return result return result