askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
conftest.py · 3.2 KB · 80 lines
| 1 | import json |
| 2 | import sys |
| 3 | from pathlib import Path |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parents[1] |
| 9 | if str(ROOT) not in sys.path: |
| 10 | sys.path.insert(0, str(ROOT)) |
| 11 | |
| 12 | PACKAGES = ROOT / "packages" |
| 13 | |
| 14 | |
| 15 | @pytest.fixture(autouse=True) |
| 16 | def isolated_default_data_dir(monkeypatch, tmp_path): |
| 17 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path / "data")) |
| 18 | |
| 19 | |
| 20 | # --- shared TSV fixture staging --------------------------------------------- |
| 21 | # |
| 22 | # A cluster of tests build a data_dir by hand: copy a real package schema |
| 23 | # into schemas/, then write collections/<name>/records.tsv themselves. Many |
| 24 | # of them used to hand-type that header as a literal tab-separated string. |
| 25 | # That string has no connection to the schema it claims to describe, so |
| 26 | # when a field is added to the real schema, the hand-typed header silently |
| 27 | # stops matching field order -- the test keeps passing while exercising a |
| 28 | # column layout that no longer exists in production. schema_header() and |
| 29 | # stage_collection() derive the header from the schema itself so drift is |
| 30 | # structurally impossible. |
| 31 | # |
| 32 | # These are plain functions, not fixtures: `from conftest import |
| 33 | # schema_header, stage_collection` at each call site, so a test's fixture |
| 34 | # setup still reads as ordinary, narrative code rather than disappearing |
| 35 | # behind fixture injection. |
| 36 | |
| 37 | def schema_header(package_id, collection): |
| 38 | """The TSV header line (with trailing newline) for one package |
| 39 | collection, in the exact field order its real schema declares.""" |
| 40 | schema = json.loads( |
| 41 | (PACKAGES / package_id / "schemas" / f"{collection}.json").read_text()) |
| 42 | return "\t".join(f["name"] for f in schema["fields"]) + "\n" |
| 43 | |
| 44 | |
| 45 | def stage_collection(data_dir, package_id, collection, *, rows="", seed=False): |
| 46 | """Stage one real package collection under `data_dir`: copy its schema |
| 47 | JSON into schemas/, then create collections/<collection>/records.tsv. |
| 48 | |
| 49 | seed=True copies the package's seed/<collection>.tsv verbatim -- for |
| 50 | tests that need the seeded reference rows themselves (e.g. |
| 51 | denominations), not just an empty, correctly-shaped collection. |
| 52 | Otherwise the file is schema_header() plus `rows`, a caller-supplied |
| 53 | TSV body already in schema field order (empty for a collection a test |
| 54 | only needs to exist). |
| 55 | """ |
| 56 | schema_dir = data_dir / "schemas" |
| 57 | schema_dir.mkdir(parents=True, exist_ok=True) |
| 58 | (schema_dir / f"{collection}.json").write_text( |
| 59 | (PACKAGES / package_id / "schemas" / f"{collection}.json").read_text()) |
| 60 | |
| 61 | coll_dir = data_dir / "collections" / collection |
| 62 | coll_dir.mkdir(parents=True, exist_ok=True) |
| 63 | records_path = coll_dir / "records.tsv" |
| 64 | if seed: |
| 65 | records_path.write_text( |
| 66 | (PACKAGES / package_id / "seed" / f"{collection}.tsv").read_text()) |
| 67 | else: |
| 68 | records_path.write_text(schema_header(package_id, collection) + rows) |
| 69 | return records_path |
| 70 | |
| 71 | |
| 72 | # The e2e browser lane needs playwright, which the per-commit Tests |
| 73 | # environment deliberately does not install. Deselection via the marker is |
| 74 | # not enough: pytest still IMPORTS a module to collect it, and an import |
| 75 | # error fails the whole run. Where the dependency is absent, the lane |
| 76 | # should be invisible, not broken. |
| 77 | try: |
| 78 | import playwright # noqa: F401 |
| 79 | except ImportError: |
| 80 | collect_ignore_glob = ["e2e/*"] |