From 1727e07a02cf0b606ff0619dc8bf547deedb350b Mon Sep 17 00:00:00 2001 From: Joshua Date: Thu, 17 Sep 2026 09:48:27 -0400 Subject: [PATCH] fix: ascending id order in list_definitions_for_child (NQ-71-001) --- custom_components/nestquest/quest_definitions.py | 13 +++++++------ tests/test_quest_definitions.py | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/custom_components/nestquest/quest_definitions.py b/custom_components/nestquest/quest_definitions.py index 739b1da..a38c240 100644 --- a/custom_components/nestquest/quest_definitions.py +++ b/custom_components/nestquest/quest_definitions.py @@ -511,19 +511,20 @@ async def list_definitions_for_child( """Return the definitions assigned to ``child_id`` (rich view). ``child_id`` must be a plain int (bools and floats rejected, since - SQLite would bind ``True`` onto child 1). Ordering follows the DAO's - ``list_by_child`` (newest definition first), so the result is - deterministic. Each entry is a :class:`CreatedQuestDefinition` - snapshot bundling the definition with its decoded rule, assignees and - windows. + SQLite would bind ``True`` onto child 1). Results are ordered by + rising definition id, matching ``list_active_definitions`` and + ``list_definitions_firing_on``. Each entry is a + :class:`CreatedQuestDefinition` snapshot bundling the definition with + its decoded rule, assignees and windows. """ _validate_child_id(child_id) dao = QuestDefinitionsDao(database) definitions = await dao.list_by_child(child_id) - return [ + bundles = [ await _bundle_definition(database, dao, definition) for definition in definitions ] + return sorted(bundles, key=lambda bundle: bundle.definition.id) async def list_definitions_firing_on( diff --git a/tests/test_quest_definitions.py b/tests/test_quest_definitions.py index 3152f8b..81ae99c 100644 --- a/tests/test_quest_definitions.py +++ b/tests/test_quest_definitions.py @@ -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"] ) - # 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) assert [bundle.definition.id for bundle in result] == [ - both.definition.id, ada_only.definition.id, + both.definition.id, ] - assert result[1].rule == _daily_rule() - assert [c.id for c in result[1].assignees] == [child.id] - assert [w.window for w in result[1].windows] == ["morning"] + assert result[0].rule == _daily_rule() + assert [c.id for c in result[0].assignees] == [child.id] + assert [w.window for w in result[0].windows] == ["morning"] bo_result = await list_definitions_for_child(database, bo.id) assert [bundle.definition.id for bundle in bo_result] == [ - bo_only.definition.id, both.definition.id, + bo_only.definition.id, ] return result