|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""The two fixture-worker entry points must serve the same catalogs. |
| 4 | +
|
| 5 | +``vgi-fixture-worker`` (subprocess/launcher transport) and ``vgi-fixture-http`` |
| 6 | +(HTTP transport) each assemble their own list of worker classes. The SAME |
| 7 | +``.test`` files run against both, so a catalog present in one list and absent |
| 8 | +from the other fails those files on one transport only — which reads as a |
| 9 | +transport-specific bug and is not one. |
| 10 | +
|
| 11 | +That is not hypothetical. ``NarrowBindWorker`` was added to the subprocess entry |
| 12 | +point and not the HTTP one, so ``narrow_bind_mismatch.test`` failed every HTTP |
| 13 | +run with ``No worker handles catalog 'narrow_bind'``. That test guards a client |
| 14 | +SIGSEGV — a worker whose bind ``output_schema`` is narrower than the table's |
| 15 | +advertised columns used to walk off the end of the batch in |
| 16 | +``ArrowTableFunction::ArrowToDuckDB`` — so it was the worst one to silently lose |
| 17 | +on a transport. |
| 18 | +
|
| 19 | +Both lists are built inside function bodies with local imports (deliberately: |
| 20 | +the fixture extras are optional, and a module-level import would make the whole |
| 21 | +package require them). So this reads the source rather than calling the |
| 22 | +functions, which would start a server. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +import ast |
| 28 | +import inspect |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +import pytest |
| 32 | + |
| 33 | +# Workers each entry point may legitimately add on its own. The writable |
| 34 | +# catalog is conditional on an optional extra in BOTH, and is appended outside |
| 35 | +# the literal list, so it never appears in either parsed set. |
| 36 | +_ALLOWED_ASYMMETRY: dict[str, str] = {} |
| 37 | + |
| 38 | + |
| 39 | +def _worker_class_names(module_path: Path, func_name: str) -> set[str]: |
| 40 | + """Names in the `worker_classes = [...]` / `workers = [...]` literal. |
| 41 | +
|
| 42 | + Parsed rather than executed: calling either function starts a worker. |
| 43 | + """ |
| 44 | + tree = ast.parse(module_path.read_text()) |
| 45 | + |
| 46 | + target: ast.FunctionDef | None = None |
| 47 | + for node in ast.walk(tree): |
| 48 | + if isinstance(node, ast.FunctionDef) and node.name == func_name: |
| 49 | + target = node |
| 50 | + break |
| 51 | + assert target is not None, f"{module_path.name} has no function {func_name}()" |
| 52 | + |
| 53 | + for node in ast.walk(target): |
| 54 | + if not isinstance(node, ast.AnnAssign | ast.Assign): |
| 55 | + continue |
| 56 | + targets = [node.target] if isinstance(node, ast.AnnAssign) else node.targets |
| 57 | + names = {t.id for t in targets if isinstance(t, ast.Name)} |
| 58 | + if not names & {"worker_classes", "workers"}: |
| 59 | + continue |
| 60 | + if not isinstance(node.value, ast.List): |
| 61 | + continue |
| 62 | + found = {e.id for e in node.value.elts if isinstance(e, ast.Name)} |
| 63 | + if found: |
| 64 | + return found |
| 65 | + |
| 66 | + pytest.fail( |
| 67 | + f"{module_path.name}:{func_name}() no longer assigns a list literal of worker " |
| 68 | + "classes to `worker_classes` or `workers`. This guard now matches nothing — " |
| 69 | + "update the parser rather than deleting the test." |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture(scope="module") |
| 74 | +def _lists() -> tuple[set[str], set[str]]: |
| 75 | + import vgi._test_fixtures.http_server as http_server |
| 76 | + import vgi._test_fixtures.worker as subprocess_worker |
| 77 | + |
| 78 | + sub = _worker_class_names(Path(inspect.getfile(subprocess_worker)), "main") |
| 79 | + http = _worker_class_names(Path(inspect.getfile(http_server)), "main") |
| 80 | + return sub, http |
| 81 | + |
| 82 | + |
| 83 | +def test_both_lists_are_non_empty(_lists: tuple[set[str], set[str]]) -> None: |
| 84 | + """A parser that silently matches nothing would make this whole file vacuous.""" |
| 85 | + sub, http = _lists |
| 86 | + assert len(sub) >= 2, f"subprocess entry point parsed as {sub} — parser is broken" |
| 87 | + assert len(http) >= 2, f"HTTP entry point parsed as {http} — parser is broken" |
| 88 | + |
| 89 | + |
| 90 | +def test_entry_points_serve_the_same_catalogs(_lists: tuple[set[str], set[str]]) -> None: |
| 91 | + """Neither entry point may serve a catalog the other does not.""" |
| 92 | + sub, http = _lists |
| 93 | + only_sub = sorted(sub - http - set(_ALLOWED_ASYMMETRY)) |
| 94 | + only_http = sorted(http - sub - set(_ALLOWED_ASYMMETRY)) |
| 95 | + assert not only_sub and not only_http, ( |
| 96 | + "the fixture entry points serve different catalogs, so the shared .test files " |
| 97 | + "will fail on one transport only:\n" |
| 98 | + f" only in vgi-fixture-worker (subprocess): {only_sub}\n" |
| 99 | + f" only in vgi-fixture-http: {only_http}\n" |
| 100 | + "Add the missing worker to the other list, or record it in _ALLOWED_ASYMMETRY " |
| 101 | + "with the reason it is genuinely one-transport." |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def test_allowed_asymmetry_stays_honest(_lists: tuple[set[str], set[str]]) -> None: |
| 106 | + """An excuse for a worker neither list mentions reads as coverage that isn't there.""" |
| 107 | + sub, http = _lists |
| 108 | + for name, reason in _ALLOWED_ASYMMETRY.items(): |
| 109 | + assert name in sub or name in http, ( |
| 110 | + f"_ALLOWED_ASYMMETRY excuses {name!r} ({reason}), but neither entry point " |
| 111 | + "mentions it — drop the entry." |
| 112 | + ) |
0 commit comments