|
| 1 | +"""Integration test for DuckDB manual refresh expansion loading.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqlit.domains.connections.providers.duckdb.adapter import DuckDBAdapter |
| 8 | +from sqlit.domains.shell.app.main import SSMSTUI |
| 9 | +from tests.helpers import ConnectionConfig |
| 10 | +from tests.integration.browsing_base import find_connection_node, find_folder_node, has_table_children, wait_for_condition |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.integration |
| 14 | +@pytest.mark.asyncio |
| 15 | +async def test_duckdb_expanded_tables_load_on_manual_refresh(monkeypatch: pytest.MonkeyPatch) -> None: |
| 16 | + try: |
| 17 | + import duckdb # type: ignore |
| 18 | + except ImportError: |
| 19 | + pytest.skip("duckdb is not installed") |
| 20 | + |
| 21 | + connection = duckdb.connect(":memory:") |
| 22 | + connection.execute("CREATE TABLE test_users(id INTEGER, name VARCHAR)") |
| 23 | + |
| 24 | + def connect_in_memory(self: DuckDBAdapter, config) -> object: |
| 25 | + _ = config |
| 26 | + return connection |
| 27 | + |
| 28 | + monkeypatch.setattr(DuckDBAdapter, "connect", connect_in_memory) |
| 29 | + |
| 30 | + config = ConnectionConfig( |
| 31 | + name="duckdb-memory", |
| 32 | + db_type="duckdb", |
| 33 | + file_path=":memory:", |
| 34 | + ) |
| 35 | + |
| 36 | + app = SSMSTUI() |
| 37 | + |
| 38 | + try: |
| 39 | + async with app.run_test(size=(120, 40)) as pilot: |
| 40 | + await pilot.pause(0.1) |
| 41 | + |
| 42 | + app.connections = [config] |
| 43 | + app.refresh_tree() |
| 44 | + await pilot.pause(0.1) |
| 45 | + |
| 46 | + await wait_for_condition( |
| 47 | + pilot, |
| 48 | + lambda: len(app.object_tree.root.children) > 0, |
| 49 | + timeout_seconds=5.0, |
| 50 | + description="tree to be populated with connections", |
| 51 | + ) |
| 52 | + |
| 53 | + app._expanded_paths = {f"conn:{config.name}/folder:tables"} |
| 54 | + |
| 55 | + app.connect_to_server(config) |
| 56 | + await pilot.pause(0.5) |
| 57 | + |
| 58 | + await wait_for_condition( |
| 59 | + pilot, |
| 60 | + lambda: app.current_connection is not None, |
| 61 | + timeout_seconds=10.0, |
| 62 | + description="duckdb connection to be established", |
| 63 | + ) |
| 64 | + |
| 65 | + connected_node = find_connection_node(app.object_tree.root, config.name) |
| 66 | + assert connected_node is not None |
| 67 | + |
| 68 | + await wait_for_condition( |
| 69 | + pilot, |
| 70 | + lambda: len(connected_node.children) > 0, |
| 71 | + timeout_seconds=5.0, |
| 72 | + description="connected node to be populated", |
| 73 | + ) |
| 74 | + |
| 75 | + tables_folder = find_folder_node(connected_node, "tables") |
| 76 | + assert tables_folder is not None |
| 77 | + |
| 78 | + assert not getattr(tables_folder, "is_expanded", False) |
| 79 | + |
| 80 | + await pilot.press("f") |
| 81 | + await pilot.pause(0.2) |
| 82 | + |
| 83 | + await wait_for_condition( |
| 84 | + pilot, |
| 85 | + lambda: has_table_children(tables_folder), |
| 86 | + timeout_seconds=10.0, |
| 87 | + description="tables to load after manual refresh", |
| 88 | + ) |
| 89 | + app.action_disconnect() |
| 90 | + app.exit() |
| 91 | + await pilot.pause(0.1) |
| 92 | + finally: |
| 93 | + connection.close() |
0 commit comments