Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_app_finance_detail_retrofit.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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