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_cash_counts.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_cash_counts.py · 8.7 KB · 207 lines
1"""Counting the accounts nobody sends a statement for
2(plan/value-accounts-and-denominations-spec.md §4-5).
3
4A till is real money with no independent witness, so the interesting tests
5are not the arithmetic -- they are the controls: a witness can be required,
6the witness cannot be the counter, and a variance is booked as a real
7expense rather than quietly dropped.
8"""
9
10import pathlib
11
12import object_execution
13import object_records
14import python_object_runtime
15from conftest import stage_collection
16
17REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
18PACKAGES = REPO_ROOT / "packages"
19BANKING_OBJECTS = PACKAGES / "app-banking" / "objects"
20RUNTIME = python_object_runtime.PythonObjectRuntime()
21
22CASH, OVER_SHORT = "acct-cash", "acct-over-short"
23TILL = "va-till"
24
25
26def setup_env(tmp_path, monkeypatch, *, settings=(("reconcile.journal.cash_over_short_account", OVER_SHORT),)):
27 data_dir = tmp_path / "data"
28 for pkg, name in (("app-banking", "value_accounts"),
29 ("app-banking", "value_account_counts"),
30 ("app-finance", "fin_accounts"),
31 ("app-finance", "fin_journals"),
32 ("app-finance", "fin_journal_lines"),
33 ("app-finance", "denominations")):
34 stage_collection(data_dir, pkg, name)
35 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
36
37 rows = ""
38 for i, (k, v) in enumerate(settings):
39 rows += f"s{i}\t{k}\t{v}\t\n"
40 stage_collection(data_dir, "app-settings", "app_settings", rows=rows)
41
42 for acct, kind in ((CASH, "asset"), (OVER_SHORT, "expense")):
43 object_records.create_collection_record(
44 "fin_accounts", {"id": acct, "name": acct, "account_type": kind,
45 "owner_id": "dan"}, base_dir=data_dir)
46 return data_dir
47
48
49def make_till(data_dir, *, requires_witness=False, verification="physical_count",
50 aid=TILL):
51 return object_records.create_collection_record(
52 "value_accounts",
53 {"id": aid, "name": "Front Register", "kind": "cash_box",
54 "fin_account_id": CASH, "custody": "self", "custodian": "front register",
55 "verification": verification,
56 "requires_second_attestor": "true" if requires_witness else "false",
57 "owner_id": "dan"},
58 base_dir=data_dir)
59
60
61def book_cash(data_dir, amount_minor, *, jid="j-open", on="2026-07-01"):
62 """Put a posted balance in the till's book account."""
63 object_records.create_collection_record(
64 "fin_journals",
65 {"id": jid, "date": on, "description": "Opening float", "status": "posted",
66 "kind": "standard", "owner_id": "dan"}, base_dir=data_dir)
67 object_records.create_collection_record(
68 "fin_journal_lines",
69 {"id": f"{jid}-dr", "journal_id": jid, "account_id": CASH,
70 "debit_cents": str(amount_minor), "credit_cents": "0", "owner_id": "dan"},
71 base_dir=data_dir)
72 object_records.create_collection_record(
73 "fin_journal_lines",
74 {"id": f"{jid}-cr", "journal_id": jid, "account_id": OVER_SHORT,
75 "debit_cents": "0", "credit_cents": str(amount_minor), "owner_id": "dan"},
76 base_dir=data_dir)
77
78
79def count(payload, *, user="dan"):
80 return object_execution.execute_object(
81 RUNTIME,
82 object_execution.ObjectExecutionRequest(
83 "action_record_count", method="POST",
84 payload={"_identity": {"user_id": user}, **payload}),
85 roots=[BANKING_OBJECTS]).result
86
87
88def counts(data_dir):
89 return object_records.read_collection_records("value_account_counts", base_dir=data_dir)
90
91
92def journal_lines(data_dir, journal_id):
93 return {l["account_id"]: l for l in
94 object_records.read_collection_records("fin_journal_lines", base_dir=data_dir)
95 if l["journal_id"] == journal_id}
96
97
98def test_a_matching_count_records_evidence_and_books_nothing(tmp_path, monkeypatch):
99 data_dir = setup_env(tmp_path, monkeypatch)
100 make_till(data_dir)
101 book_cash(data_dir, 20000)
102 result = count({"value_account_id": TILL, "counted_minor": 20000,
103 "counted_on": "2026-07-31"})
104 assert result["status"] == 200 and result["variance_minor"] == 0
105 assert result["journal"] is None
106 row = counts(data_dir)[0]
107 # The book figure is stamped, so the count stays meaningful later.
108 assert row["book_balance_minor"] == "20000"
109 assert row["counted_by"] == "dan"
110
111
112def test_a_shortage_books_cash_over_short(tmp_path, monkeypatch):
113 """The drawer is light: that is an expense the business really incurred,
114 and booking it is what makes a slow leak visible as a number."""
115 data_dir = setup_env(tmp_path, monkeypatch)
116 make_till(data_dir)
117 book_cash(data_dir, 20000)
118 result = count({"value_account_id": TILL, "counted_minor": 19750,
119 "counted_on": "2026-07-31"})
120 assert result["variance_minor"] == -250
121 assert result["journal"]["posted"] is True
122 booked = journal_lines(data_dir, result["journal"]["journal_id"])
123 assert booked[OVER_SHORT]["debit_cents"] == "250" # expense up
124 assert booked[CASH]["credit_cents"] == "250" # cash down
125 assert counts(data_dir)[0]["journal_id"] == result["journal"]["journal_id"]
126
127
128def test_an_overage_books_the_mirror(tmp_path, monkeypatch):
129 data_dir = setup_env(tmp_path, monkeypatch)
130 make_till(data_dir)
131 book_cash(data_dir, 20000)
132 result = count({"value_account_id": TILL, "counted_minor": 20100})
133 assert result["variance_minor"] == 100
134 booked = journal_lines(data_dir, result["journal"]["journal_id"])
135 assert booked[CASH]["debit_cents"] == "100"
136 assert booked[OVER_SHORT]["credit_cents"] == "100"
137
138
139def test_an_account_can_refuse_a_self_certified_count(tmp_path, monkeypatch):
140 """The oldest control in retail, as one field: the person counting the
141 till is usually the person who could take from it."""
142 data_dir = setup_env(tmp_path, monkeypatch)
143 make_till(data_dir, requires_witness=True)
144 book_cash(data_dir, 20000)
145 unwitnessed = count({"value_account_id": TILL, "counted_minor": 19750})
146 assert unwitnessed["status"] == 409 and "witness" in unwitnessed["error"]
147 assert counts(data_dir) == []
148
149 witnessed = count({"value_account_id": TILL, "counted_minor": 19750,
150 "witnessed_by": "pat"})
151 assert witnessed["status"] == 200
152 assert witnessed["assurance"] == "medium" # lifted, but still not a statement
153 assert counts(data_dir)[0]["witnessed_by"] == "pat"
154
155
156def test_the_witness_cannot_be_the_counter(tmp_path, monkeypatch):
157 data_dir = setup_env(tmp_path, monkeypatch)
158 make_till(data_dir, requires_witness=True)
159 book_cash(data_dir, 20000)
160 result = count({"value_account_id": TILL, "counted_minor": 19750,
161 "witnessed_by": "dan"}, user="dan")
162 assert result["status"] == 409
163 assert "other than the person counting" in result["error"]
164
165
166def test_an_unwitnessed_count_is_rated_weakest(tmp_path, monkeypatch):
167 data_dir = setup_env(tmp_path, monkeypatch)
168 make_till(data_dir) # witness not required
169 book_cash(data_dir, 20000)
170 result = count({"value_account_id": TILL, "counted_minor": 20000})
171 assert result["assurance"] == "weak" # self-certification, and it says so
172
173
174def test_a_variance_with_nowhere_to_go_is_refused(tmp_path, monkeypatch):
175 """Better to refuse than to record a count whose difference silently
176 never reaches the ledger."""
177 data_dir = setup_env(tmp_path, monkeypatch, settings=())
178 make_till(data_dir)
179 book_cash(data_dir, 20000)
180 result = count({"value_account_id": TILL, "counted_minor": 19750})
181 assert result["status"] == 409 and "cash_over_short_account" in result["error"]
182 assert counts(data_dir) == []
183
184
185def test_only_counted_accounts_can_be_counted(tmp_path, monkeypatch):
186 data_dir = setup_env(tmp_path, monkeypatch)
187 make_till(data_dir, verification="statement_import", aid="va-bank")
188 result = count({"value_account_id": "va-bank", "counted_minor": 100})
189 assert result["status"] == 409
190 assert "Import its statement instead" in result["error"]
191
192
193def test_refusals(tmp_path, monkeypatch):
194 data_dir = setup_env(tmp_path, monkeypatch)
195 make_till(data_dir)
196 assert count({"value_account_id": TILL})["status"] == 400
197 assert count({"value_account_id": TILL, "counted_minor": -5})["status"] == 400
198 assert count({"value_account_id": "nope", "counted_minor": 100})["status"] == 404
199 other = count({"value_account_id": TILL, "counted_minor": 100}, user="mallory")
200 assert other["status"] == 403
201 anon = object_execution.execute_object(
202 RUNTIME,
203 object_execution.ObjectExecutionRequest(
204 "action_record_count", method="POST",
205 payload={"value_account_id": TILL, "counted_minor": 100}),
206 roots=[BANKING_OBJECTS]).result
207 assert anon["status"] == 403