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

0 downloads

dbbasic-object-server/tests/test_app_finance_setup_accounts.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_app_finance_setup_accounts.py · 5.1 KB · 134 lines
1"""65 multi-entity slice 4: the Setup Accounts action -- seed an entity's chart
2of accounts from its mode (a faithful port of the predecessor's
3Entity.create_default_accounts / the setup_finance_accounts MCP tool). Idempotent,
4owner-gated.
5"""
6
7import json
8import os
9import tempfile
10import types
11from pathlib import Path
12
13import object_mcp
14import object_packages
15import object_records
16
17PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
18APP_FINANCE_DIR = PACKAGES_ROOT / "app-finance"
19
20
21def _install(tmp_path):
22 data_dir = tmp_path / "data"
23 object_root = tmp_path / "objects"
24 object_root.mkdir()
25 for pkg in ("app-entities", "app-finance", "app-projects"):
26 object_packages.install_package(
27 pkg, root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], allow_replace=True,
28 )
29 return data_dir
30
31
32def _load_object(data_dir, monkeypatch):
33 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
34 src = (APP_FINANCE_DIR / "objects" / "site" / "setup_accounts.py").read_text()
35 mod = types.ModuleType("setup_accounts_under_test")
36 mod._logger = types.SimpleNamespace(info=lambda *a, **k: None)
37 exec(compile(src, "setup_accounts.py", "exec"), mod.__dict__)
38 return mod
39
40
41def _post(mod, user_id, entity_id):
42 req = {"entity_id": entity_id}
43 if user_id is not None:
44 req["_identity"] = {"user_id": user_id}
45 r = mod.POST(req)
46 return r.get("status", 200), json.loads(r["body"])
47
48
49def _entity(data_dir, owner, mode):
50 return object_records.create_collection_record(
51 "entities", {"name": mode.title(), "owner_id": owner, "mode": mode},
52 base_dir=data_dir, actor="test",
53 )["id"]
54
55
56def _accounts_for(data_dir, entity_id):
57 return [a for a in object_records.read_collection_records("fin_accounts", base_dir=data_dir)
58 if a.get("entity_id") == entity_id]
59
60
61def test_each_mode_seeds_its_chart_owned_and_scoped(tmp_path, monkeypatch):
62 data_dir = _install(tmp_path)
63 mod = _load_object(data_dir, monkeypatch)
64 for mode, count in (("simple", 2), ("standard", 15), ("double_entry", 29)):
65 eid = _entity(data_dir, "7", mode)
66 status, body = _post(mod, "7", eid)
67 assert status == 200 and body["mode"] == mode and body["created"] == count
68 accts = _accounts_for(data_dir, eid)
69 assert len(accts) == count
70 # every account is owned by the caller and scoped to this entity
71 assert all(a["owner_id"] == "7" and a["entity_id"] == eid for a in accts)
72 # a known account carries its predecessor code
73 assert all(a["account_type"] in {"asset", "liability", "equity", "income", "expense"} for a in accts)
74
75
76def test_idempotent_second_run_creates_nothing(tmp_path, monkeypatch):
77 data_dir = _install(tmp_path)
78 mod = _load_object(data_dir, monkeypatch)
79 eid = _entity(data_dir, "7", "standard")
80 _post(mod, "7", eid)
81 status, body = _post(mod, "7", eid)
82 assert status == 200 and body["created"] == 0 and body["skipped"] == 15
83 assert len(_accounts_for(data_dir, eid)) == 15 # no duplicates
84
85
86def test_owner_gated(tmp_path, monkeypatch):
87 data_dir = _install(tmp_path)
88 mod = _load_object(data_dir, monkeypatch)
89 eid = _entity(data_dir, "7", "standard")
90 # another owner -> 403, nothing created
91 status, _ = _post(mod, "8", eid)
92 assert status == 403
93 assert _accounts_for(data_dir, eid) == []
94 # anonymous -> 401
95 status, _ = _post(mod, None, eid)
96 assert status == 401
97
98
99def test_unknown_entity_and_missing_id(tmp_path, monkeypatch):
100 data_dir = _install(tmp_path)
101 mod = _load_object(data_dir, monkeypatch)
102 assert _post(mod, "7", "00000000-0000-4000-8000-000000000000")[0] == 404
103 assert mod.POST({"_identity": {"user_id": "7"}})["status"] == 400 # no entity_id
104
105
106def test_package_wires_the_object_route_and_rule():
107 package = object_packages.get_package("app-finance", root=PACKAGES_ROOT)
108 assert "site_setup_accounts" in {o["id"] for o in package["objects"]}
109 assert "site_routes" in {e["collection"] for e in package["seed"]}
110 # dependencies are normalized to {id, version} dicts by object_packages.
111 assert any(d.get("id") == "app-entities" for d in package.get("dependencies", []))
112 rules = json.loads((APP_FINANCE_DIR / "permissions" / "rules.json").read_text())["rules"]
113 assert any(r.get("object_id") == "site_setup_accounts" and r.get("actions") == ["execute"]
114 for r in rules)
115 import csv
116 routes = list(csv.DictReader(open(APP_FINANCE_DIR / "seed" / "site_routes.tsv"), delimiter="\t"))
117 assert routes[0]["pattern"] == "/finance/setup-accounts"
118 assert routes[0]["object_id"] == "site_setup_accounts"
119
120
121def test_mcp_setup_finance_accounts_verb_routes_to_the_object():
122 method, path, _query, body = object_mcp.tool_route(
123 "setup_finance_accounts", {"entity_id": "E1"},
124 )
125 assert method == "POST"
126 assert path == "/admin/objects/site_setup_accounts/execute"
127 assert json.loads(body)["payload"] == {"entity_id": "E1"}
128
129
130def test_mcp_setup_finance_accounts_requires_entity_id():
131 import pytest
132
133 with pytest.raises(ValueError):
134 object_mcp.tool_route("setup_finance_accounts", {})