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_bank_reconciliation_view.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_bank_reconciliation_view.py · 16.1 KB · 335 lines
1"""The bank reconciliation statement (plan/bank-import-reconciliation-spec.md
2section 6): object_banking.reconciliation()'s fold math, and site_reconcile,
3the page that renders it.
4
5The control under test is the same one tests/test_bank_reconciliation.py
6checks for the matcher and resolution verbs: nobody -- not an anonymous
7visitor, not another signed-in owner -- can see a bank account's
8reconciliation state except the owner who set it up. The math itself is
9the classic tie: bank closing balance minus book balance should equal
10exactly the outstanding timing items once the unmatched tail is empty.
11"""
12
13import json
14import pathlib
15
16import object_banking
17import object_execution
18import object_records
19import python_object_runtime
20from conftest import stage_collection
21
22REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
23PACKAGES = REPO_ROOT / "packages"
24BANKING_OBJECTS = PACKAGES / "app-banking" / "objects"
25RUNTIME = python_object_runtime.PythonObjectRuntime()
26
27DAN = {"user_id": "dan"}
28PAT = {"user_id": "pat"}
29
30
31def setup_env(tmp_path, monkeypatch):
32 data_dir = tmp_path / "data"
33 wanted = [("app-banking", "value_accounts"), ("app-banking", "bank_statement_imports"),
34 ("app-banking", "bank_lines"),
35 ("app-finance", "fin_accounts"), ("app-finance", "fin_journals"),
36 ("app-finance", "fin_journal_lines")]
37 for pkg, name in wanted:
38 stage_collection(data_dir, pkg, name)
39 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
40 return data_dir
41
42
43def make_fin_account(data_dir, aid, owner="dan"):
44 return object_records.create_collection_record(
45 "fin_accounts", {"id": aid, "name": aid, "account_type": "asset", "owner_id": owner},
46 base_dir=data_dir)
47
48
49def make_bank_account(data_dir, bid, *, fin_account_id, owner="dan", name=None, currency="USD"):
50 return object_records.create_collection_record(
51 "value_accounts",
52 {"id": bid, "name": name or bid, "fin_account_id": fin_account_id,
53 "owner_id": owner, "kind": "bank"},
54 base_dir=data_dir)
55
56
57def make_import(data_dir, iid, *, bank_account_id, period_end, closing_cents,
58 opening_cents=0, status="accepted", flags=None, owner="dan"):
59 return object_records.create_collection_record(
60 "bank_statement_imports",
61 {"id": iid, "bank_account_id": bank_account_id, "period_start": "2026-07-01",
62 "period_end": period_end, "opening_balance_cents": str(opening_cents),
63 "closing_balance_cents": str(closing_cents), "status": status,
64 "flags": json.dumps(flags or {}), "owner_id": owner},
65 base_dir=data_dir)
66
67
68def make_line(data_dir, lid, *, bank_account_id, amount, on="2026-07-02", desc="",
69 status="unmatched", resolved_as="", owner="dan", suggestions="",
70 matched_to=""):
71 # A matched line names its target (bank_lines.matched_to required_when).
72 matched_to = matched_to or ("payments/p1" if status == "matched" else "")
73 return object_records.create_collection_record(
74 "bank_lines",
75 {"id": lid, "bank_account_id": bank_account_id, "posted_on": on,
76 "amount_cents": str(amount), "description": desc, "raw": f"raw:{desc}",
77 "line_hash": f"h-{lid}", "match_status": status, "resolved_as": resolved_as,
78 "matched_to": matched_to, "suggestions": suggestions, "owner_id": owner},
79 base_dir=data_dir)
80
81
82def make_journal(data_dir, jid, *, date="2026-07-05", status="posted", owner="dan"):
83 return object_records.create_collection_record(
84 "fin_journals", {"id": jid, "date": date, "status": status, "owner_id": owner},
85 base_dir=data_dir)
86
87
88def make_journal_line(data_dir, lid, *, journal_id, account_id, debit=0, credit=0, owner="dan"):
89 return object_records.create_collection_record(
90 "fin_journal_lines",
91 {"id": lid, "journal_id": journal_id, "account_id": account_id,
92 "debit_cents": str(debit), "credit_cents": str(credit), "owner_id": owner},
93 base_dir=data_dir)
94
95
96def page(method, payload):
97 result = object_execution.execute_object(
98 RUNTIME,
99 object_execution.ObjectExecutionRequest(
100 "site_reconcile", method=method, payload=payload),
101 roots=[BANKING_OBJECTS])
102 return result.result
103
104
105# --- reconciliation() fold math ----------------------------------------------
106
107def test_no_data_folds_to_all_nones_never_raises(tmp_path, monkeypatch):
108 setup_env(tmp_path, monkeypatch)
109 result = object_banking.reconciliation("no-such-account", base_dir=tmp_path / "data")
110 assert result["bank_closing_cents"] is None
111 assert result["book_balance_cents"] is None
112 assert result["difference_cents"] is None
113 assert result["reconciled"] is False
114 assert result["assurance"] is None
115
116
117def test_bank_closing_book_balance_and_difference(tmp_path, monkeypatch):
118 data_dir = setup_env(tmp_path, monkeypatch)
119 make_fin_account(data_dir, "cash")
120 make_bank_account(data_dir, "bank-1", fin_account_id="cash")
121 make_import(data_dir, "imp1", bank_account_id="bank-1",
122 period_end="2026-07-31", closing_cents=100000,
123 flags={"tie_out": {"ran": True, "passed": True}})
124 # Book side: one posted journal debiting cash 90000.
125 make_journal(data_dir, "j1")
126 make_journal_line(data_dir, "jl1", journal_id="j1", account_id="cash", debit=90000)
127
128 result = object_banking.reconciliation("bank-1", base_dir=data_dir)
129 assert result["bank_closing_cents"] == 100000
130 assert result["bank_statement_date"] == "2026-07-31"
131 assert result["book_balance_cents"] == 90000
132 assert result["difference_cents"] == 10000
133
134
135def test_reconciled_true_when_difference_equals_timing_items(tmp_path, monkeypatch):
136 data_dir = setup_env(tmp_path, monkeypatch)
137 make_fin_account(data_dir, "cash")
138 make_bank_account(data_dir, "bank-1", fin_account_id="cash")
139 make_import(data_dir, "imp1", bank_account_id="bank-1",
140 period_end="2026-07-31", closing_cents=100000,
141 flags={"tie_out": {"ran": True, "passed": True}})
142 make_journal(data_dir, "j1")
143 make_journal_line(data_dir, "jl1", journal_id="j1", account_id="cash", debit=90000)
144 # A deposit-in-transit: on the statement (part of the 100000 closing
145 # balance) but never booked -- classic outstanding timing item.
146 make_line(data_dir, "L1", bank_account_id="bank-1", amount=10000, desc="DEPOSIT IN TRANSIT",
147 status="resolved", resolved_as="timing")
148
149 result = object_banking.reconciliation("bank-1", base_dir=data_dir)
150 assert result["timing_cents"] == 10000
151 assert result["timing_count"] == 1
152 assert result["difference_cents"] == 10000
153 assert result["reconciled"] is True
154
155
156def test_unmatched_tail_breaks_reconciliation(tmp_path, monkeypatch):
157 data_dir = setup_env(tmp_path, monkeypatch)
158 make_fin_account(data_dir, "cash")
159 make_bank_account(data_dir, "bank-1", fin_account_id="cash")
160 make_import(data_dir, "imp1", bank_account_id="bank-1",
161 period_end="2026-07-31", closing_cents=100000,
162 flags={"tie_out": {"ran": True, "passed": True}})
163 make_journal(data_dir, "j1")
164 make_journal_line(data_dir, "jl1", journal_id="j1", account_id="cash", debit=90000)
165 # Same 10000 gap, but this time it is an unexplained unmatched line,
166 # not a timing item -- the numbers still differ by 10000, but the
167 # difference is not EXPLAINED, so reconciled must be False.
168 make_line(data_dir, "L1", bank_account_id="bank-1", amount=10000, desc="MYSTERY DEPOSIT",
169 status="unmatched")
170
171 result = object_banking.reconciliation("bank-1", base_dir=data_dir)
172 assert result["unmatched_cents"] == 10000
173 assert result["unmatched_count"] == 1
174 assert result["difference_cents"] == 10000
175 assert result["timing_cents"] == 0
176 assert result["reconciled"] is False
177
178
179def test_matched_and_resolved_non_timing_lines_count_as_reconciled(tmp_path, monkeypatch):
180 data_dir = setup_env(tmp_path, monkeypatch)
181 make_bank_account(data_dir, "bank-1", fin_account_id="")
182 make_line(data_dir, "L1", bank_account_id="bank-1", amount=5000, status="matched")
183 make_line(data_dir, "L2", bank_account_id="bank-1", amount=-2500, status="resolved", resolved_as="fee")
184 make_line(data_dir, "L3", bank_account_id="bank-1", amount=200, status="suggested")
185 make_line(data_dir, "L4", bank_account_id="bank-1", amount=100, status="unmatched")
186
187 result = object_banking.reconciliation("bank-1", base_dir=data_dir)
188 assert result["matched_cents"] == 5000 + (-2500)
189 assert result["unmatched_cents"] == 200 + 100
190 assert result["suggested_count"] == 1
191 assert result["unmatched_count"] == 1
192 # No fin_account_id on this bank account -- book_balance_cents is
193 # genuinely unknown, not zero.
194 assert result["book_balance_cents"] is None
195 assert result["difference_cents"] is None
196
197
198def test_as_of_scopes_imports_and_journals(tmp_path, monkeypatch):
199 data_dir = setup_env(tmp_path, monkeypatch)
200 make_fin_account(data_dir, "cash")
201 make_bank_account(data_dir, "bank-1", fin_account_id="cash")
202 make_import(data_dir, "imp1", bank_account_id="bank-1",
203 period_end="2026-06-30", closing_cents=50000,
204 flags={"tie_out": {"ran": True, "passed": True}})
205 make_import(data_dir, "imp2", bank_account_id="bank-1",
206 period_end="2026-07-31", closing_cents=100000,
207 flags={"tie_out": {"ran": True, "passed": True}})
208 make_journal(data_dir, "j1", date="2026-06-15")
209 make_journal_line(data_dir, "jl1", journal_id="j1", account_id="cash", debit=50000)
210 make_journal(data_dir, "j2", date="2026-07-15")
211 make_journal_line(data_dir, "jl2", journal_id="j2", account_id="cash", debit=50000)
212
213 as_of_june = object_banking.reconciliation("bank-1", base_dir=data_dir, as_of="2026-06-30")
214 assert as_of_june["bank_closing_cents"] == 50000
215 assert as_of_june["book_balance_cents"] == 50000
216
217 unscoped = object_banking.reconciliation("bank-1", base_dir=data_dir)
218 assert unscoped["bank_closing_cents"] == 100000
219 assert unscoped["book_balance_cents"] == 100000
220
221
222# --- assurance levels ---------------------------------------------------------
223
224def test_assurance_verified_when_tie_out_passed(tmp_path, monkeypatch):
225 data_dir = setup_env(tmp_path, monkeypatch)
226 make_bank_account(data_dir, "bank-1", fin_account_id="")
227 make_import(data_dir, "imp1", bank_account_id="bank-1", period_end="2026-07-31",
228 closing_cents=100000, flags={"tie_out": {"ran": True, "passed": True}})
229 assert object_banking.reconciliation("bank-1", base_dir=data_dir)["assurance"] == "verified"
230
231
232def test_assurance_unverified_when_tie_out_did_not_run(tmp_path, monkeypatch):
233 data_dir = setup_env(tmp_path, monkeypatch)
234 make_bank_account(data_dir, "bank-1", fin_account_id="")
235 make_import(data_dir, "imp1", bank_account_id="bank-1", period_end="2026-07-31",
236 closing_cents=100000,
237 flags={"tie_out": {"ran": False, "reason": "statement carried no balances"}})
238 assert object_banking.reconciliation("bank-1", base_dir=data_dir)["assurance"] == "unverified"
239
240
241def test_assurance_flagged_when_a_check_failed(tmp_path, monkeypatch):
242 data_dir = setup_env(tmp_path, monkeypatch)
243 make_bank_account(data_dir, "bank-1", fin_account_id="")
244 make_import(data_dir, "imp1", bank_account_id="bank-1", period_end="2026-07-31",
245 closing_cents=90000, status="flagged",
246 flags={"tie_out": {"ran": True, "passed": False, "delta_cents": -10000}})
247 assert object_banking.reconciliation("bank-1", base_dir=data_dir)["assurance"] == "flagged"
248
249
250def test_assurance_reflects_latest_import_even_when_bank_closing_falls_back(tmp_path, monkeypatch):
251 """The newest statement failed tie-out (flagged), so its own balance is
252 not trustworthy enough to report as bank_closing_cents -- that falls
253 back to the last accepted statement. But assurance must still surface
254 the flagged state of the NEWEST statement; silently showing 'verified'
255 off an old import would hide exactly the anti-fraud signal this report
256 exists to raise."""
257 data_dir = setup_env(tmp_path, monkeypatch)
258 make_bank_account(data_dir, "bank-1", fin_account_id="")
259 make_import(data_dir, "imp1", bank_account_id="bank-1", period_end="2026-06-30",
260 closing_cents=50000, status="accepted",
261 flags={"tie_out": {"ran": True, "passed": True}})
262 make_import(data_dir, "imp2", bank_account_id="bank-1", period_end="2026-07-31",
263 closing_cents=999999, status="flagged",
264 flags={"tie_out": {"ran": True, "passed": False, "delta_cents": -500}})
265
266 result = object_banking.reconciliation("bank-1", base_dir=data_dir)
267 assert result["bank_closing_cents"] == 50000 # last trustworthy number
268 assert result["bank_statement_date"] == "2026-06-30"
269 assert result["assurance"] == "flagged" # but the newest import failed
270
271
272# --- the page: identity gate + owner scoping ----------------------------------
273
274def test_anonymous_visitor_sees_a_sign_in_prompt_and_no_data(tmp_path, monkeypatch):
275 data_dir = setup_env(tmp_path, monkeypatch)
276 make_fin_account(data_dir, "cash")
277 make_bank_account(data_dir, "bank-1", fin_account_id="cash", name="Dan's Checking")
278 make_line(data_dir, "L1", bank_account_id="bank-1", amount=5000, desc="SECRET DEPOSIT")
279
280 body = page("GET", {})["body"]
281 assert "Sign in" in body
282 assert "Dan's Checking" not in body
283 assert "SECRET DEPOSIT" not in body
284
285
286def test_another_owners_account_and_lines_never_appear(tmp_path, monkeypatch):
287 data_dir = setup_env(tmp_path, monkeypatch)
288 make_fin_account(data_dir, "cash-dan", owner="dan")
289 make_fin_account(data_dir, "cash-pat", owner="pat")
290 make_bank_account(data_dir, "bank-dan", fin_account_id="cash-dan", owner="dan", name="Dan Checking")
291 make_bank_account(data_dir, "bank-pat", fin_account_id="cash-pat", owner="pat", name="Pat Checking")
292 make_line(data_dir, "Ldan", bank_account_id="bank-dan", amount=1000, desc="DAN LINE", owner="dan")
293 make_line(data_dir, "Lpat", bank_account_id="bank-pat", amount=2000, desc="PAT LINE", owner="pat")
294
295 dan_body = page("GET", {"_identity": DAN})["body"]
296 assert "Dan Checking" in dan_body
297 assert "Pat Checking" not in dan_body
298
299 # Pat tries to view Dan's account by guessing its id in the query
300 # string -- the page must ignore it rather than looking it up.
301 pat_body_snooping = page("GET", {"_identity": PAT, "account": "bank-dan"})["body"]
302 assert "Dan Checking" not in pat_body_snooping
303 assert "DAN LINE" not in pat_body_snooping
304 assert "not found among yours" in pat_body_snooping
305
306 # And when Pat legitimately opens their own account, only their own
307 # lines show up.
308 pat_body_own = page("GET", {"_identity": PAT, "account": "bank-pat"})["body"]
309 assert "Pat Checking" in pat_body_own
310 assert "PAT LINE" in pat_body_own
311 assert "DAN LINE" not in pat_body_own
312
313
314def test_page_renders_the_statement_for_a_selected_account(tmp_path, monkeypatch):
315 data_dir = setup_env(tmp_path, monkeypatch)
316 make_fin_account(data_dir, "cash")
317 make_bank_account(data_dir, "bank-1", fin_account_id="cash", name="Business Checking")
318 make_import(data_dir, "imp1", bank_account_id="bank-1", period_end="2026-07-31",
319 closing_cents=100000, flags={"tie_out": {"ran": True, "passed": True}})
320 make_journal(data_dir, "j1")
321 make_journal_line(data_dir, "jl1", journal_id="j1", account_id="cash", debit=90000)
322 make_line(data_dir, "L1", bank_account_id="bank-1", amount=10000, desc="DEPOSIT IN TRANSIT",
323 status="resolved", resolved_as="timing")
324 make_line(data_dir, "L2", bank_account_id="bank-1", amount=500, desc="UNKNOWN CREDIT",
325 status="suggested", suggestions=json.dumps(
326 [{"tier": 2, "refs": ["payments/p1"], "why": "exact amount within 3 day(s)"}]))
327
328 body = page("GET", {"_identity": DAN, "account": "bank-1"})["body"]
329 assert "Business Checking" in body
330 assert "USD 1,000.00" in body # bank closing balance
331 assert "USD 900.00" in body # book balance
332 assert "Reconciled" in body
333 assert "DEPOSIT IN TRANSIT" in body
334 assert "UNKNOWN CREDIT" in body
335 assert "exact amount within 3 day(s)" in body