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_value_accounts.py · 6.3 KB · 128 lines
| 1 | """Value accounts (plan/value-accounts-and-denominations-spec.md §3, §5): |
| 2 | the places value actually sits, and how much a reconciliation of each is |
| 3 | worth. |
| 4 | |
| 5 | The schema is doing real work here, so it is worth testing directly: the |
| 6 | difference between a bank account and a box of cash is not cosmetic, and |
| 7 | the assurance ladder is the part a fraud would exploit if we flattened it. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import pathlib |
| 12 | |
| 13 | import object_money |
| 14 | import object_records |
| 15 | from conftest import stage_collection |
| 16 | |
| 17 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 18 | PACKAGES = REPO_ROOT / "packages" |
| 19 | BANKING = PACKAGES / "app-banking" |
| 20 | FINANCE = PACKAGES / "app-finance" |
| 21 | |
| 22 | |
| 23 | def setup_env(tmp_path, monkeypatch): |
| 24 | data_dir = tmp_path / "data" |
| 25 | stage_collection(data_dir, "app-banking", "value_accounts") |
| 26 | stage_collection(data_dir, "app-finance", "denominations", seed=True) |
| 27 | stage_collection(data_dir, "app-finance", "fin_accounts") |
| 28 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 29 | object_records.create_collection_record( |
| 30 | "fin_accounts", {"id": "acct-cash", "name": "Cash", "account_type": "asset", |
| 31 | "owner_id": "dan"}, base_dir=data_dir) |
| 32 | return data_dir |
| 33 | |
| 34 | |
| 35 | def make_account(data_dir, aid, **fields): |
| 36 | record = {"id": aid, "name": fields.pop("name", aid), "owner_id": "dan"} |
| 37 | record.update(fields) |
| 38 | return object_records.create_collection_record( |
| 39 | "value_accounts", record, base_dir=data_dir) |
| 40 | |
| 41 | |
| 42 | def test_the_four_custody_shapes_all_store(tmp_path, monkeypatch): |
| 43 | """One collection has to hold a bank account, a hardware wallet, a till |
| 44 | and a gift card without any of them being a special case.""" |
| 45 | data_dir = setup_env(tmp_path, monkeypatch) |
| 46 | make_account(data_dir, "va-bank", name="Business Checking", kind="bank", |
| 47 | denomination_id="den-usd", fin_account_id="acct-cash", |
| 48 | custody="institution", custodian="Chase", reference="1004", |
| 49 | verification="statement_import") |
| 50 | make_account(data_dir, "va-btc", name="Cold Wallet", kind="crypto", |
| 51 | denomination_id="den-btc", fin_account_id="acct-cash", |
| 52 | custody="self", custodian="Ledger in the safe", |
| 53 | reference="bc1qexampleaddress", verification="chain_query", |
| 54 | last_proven_control_at="2026-07-01") |
| 55 | make_account(data_dir, "va-till", name="Front Register", kind="cash_box", |
| 56 | denomination_id="den-usd", fin_account_id="acct-cash", |
| 57 | custody="self", custodian="front register", |
| 58 | verification="physical_count", requires_second_attestor="true", |
| 59 | managed_by="pat") |
| 60 | make_account(data_dir, "va-gift", name="Acme Gift Cards", kind="gift_card", |
| 61 | denomination_id="den-usd", fin_account_id="acct-cash", |
| 62 | custody="third_party", custodian="Acme", |
| 63 | verification="issuer_balance") |
| 64 | |
| 65 | rows = {r["id"]: r for r in |
| 66 | object_records.read_collection_records("value_accounts", base_dir=data_dir)} |
| 67 | assert len(rows) == 4 |
| 68 | assert rows["va-btc"]["custody"] == "self" |
| 69 | assert rows["va-btc"]["last_proven_control_at"] == "2026-07-01" |
| 70 | assert rows["va-till"]["requires_second_attestor"] == "true" |
| 71 | # managed_by is separate from owner_id on purpose: the person who |
| 72 | # reconciles the register need not be the person who can move a wire. |
| 73 | assert rows["va-till"]["managed_by"] == "pat" |
| 74 | assert rows["va-till"]["owner_id"] == "dan" |
| 75 | |
| 76 | |
| 77 | def test_assurance_is_not_flat_across_verification_methods(): |
| 78 | """The ladder that stops the system telling a comfortable lie.""" |
| 79 | assert object_money.assurance_for("chain_query") == "strong" |
| 80 | assert object_money.assurance_for("statement_import") == "strong" |
| 81 | assert object_money.assurance_for("issuer_balance") == "medium" |
| 82 | # Self-certification: the counter is usually the person who could take. |
| 83 | assert object_money.assurance_for("physical_count") == "weak" |
| 84 | assert object_money.assurance_for("none") == "none" |
| 85 | assert object_money.assurance_for("") == "none" |
| 86 | |
| 87 | |
| 88 | def test_a_witness_lifts_a_self_counted_till(tmp_path, monkeypatch): |
| 89 | """A second attestor is the oldest control there is against the count |
| 90 | and the custody being the same pair of hands.""" |
| 91 | assert object_money.assurance_for("physical_count", witnessed=True) == "medium" |
| 92 | # Witnessing does not upgrade evidence that was already independent. |
| 93 | assert object_money.assurance_for("statement_import", witnessed=True) == "strong" |
| 94 | assert object_money.assurance_for("none", witnessed=True) == "none" |
| 95 | |
| 96 | |
| 97 | def test_every_verification_in_the_schema_has_a_ranking(tmp_path, monkeypatch): |
| 98 | """A verification method the ladder does not know would silently rank as |
| 99 | no evidence at all, which is exactly the kind of quiet default that |
| 100 | makes a control useless.""" |
| 101 | schema = json.loads((BANKING / "schemas" / "value_accounts.json").read_text()) |
| 102 | by_name = {f["name"]: f for f in schema["fields"]} |
| 103 | for method in by_name["verification"]["enum"]: |
| 104 | assert method in object_money.ASSURANCE_BY_VERIFICATION, method |
| 105 | |
| 106 | |
| 107 | def test_denomination_drives_how_a_balance_reads(tmp_path, monkeypatch): |
| 108 | """The same integer means different money in different accounts, which |
| 109 | is the whole reason denomination lives on the account.""" |
| 110 | data_dir = setup_env(tmp_path, monkeypatch) |
| 111 | make_account(data_dir, "va-bank", kind="bank", denomination_id="den-usd") |
| 112 | make_account(data_dir, "va-btc", kind="crypto", denomination_id="den-btc") |
| 113 | assert object_money.format_amount(5000000, "USD", base_dir=data_dir) == "$50,000.00 USD" |
| 114 | assert object_money.format_amount(5000000, "BTC", base_dir=data_dir) == "0.05000000 BTC" |
| 115 | |
| 116 | |
| 117 | def test_schema_declares_the_fields_the_spec_requires(tmp_path, monkeypatch): |
| 118 | schema = json.loads((BANKING / "schemas" / "value_accounts.json").read_text()) |
| 119 | by_name = {f["name"]: f for f in schema["fields"]} |
| 120 | for field in ("kind", "denomination_id", "fin_account_id", "custody", |
| 121 | "custodian", "verification", "managed_by", |
| 122 | "requires_second_attestor", "last_proven_control_at"): |
| 123 | assert field in by_name, field |
| 124 | assert by_name["denomination_id"]["relation"]["collection"] == "denominations" |
| 125 | assert by_name["fin_account_id"]["relation"]["collection"] == "fin_accounts" |
| 126 | assert "cash_box" in by_name["kind"]["enum"] |
| 127 | assert "self" in by_name["custody"]["enum"] |
| 128 | assert "chain_query" in by_name["verification"]["enum"] |