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.
test_install_order_nav.py · 4.8 KB · 129 lines
| 1 | """Nav entries and attention sources do not depend on install order. |
| 2 | |
| 3 | Found by three simulated-organisation runs (a clinic, a nonprofit and a |
| 4 | shop): each installed its own package before app-nav, the install |
| 5 | succeeded with no warning, and the app had no menu entry and no |
| 6 | attention count. `nav` and `attention` had been reported `skipped` and |
| 7 | nothing ever wrote them afterwards; reinstalling in the "right" order was |
| 8 | the only fix, and nobody knew there was one. |
| 9 | |
| 10 | Now installing app-nav registers what every installed package declared, |
| 11 | and an install whose own entries are skipped says so in `warnings`. |
| 12 | """ |
| 13 | |
| 14 | import json |
| 15 | import shutil |
| 16 | from pathlib import Path |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | import object_package_baselines |
| 21 | import object_packages |
| 22 | import object_records |
| 23 | |
| 24 | PACKAGES = Path(__file__).resolve().parents[1] / "packages" |
| 25 | APP = "app-invoices" # declares nav "invoices" and attention "invoices_overdue" |
| 26 | |
| 27 | |
| 28 | @pytest.fixture() |
| 29 | def box(tmp_path): |
| 30 | data = tmp_path / "data" |
| 31 | data.mkdir() |
| 32 | objects = tmp_path / "objects" |
| 33 | objects.mkdir() |
| 34 | |
| 35 | def install(package_id, *, root=PACKAGES, **kwargs): |
| 36 | return object_packages.install_package( |
| 37 | package_id, root=root, base_dir=data, object_roots=[objects], **kwargs) |
| 38 | |
| 39 | install.data = data |
| 40 | return install |
| 41 | |
| 42 | |
| 43 | def _rows(data, collection): |
| 44 | return {row["id"]: row for row in object_records.read_collection_records(collection, base_dir=data)} |
| 45 | |
| 46 | |
| 47 | def _registered(data): |
| 48 | return ({key: row["package"] for key, row in _rows(data, "nav_entries").items()}, |
| 49 | {key: row["package"] for key, row in _rows(data, "attention_sources").items()}) |
| 50 | |
| 51 | |
| 52 | def test_a_package_installed_before_app_nav_is_registered_when_app_nav_arrives(box): |
| 53 | first = box(APP) |
| 54 | assert {item["status"] for item in first["nav"] + first["attention"]} == {"skipped"} |
| 55 | nav = box("app-nav") |
| 56 | |
| 57 | entries, sources = _registered(box.data) |
| 58 | assert entries["invoices"] == APP |
| 59 | assert sources["invoices_overdue"] == APP |
| 60 | assert {(item["package"], item["kind"], item["id"], item["status"]) |
| 61 | for item in nav["registered_for_others"]} == { |
| 62 | (APP, "nav", "invoices", "written"), |
| 63 | (APP, "attention", "invoices_overdue", "written"), |
| 64 | } |
| 65 | |
| 66 | |
| 67 | def test_both_orders_end_with_the_same_registry(box, tmp_path): |
| 68 | box(APP) |
| 69 | box("app-nav") |
| 70 | late = _registered(box.data) |
| 71 | |
| 72 | data = tmp_path / "other" / "data" |
| 73 | objects = tmp_path / "other" / "objects" |
| 74 | data.mkdir(parents=True) |
| 75 | objects.mkdir() |
| 76 | for package_id in ("app-nav", APP): |
| 77 | object_packages.install_package(package_id, root=PACKAGES, base_dir=data, |
| 78 | object_roots=[objects]) |
| 79 | assert _registered(data) == late |
| 80 | |
| 81 | |
| 82 | def test_an_install_whose_entries_are_skipped_says_so(box): |
| 83 | """The silent part of the bug: a successful install, no warning.""" |
| 84 | result = box(APP) |
| 85 | warning = " ".join(result["warnings"]) |
| 86 | assert "NOT registered" in warning and "app-nav" in warning |
| 87 | box("app-nav") |
| 88 | again = box(APP, allow_replace=True) |
| 89 | assert not any("NOT registered" in text for text in again["warnings"]) |
| 90 | |
| 91 | |
| 92 | def test_installing_app_nav_writes_only_missing_rows(box): |
| 93 | """It cannot undo an operator's choice or settle a collision: a row |
| 94 | that exists is left exactly as it is.""" |
| 95 | box("app-nav") |
| 96 | box(APP) |
| 97 | object_records.update_collection_record( |
| 98 | "nav_entries", "invoices", {"operator_hidden": "true", "label": "Bills"}, |
| 99 | base_dir=box.data, actor="test") |
| 100 | result = box("app-nav", allow_replace=True) |
| 101 | assert result["registered_for_others"] == [] |
| 102 | row = _rows(box.data, "nav_entries")["invoices"] |
| 103 | assert (row["operator_hidden"], row["label"]) == ("true", "Bills") |
| 104 | |
| 105 | |
| 106 | def test_an_older_baseline_is_registered_from_the_installed_versions_manifest(box, tmp_path): |
| 107 | """A box installed before baselines recorded declarations: the manifest |
| 108 | is used only when it is the version installed, and otherwise the |
| 109 | package is named rather than skipped in silence.""" |
| 110 | root = tmp_path / "packages" |
| 111 | shutil.copytree(PACKAGES / APP, root / APP) |
| 112 | shutil.copytree(PACKAGES / "app-nav", root / "app-nav") |
| 113 | box(APP, root=root) |
| 114 | path = object_package_baselines.baseline_path(APP, base_dir=box.data) |
| 115 | baseline = json.loads(path.read_text()) |
| 116 | baseline.pop("nav") |
| 117 | baseline.pop("attention") |
| 118 | path.write_text(json.dumps(baseline)) |
| 119 | |
| 120 | manifest = root / APP / "dbbasic-package.json" |
| 121 | shipped = json.loads(manifest.read_text()) |
| 122 | manifest.write_text(json.dumps({**shipped, "version": "99.0.0"})) |
| 123 | result = box("app-nav", root=root) |
| 124 | assert "invoices" not in _rows(box.data, "nav_entries") |
| 125 | assert any(text.startswith(f"{APP}:") for text in result["warnings"]) |
| 126 | |
| 127 | manifest.write_text(json.dumps(shipped)) |
| 128 | box("app-nav", root=root, allow_replace=True) |
| 129 | assert _rows(box.data, "nav_entries")["invoices"]["package"] == APP |