Debian Codegraph @

tests/functional_tests/helpers.py

raw
1 """The waits that know what Harlequin's widgets look like, over `tests.waiting`."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 from harlequin.app import Harlequin
8 from harlequin.catalog import Catalog, CatalogItem
9 from harlequin.components import ErrorModal
10 from harlequin.components.code_editor import CodeEditor
11 from harlequin.components.data_catalog.database_tree import DatabaseTree
12 from harlequin.components.results_viewer import ResultsTable
13 from tests.waiting import wait_for, wait_for_value
14
15 if TYPE_CHECKING:
16 from textual.pilot import Pilot
17 from textual.widgets._tree import TreeNode
18
19
20 async def wait_for_editor(pilot: Pilot, app: Harlequin) -> CodeEditor:
21 """The Query Editor, once the app has one.
22
23 The editor collection is mounted lazily, so an app that is running does not
24 yet have an editor to type into.
25 """
26 return await wait_for_value(
27 pilot, lambda: app.editor, description="the Query Editor to be mounted"
28 )
29
30
31 async def wait_for_any_table(pilot: Pilot, app: Harlequin) -> ResultsTable:
32 """The table the Results Viewer is showing, once a query has filled one in.
33
34 Any table in the active pane: a re-run clears the panes without awaiting
35 the removal, so this can still hand back the previous query's table. A test
36 that runs a second query gates on `results_viewer.tab_count` instead.
37 """
38 return await wait_for_value(
39 pilot,
40 app.results_viewer.get_visible_table,
41 description="the Results Viewer to show a table",
42 )
43
44
45 async def wait_for_error_modal(pilot: Pilot, app: Harlequin) -> ErrorModal:
46 """The error modal on top of the stack, once the app has raised one."""
47 return await wait_for_value(
48 pilot,
49 lambda: app.screen if isinstance(app.screen, ErrorModal) else None,
50 description="an error modal",
51 )
52
53
54 async def wait_for_catalog_tree(pilot: Pilot, app: Harlequin) -> DatabaseTree:
55 """The Data Catalog's database tree, once it has rendered what it found.
56
57 The tree's own background loader is not one of the workers
58 `wait_for_workers` waits on, so an app whose workers are all done can still
59 be showing an empty catalog.
60 """
61 tree = app.data_catalog.database_tree
62 await wait_for(
63 pilot,
64 lambda: not tree.loading and bool(tree.root.children),
65 description="the Data Catalog to render its databases",
66 )
67 return tree
68
69
70 async def rendered_catalog(pilot: Pilot, app: Harlequin) -> Catalog:
71 """The catalog the tree is holding, once its nodes are mounted.
72
73 `wait_for_workers` returns when `get_catalog()` has answered, which is
74 before the app has handled the message carrying its result, so reading the
75 tree straight after it races the message pump. `loading` is what the tree
76 clears at the end of `watch_catalog`, after the reload that mounts the
77 nodes -- setting `catalog` alone says nothing is on screen yet.
78 """
79 tree = app.data_catalog.database_tree
80 return await wait_for_value(
81 pilot,
82 lambda: tree.catalog if not tree.loading else None,
83 description="the data catalog to load",
84 )
85
86
87 async def replaced_catalog(pilot: Pilot, app: Harlequin, previous: Catalog) -> Catalog:
88 """The catalog the tree is holding once it is no longer `previous`."""
89 tree = app.data_catalog.database_tree
90 return await wait_for_value(
91 pilot,
92 lambda: (
93 tree.catalog if tree.catalog is not previous and not tree.loading else None
94 ),
95 description="the data catalog to be replaced",
96 )
97
98
99 async def first_database_node(pilot: Pilot, app: Harlequin) -> TreeNode[CatalogItem]:
100 """The tree's first database node, once the catalog has been rendered."""
101 tree = await wait_for_catalog_tree(pilot, app)
102 return tree.root.children[0]
103
104
105 async def expand_catalog_node(pilot: Pilot, node: TreeNode[CatalogItem]) -> None:
106 """Expand a catalog node and wait until its real children are rendered.
107
108 The catalog shows a "loading…" placeholder child as soon as an unloaded
109 node is expanded, and `fetch_children` sets `loaded` on the worker thread
110 while the node is repopulated later on the event loop -- so a node with one
111 real child passes a count comparison while the placeholder is still what is
112 on screen.
113 """
114 node.expand()
115
116 def children_are_rendered() -> bool:
117 data = node.data
118 if data is None:
119 return True
120 return (
121 getattr(data, "loaded", True)
122 and not DatabaseTree._has_placeholder(node)
123 and len(node.children) == len(data.children)
124 )
125
126 await wait_for(
127 pilot,
128 children_are_rendered,
129 description=f"the children of {node.label!s} to be rendered",
130 )