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
test_app_finance_detail_retrofit.py · 7.1 KB · 155 lines
| 1 | """Stage-6 retrofit of packages/app-finance: the bespoke site_journal_view.py |
| 2 | page is replaced by a seeded 59 detail view composed of three GENERIC block |
| 3 | renderers -- an editable+deletable `detail` block (fin_journals; the |
| 4 | draft->posted transition becomes a status-field edit in the owner's edit |
| 5 | form, guarded by schemas/fin_journals.json's transitions, not a one-click |
| 6 | Post button -- same make-it-basic trade as app-forum's pin/lock), a |
| 7 | `related` block (fin_journal_lines by journal_id -- an append-only, |
| 8 | RELATIONAL event-log of ledger postings, never embedded), and a `form` |
| 9 | block with journal_id locked to the page's journal (Add Posting). |
| 10 | Debit/credit *_cents render through /form's own cents formatting on the |
| 11 | related list -- no custom code. The bespoke page's client-computed |
| 12 | total-debits/total-credits/is-balanced summary strip has no generic block |
| 13 | equivalent and is intentionally not carried forward (see |
| 14 | dbbasic-package.json's description). |
| 15 | """ |
| 16 | |
| 17 | import csv |
| 18 | import json |
| 19 | from pathlib import Path |
| 20 | |
| 21 | import object_packages |
| 22 | import object_permissions |
| 23 | |
| 24 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 25 | APP_FINANCE_DIR = PACKAGES_ROOT / "app-finance" |
| 26 | |
| 27 | |
| 28 | def _seed_rows(name): |
| 29 | with open(APP_FINANCE_DIR / "seed" / f"{name}.tsv", newline="") as fh: |
| 30 | return list(csv.DictReader(fh, delimiter="\t")) |
| 31 | |
| 32 | |
| 33 | def test_manifest_drops_bespoke_journal_page_and_seeds_the_detail_view(): |
| 34 | package = object_packages.get_package("app-finance", root=PACKAGES_ROOT) |
| 35 | assert {obj["id"] for obj in package["objects"]} == { |
| 36 | "site_trial_balance", "site_setup_accounts", |
| 37 | "site_statements", |
| 38 | "hook_fin_journals", # pre-write balance + closed-period hook |
| 39 | "hook_fin_journal_lines", # 0.7.0: lines of settled journals are settled |
| 40 | "action_reverse_journal", "system_fin_recurring_runner", # books spine |
| 41 | # 0.9.0: expenses -- the approval gate and the journal it composes. |
| 42 | "hook_expenses", "system_expense_books", |
| 43 | # 0.10.0: the attention provider for expenses waiting on an approver. |
| 44 | "system_expense_attention", |
| 45 | } |
| 46 | assert {entry["collection"] for entry in package["seed"]} == { |
| 47 | "denominations", # 0.4.0: seeded units of value (USD/JPY/BTC/ETH/XAU/...) |
| 48 | "fin_closed_periods", # 0.7.0: settled books stay settled |
| 49 | "rates", # 0.5.0: observed conversion rates (header-only; fetched, not seeded) |
| 50 | "fin_accounts", "fin_journals", "fin_journal_lines", "fin_recurring", |
| 51 | "expenses", # 0.9.0: the materials half of time and materials |
| 52 | "views", "site_routes", |
| 53 | } |
| 54 | assert {dep["id"] for dep in package["dependencies"]} == {"app-entities", "app-views"} |
| 55 | |
| 56 | |
| 57 | def test_journal_view_object_file_is_deleted(): |
| 58 | assert not (APP_FINANCE_DIR / "objects" / "site" / "journal_view.py").exists() |
| 59 | |
| 60 | |
| 61 | def test_journal_view_composes_detail_related_and_fk_locked_form(): |
| 62 | rows = _seed_rows("views") |
| 63 | # 0.7.0 added the /accounts and /journals index views alongside this |
| 64 | # detail view, so assert on the detail row rather than a total that |
| 65 | # keeps growing. |
| 66 | rows = [r for r in rows if r["id"] == "view_journal_detail"] |
| 67 | assert len(rows) == 1 |
| 68 | view = rows[0] |
| 69 | assert view["id"] == "view_journal_detail" |
| 70 | assert view["route"] == "/journals/{journal_id}" |
| 71 | blocks = json.loads(view["blocks"]) |
| 72 | kinds = [b["kind"] for b in blocks] |
| 73 | # An `aggregate` balance block restores the bespoke page's debit/credit |
| 74 | # totals + Balanced badge, which no other block could express. |
| 75 | assert kinds == ["detail", "aggregate", "related", "form"] |
| 76 | |
| 77 | detail, aggregate, related, form = blocks |
| 78 | |
| 79 | # 1. editable+deletable journal detail (status edit replaces the |
| 80 | # bespoke one-click draft->posted Post button) |
| 81 | assert detail["collection"] == "fin_journals" |
| 82 | assert detail["record_id"] == "$record_id" |
| 83 | assert detail["editable"] is True and detail["deletable"] is True |
| 84 | assert detail["delete_redirect"] == "/journals" |
| 85 | |
| 86 | # 2. balance summary: sums of the postings' debit/credit cents + a |
| 87 | # Balanced/Not badge (debit_cents total must equal credit_cents total). |
| 88 | assert aggregate["kind"] == "aggregate" |
| 89 | assert aggregate["collection"] == "fin_journal_lines" |
| 90 | assert aggregate["fk_field"] == "journal_id" |
| 91 | assert aggregate["match"] == "$record_id" |
| 92 | assert aggregate["sums"] == ["debit_cents", "credit_cents"] |
| 93 | assert aggregate["balance"] == ["debit_cents", "credit_cents"] |
| 94 | |
| 95 | # 3. postings, relational (fin_journal_lines by journal_id -- not embedded) |
| 96 | assert related["collection"] == "fin_journal_lines" |
| 97 | assert related["fk_field"] == "journal_id" |
| 98 | assert related["match"] == "$record_id" |
| 99 | assert related["title"] == "Postings" |
| 100 | |
| 101 | # 4. add-posting form, journal_id locked to the page's journal |
| 102 | assert form["collection"] == "fin_journal_lines" |
| 103 | assert form["fixed"] == {"journal_id": "$record_id"} |
| 104 | assert form["title"] == "Add Posting" |
| 105 | |
| 106 | |
| 107 | def test_journal_route_points_to_view_render_and_setup_accounts_route_survives(): |
| 108 | rows = _seed_rows("site_routes") |
| 109 | # the setup-accounts route (65 slice 4) must still be first -- this |
| 110 | # retrofit APPENDS, it never overwrites the existing site_routes.tsv. |
| 111 | assert rows[0]["pattern"] == "/finance/setup-accounts" |
| 112 | assert rows[0]["object_id"] == "site_setup_accounts" |
| 113 | # 0.6.0 appended /statements; this file's point is that the retrofit |
| 114 | # APPENDS rather than overwriting, so assert the journal route's |
| 115 | # position rather than the total count, which will keep growing. |
| 116 | assert len(rows) >= 2 |
| 117 | assert rows[1]["id"] == "route_journal_detail" |
| 118 | assert rows[1]["pattern"] == "/journals/{journal_id}" |
| 119 | assert rows[1]["object_id"] == "site_view_render" |
| 120 | assert rows[1]["priority"] == "10" |
| 121 | |
| 122 | |
| 123 | def test_permissions_no_longer_reference_the_removed_object(): |
| 124 | payload = json.loads((APP_FINANCE_DIR / "permissions" / "rules.json").read_text()) |
| 125 | object_ids = {rule.get("object_id") for rule in payload["rules"]} |
| 126 | assert "site_journal_view" not in object_ids |
| 127 | assert "site_accounts" in object_ids |
| 128 | assert "site_journals" in object_ids |
| 129 | assert "site_trial_balance" in object_ids |
| 130 | assert "site_setup_accounts" in object_ids |
| 131 | |
| 132 | |
| 133 | def test_no_public_read_survives_the_retrofit(): |
| 134 | """Journals/postings stay owner-private: the retrofit must not have |
| 135 | added a public read rule on any collection.""" |
| 136 | payload = json.loads((APP_FINANCE_DIR / "permissions" / "rules.json").read_text()) |
| 137 | policy = object_permissions.policy_from_dict( |
| 138 | {"access_mode": "role_based", "rules": payload["rules"]} |
| 139 | ) |
| 140 | for collection in ("fin_accounts", "fin_journals", "fin_journal_lines", "fin_recurring"): |
| 141 | decision = object_permissions.check_permission( |
| 142 | None, object_permissions.READ, policy=policy, collection=collection, |
| 143 | record={"owner_id": "7"}, |
| 144 | ) |
| 145 | assert decision.allowed is False, f"anonymous read should be denied on {collection}" |
| 146 | |
| 147 | |
| 148 | def test_dry_run_is_safe(tmp_path): |
| 149 | object_root = tmp_path / "objects" |
| 150 | object_root.mkdir() |
| 151 | plan = object_packages.dry_run_package( |
| 152 | "app-finance", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 153 | ) |
| 154 | assert plan["safe_to_install"] is True |
| 155 | assert plan["warnings"] == [] |