"""COMPUTATION lane — DB Browser for SQLite (Qt5). The app's CORE: input -> output. Two surfaces are driven for real and their computed results read back and asserted against the sample.db ground truth: * Execute SQL — type a query, press F5, read the result grid + status text (count / sum / max / group-by / where / view / expression / error). * Browse Data — select a table in the combo, read the "1 - N of N" row-count label and the grid cells (selection -> observed value change). Every value is READ OFF THE RUNNING APP, never from source/constants. A dead shell that never runs the query (empty grid / no status) fails every test. """ import re import time import pytest, pytest_check as check import rb_atspi from gi.repository import Atspi DB = "/workspace/output/fixtures/sample.db" # --------------------------------------------------------------------------- # # Module fixture — ensure sample.db is open (cold baseline starts with no DB). # --------------------------------------------------------------------------- # @pytest.fixture(scope="module", autouse=True) def _load_db(app): rb_atspi.focus_app_window(app) frame = rb_atspi.get_main_frame(app) for _ in range(4): rb_atspi.press_escape(); time.sleep(0.15) if "sample.db" not in (frame.get_name() or ""): fm = rb_atspi.find_first(frame, role="menu item", name="File") if fm: rb_atspi.click_node(fm); time.sleep(0.4) op = rb_atspi.find_first(frame, role="menu item", name="Open Database...") if op: rb_atspi.open_modal(op); time.sleep(1.2) rb_atspi.type_text(DB); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.5) yield # --------------------------------------------------------------------------- # # Geometry-based locators (verified live on the running reference). # --------------------------------------------------------------------------- # def _extents(n): try: return n.get_extents(Atspi.CoordType.SCREEN) except Exception: return None def _forigin(frame): """Frame's top-left in SCREEN coords. Locators are computed RELATIVE to this so they work regardless of where the window manager places the window (a cold relaunch does NOT put it at the same absolute x/y the warm testgen instance had).""" e = _extents(frame) return (e.x, e.y) if e else (0, 0) def _exec_tab(frame): rb_atspi.focus_app_window(rb_atspi.get_app()) t = rb_atspi.find_first(frame, role="page tab", name="Execute SQL") if t: if not rb_atspi.do_action(t, "Press"): rb_atspi.click_node(t) time.sleep(0.7) return t def _browse_tab(frame): rb_atspi.focus_app_window(rb_atspi.get_app()) t = rb_atspi.find_first(frame, role="page tab", name="Browse Data") if t: if not rb_atspi.do_action(t, "Press"): rb_atspi.click_node(t) time.sleep(1.0) return t def _sql_editor(frame): """The Scintilla SQL editor: the showing role=text at the TOP-LEFT of the Execute SQL tab (frame-relative x<400, tall, smallest y).""" fx, fy = _forigin(frame) best = None for t in rb_atspi.find_all(frame, role="text"): if not rb_atspi.is_showing(t): continue e = _extents(t) if e is None: continue rx, ry = e.x - fx, e.y - fy if rx < 400 and e.height > 80 and e.width > 200 and ry < 420: if best is None or e.y < best[1]: best = (t, e.y) return best[0] if best else None def _status_text(frame): """editErrors QTextEdit: the showing role=text at the BOTTOM-LEFT (largest y) of the Execute SQL tab (frame-relative). Returns its plain text.""" fx, fy = _forigin(frame) cand = None for t in rb_atspi.find_all(frame, role="text"): if not rb_atspi.is_showing(t): continue e = _extents(t) if e is None: continue rx, ry = e.x - fx, e.y - fy if rx < 400 and e.width > 200 and ry > 420: if cand is None or e.y > cand[1]: cand = (t, e.y) return rb_atspi.get_text(cand[0]) if cand else "" def _result_table(frame): """The Execute SQL result grid: showing role=table with real extents at the left (frame-relative x<400) below the editor (ry>250). The Browse grid persists in the tree with (0,0,0,0) extents while this tab is active, so extent-filtering picks the right one.""" fx, fy = _forigin(frame) best = None for tb in rb_atspi.find_all(frame, role="table"): if not rb_atspi.is_showing(tb): continue e = _extents(tb) if e is None or e.width <= 0: continue rx, ry = e.x - fx, e.y - fy if rx < 400 and ry > 250: if best is None or e.y < best[1]: best = (tb, e.y) return best[0] if best else None def run_sql(frame, sql): """Clear the SQL editor, type `sql`, press F5, and read back (cells, headers, status_text). Polls for the grid to populate.""" _exec_tab(frame) ed = _sql_editor(frame) assert ed is not None, "SQL editor not found on Execute SQL tab" rb_atspi.click_node(ed); time.sleep(0.25) rb_atspi.key("ctrl+a"); rb_atspi.key("Delete"); time.sleep(0.15) rb_atspi.type_text(sql); time.sleep(0.3) rb_atspi.key("F5") # Poll for execution to finish (status text mentions "Execution finished"). def _done(): return "Execution finished" in _status_text(frame) rb_atspi.wait_for(_done, timeout=6.0, interval=0.25) time.sleep(0.3) tb = _result_table(frame) cells = [c.get_name() or "" for c in rb_atspi.find_all(tb, role="table cell")] if tb else [] hdrs = [h.get_name() or "" for h in rb_atspi.find_all(tb, role="table column header")] if tb else [] return cells, hdrs, _status_text(frame) def _rowcount_label(frame): for l in rb_atspi.find_all(frame, role="label"): n = l.get_name() or "" if re.match(r"^\s*\d+\s*-\s*\d+\s*of\s*\d+", n): return n.strip() return None def _browse_combo(frame): """Table selector combo near top-left of Browse Data (frame-relative x<400, ry~90).""" fx, fy = _forigin(frame) for c in rb_atspi.find_all(frame, role="combo box"): if not rb_atspi.is_showing(c): continue e = _extents(c) if e is None: continue rx, ry = e.x - fx, e.y - fy if 0 < rx < 400 and 60 < ry < 240 and 80 < e.width < 400: return c return None def _browse_grid(frame): fx, fy = _forigin(frame) for tb in rb_atspi.find_all(frame, role="table"): if not rb_atspi.is_showing(tb): continue e = _extents(tb) if e is None or e.width <= 0: continue rx, ry = e.x - fx, e.y - fy if rx < 400 and ry < 260: return tb return None def _select_browse_table(frame, target): """Select `target` in the Browse-Data table combo. The Qt combo popup exposes list items with bogus (0,0)-based extents, so a center-click on an item misses. Instead we open the popup with a real click and use Qt type-ahead: typing the table name jumps the highlight to it, Return commits. Returns True when the combo's live name becomes `target`.""" combo = _browse_combo(frame) if combo is None: return False if (combo.get_name() or "") == target: return True e = _extents(combo) if e is None: return False rb_atspi.xdo("mousemove", e.x + e.width // 2, e.y + e.height // 2, "click", "1") time.sleep(0.5) rb_atspi.type_text(target); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.0) c2 = _browse_combo(frame) if bool(c2) and (c2.get_name() or "") == target: return True # Retry once (a stray highlight can need a second commit). rb_atspi.press_escape(); time.sleep(0.2) c3 = _browse_combo(frame) if c3 is None: return False e3 = _extents(c3) rb_atspi.xdo("mousemove", e3.x + e3.width // 2, e3.y + e3.height // 2, "click", "1") time.sleep(0.5) rb_atspi.type_text(target); time.sleep(0.4) rb_atspi.key("Return"); time.sleep(1.0) c4 = _browse_combo(frame) return bool(c4) and (c4.get_name() or "") == target # --------------------------------------------------------------------------- # # Execute-SQL value tests # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="value", expect="single cell 8") def test_sql_count_employees(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT count(*) FROM employees;") check.is_in("8", cells, f"count(*) employees expected 8, got cells={cells}") check.equal([c for c in cells if c][0] if cells else None, "8", f"first result cell should be '8', cells={cells}") check.is_in("without errors", status, f"status did not report success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="single cell 3") def test_sql_count_departments(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT count(*) FROM departments;") check.is_in("3", cells, f"count(*) departments expected 3, got cells={cells}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="single cell 538000 (SUM salary)") def test_sql_sum_salary(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT SUM(salary) FROM employees;") check.is_in("538000", cells, f"SUM(salary) expected 538000, got cells={cells}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="single cell 94000 (MAX salary)") def test_sql_max_salary(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT MAX(salary) FROM employees;") check.is_in("94000", cells, f"MAX(salary) expected 94000, got cells={cells}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb( jump_kind="value", expect="grid: Engineering,3,251000 / Marketing,2,115000 / Sales,3,172000", ) def test_sql_group_by_department(app, frame, rb, results_dir): rb.enter("execute") cells, hdrs, status = run_sql( frame, "SELECT department, COUNT(*), SUM(salary) FROM employees " "GROUP BY department ORDER BY department;", ) # Row-major: Engineering,3,251000, Marketing,2,115000, Sales,3,172000 check.is_in("without errors", status, f"status not success: {status!r}") check.equal(len(cells), 9, f"expected 9 cells (3 rows x 3 cols), got {cells}") if len(cells) == 9: check.equal(cells[0], "Engineering", f"cell[0] {cells[0]!r}") check.equal(cells[1], "3", f"cell[1] Engineering count {cells[1]!r}") check.equal(cells[2], "251000", f"cell[2] Engineering sum {cells[2]!r}") check.equal(cells[3], "Marketing", f"cell[3] {cells[3]!r}") check.equal(cells[4], "2", f"cell[4] Marketing count {cells[4]!r}") check.equal(cells[5], "115000", f"cell[5] Marketing sum {cells[5]!r}") check.equal(cells[6], "Sales", f"cell[6] {cells[6]!r}") check.equal(cells[7], "3", f"cell[7] Sales count {cells[7]!r}") check.equal(cells[8], "172000", f"cell[8] Sales sum {cells[8]!r}") else: for want in ("Engineering", "Marketing", "Sales", "251000", "115000", "172000"): check.is_in(want, cells, f"missing {want} in group-by result") rb_atspi.screenshot_assert( "test_sql_group_by_department", "Execute SQL result grid shows three department rows: Engineering with " "count 3 and salary sum 251000, Marketing with count 2 and 115000, and " "Sales with count 3 and 172000.", results_dir, window=frame, ) @pytest.mark.rb(jump_kind="value", expect="two rows: Alice and Grace") def test_sql_where_filter(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql( frame, "SELECT name FROM employees WHERE salary>80000 ORDER BY name;" ) names = [c for c in cells if c] check.equal(names, ["Alice", "Grace"], f"WHERE salary>80000 ORDER BY name expected [Alice, Grace], got {names}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="single cell 5 (high_earners view count)") def test_sql_view_high_earners_count(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT count(*) FROM high_earners;") check.is_in("5", cells, f"count(*) high_earners expected 5, got cells={cells}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="single cell 42 (6*7 expression)") def test_sql_expression_eval(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT 6*7 AS x;") check.is_in("42", cells, f"6*7 expected 42, got cells={cells}") check.is_in("without errors", status, f"status not success: {status!r}") @pytest.mark.rb(jump_kind="value", expect="error message: no such table") def test_sql_error_reporting(app, frame, rb): rb.enter("execute") cells, hdrs, status = run_sql(frame, "SELECT * FROM no_such_table;") check.is_in("with errors", status, f"invalid query should report errors, status={status!r}") check.is_in("no such table", status, f"error status should mention 'no such table', got {status!r}") # --------------------------------------------------------------------------- # # Browse-Data value tests (selection -> observed value change) # --------------------------------------------------------------------------- # @pytest.mark.rb(jump_kind="value", expect="row-count label '1 - 3 of 3' (departments)") def test_browse_default_table_rowcount(app, frame, rb, results_dir): rb.enter("browse") _browse_tab(frame) _select_browse_table(frame, "departments") # normalize to departments time.sleep(0.5) label = _rowcount_label(frame) check.equal(label, "1 - 3 of 3", f"departments row-count label expected '1 - 3 of 3', got {label!r}") rb_atspi.screenshot_assert( "test_browse_default_table_rowcount", "Browse Data tab showing the departments table: a grid with 3 rows " "(Engineering, Sales, Marketing) and a '1 - 3 of 3' row-count label.", results_dir, window=frame, ) @pytest.mark.rb( jump_kind="value", expect="after selecting employees: '1 - 8 of 8' and grid shows Alice", ) def test_browse_select_employees_changes_grid(app, frame, rb, results_dir): rb.enter("browse") _browse_tab(frame) # Start from departments so the change is a real transition. _select_browse_table(frame, "departments"); time.sleep(0.4) before = _rowcount_label(frame) ok = _select_browse_table(frame, "employees") check.is_true(ok, "combo did not switch to 'employees'") time.sleep(0.4) after = _rowcount_label(frame) check.equal(after, "1 - 8 of 8", f"employees row-count expected '1 - 8 of 8', got {after!r}") check.not_equal(before, after, "row-count label did not change on table switch") grid = _browse_grid(frame) cells = [c.get_name() or "" for c in rb_atspi.find_all(grid, role="table cell")] if grid else [] hdrs = [h.get_name() or "" for h in rb_atspi.find_all(grid, role="table column header")] if grid else [] check.is_in("Alice", cells, f"employees grid should contain 'Alice', cells={cells[:12]}") check.is_in("name", hdrs, f"employees grid headers should include 'name', got {hdrs}") rb_atspi.screenshot_assert( "test_browse_select_employees_changes_grid", "Browse Data tab showing the employees table: an 8-row grid with columns " "id/name/department/salary/hired (Alice/Engineering/85000 in the first " "row) and a '1 - 8 of 8' row-count label.", results_dir, window=frame, ) # Leave it on departments for order-independence. _select_browse_table(frame, "departments"); time.sleep(0.3) @pytest.mark.rb( jump_kind="value", expect="employees grid contains Alice and her salary 85000", ) def test_browse_cell_value_alice_salary(app, frame, rb): rb.enter("browse") _browse_tab(frame) ok = _select_browse_table(frame, "employees") check.is_true(ok, "combo did not switch to 'employees'") time.sleep(0.4) grid = _browse_grid(frame) cells = [c.get_name() or "" for c in rb_atspi.find_all(grid, role="table cell")] if grid else [] check.is_in("Alice", cells, f"grid should contain 'Alice', cells={cells[:12]}") check.is_in("85000", cells, f"grid should contain Alice's salary 85000, cells={cells[:12]}") _select_browse_table(frame, "departments"); time.sleep(0.3)