Files
ukmesh/viewshed-worker/tests/test_relation_qualification.py
hermes-gadget 79190f8382 fix(ci): ship the external-consumer SQL checker with the worker image (#196)
The Workers and Compose job runs the worker suite inside the built image
(`python -m unittest discover -s /app/tests`). The relation-qualification
test resolved the checker as `<repo>/scripts/check_external_consumer_sql.py`
via `Path(__file__).parents[2]`, which inside the container is `/scripts`,
and the image only carries `viewshed-worker/`, so the job died with
`ModuleNotFoundError: check_external_consumer_sql` before any test ran.

The image builds with `context: viewshed-worker`, so a repo-level `scripts/`
file cannot be COPYed in without widening the build context (and the release
workflow). Move the implementation next to the worker (`viewshed-worker/
check_external_consumer_sql.py`), leave a thin shim at the old path so
`backend`'s `test:external-consumers:sql` keeps working from a checkout, and
add the module to the Dockerfile COPY.

The test now resolves the module from the worker root, and a new in-image
test asserts the checker imports from that root, so dropping it from the
image fails with a named test instead of a bare import error.
2026-11-05 16:44:29 +00:00

45 lines
1.7 KiB
Python

"""Regression guard for external consumers of moved UKMesh relations.
The link worker, Discord bot and CoreScope are separate deployment units. A
green backend suite does not prove that their SQL still resolves after a table
move, so scan their runtime sources before deploying a move.
The checker lives in this tree (not ``scripts/``) because the worker image
builds from ``viewshed-worker/`` and runs this suite inside the container; see
``test_checker_shipping.py`` for the guard that keeps it there.
"""
import os
import sys
import unittest
from pathlib import Path
WORKER_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(WORKER_ROOT))
from check_external_consumer_sql import scan_roots, scan_text # noqa: E402
class RelationQualificationTest(unittest.TestCase):
def test_link_worker_sources_are_qualified(self):
violations = scan_roots([WORKER_ROOT])
self.assertEqual(violations, [], f"unqualified moved relations: {violations}")
def test_scanner_rejects_short_names_and_accepts_schema_qualified_sql(self):
bad = "SELECT * FROM nodes JOIN packets p ON true"
good = "SELECT * FROM core.nodes JOIN mesh.packets p ON true"
self.assertEqual(
[relation for _, _, relation in scan_text(bad)],
["nodes", "packets"],
)
self.assertEqual(scan_text(good), [])
def test_optional_external_roots(self):
roots = [Path(part) for part in os.environ.get("EXTERNAL_CONSUMER_ROOTS", "").split(os.pathsep) if part]
if not roots:
self.skipTest("EXTERNAL_CONSUMER_ROOTS is not configured")
self.assertEqual(scan_roots(roots), [])
if __name__ == "__main__":
unittest.main()