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_object_finance.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, 18 minutes ago History
test_object_finance.py · 12.3 KB · 292 lines
1"""Behavior tests for object_finance.py: journal_totals() and
2trial_balance(), the pure computed helpers packages/app-finance ships in
3place of a totals-stamping handler (see object_finance.py's module
4docstring for why there is no HANDLES handler in this package).
5
6Structural/manifest/permission tests for packages/app-finance live in
7tests/test_app_finance_package.py.
8"""
9
10import os
11from pathlib import Path
12
13import pytest
14
15import object_finance
16import object_packages
17import object_permissions
18import object_records
19
20PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
21
22
23@pytest.fixture(autouse=True)
24def _objects_root(tmp_path, monkeypatch):
25 """Point object lookup at the root _install installs into. fin_journals
26 and fin_journal_lines declare a locked check (closed periods) that the
27 record layer runs on every write, from the installed hook object; a
28 deployment that installed the package into a root it does not search
29 could not write a journal at all, and these tests write journals."""
30 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(tmp_path / "objects"))
31
32
33def _install(tmp_path):
34 data_dir = tmp_path / "data"
35 object_root = tmp_path / "objects"
36 object_root.mkdir()
37 # Keep DBBASIC_DATA_DIR in sync with the base_dir this test writes
38 # into, same belt-and-suspenders as tests/test_app_invoices_totals.py.
39 os.environ["DBBASIC_DATA_DIR"] = str(data_dir)
40
41 object_packages.install_package(
42 "app-finance", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root],
43 )
44 return data_dir
45
46
47def _make_account(data_dir, **overrides):
48 record = {"name": "Cash", "account_type": "asset", "owner_id": "u1"}
49 record.update(overrides)
50 return object_records.create_collection_record("fin_accounts", record, base_dir=data_dir, actor="test")
51
52
53def _make_default_account(data_dir):
54 """fin_journal_lines.account_id is a validated relation (the target
55 record must already exist -- see object_records._validate_field_
56 relation), so every _make_line() call below needs a real fin_accounts
57 row at id "a_1" (its own default account_id) to point at.
58 """
59 _make_account(data_dir, id="a_1")
60
61
62def _make_journal(data_dir, **overrides):
63 record = {"date": "2026-07-01", "description": "Opening balance", "owner_id": "u1"}
64 record.update(overrides)
65 return object_records.create_collection_record("fin_journals", record, base_dir=data_dir, actor="test")
66
67
68def _make_line(data_dir, **overrides):
69 record = {"journal_id": "j_1", "account_id": "a_1", "owner_id": "u1"}
70 record.update(overrides)
71 return object_records.create_collection_record("fin_journal_lines", record, base_dir=data_dir, actor="test")
72
73
74# -- journal_totals --------------------------------------------------------
75
76def test_journal_totals_folds_debits_and_credits(tmp_path):
77 data_dir = _install(tmp_path)
78 _make_default_account(data_dir)
79 _make_journal(data_dir, id="j_1")
80 _make_line(data_dir, id="l_1", journal_id="j_1", debit_cents="1000", credit_cents="0")
81 _make_line(data_dir, id="l_2", journal_id="j_1", debit_cents="0", credit_cents="1000")
82
83 totals = object_finance.journal_totals("j_1", base_dir=data_dir)
84 assert totals["total_debits_cents"] == 1000
85 assert totals["total_credits_cents"] == 1000
86 assert totals["is_balanced"] is True
87
88
89def test_journal_totals_is_balanced_only_when_debits_equal_credits(tmp_path):
90 data_dir = _install(tmp_path)
91 _make_default_account(data_dir)
92 _make_journal(data_dir, id="j_1")
93 _make_line(data_dir, id="l_1", journal_id="j_1", debit_cents="1500", credit_cents="0")
94 _make_line(data_dir, id="l_2", journal_id="j_1", debit_cents="0", credit_cents="1000")
95
96 totals = object_finance.journal_totals("j_1", base_dir=data_dir)
97 assert totals["total_debits_cents"] == 1500
98 assert totals["total_credits_cents"] == 1000
99 assert totals["is_balanced"] is False
100
101
102def test_journal_totals_ignores_lines_belonging_to_other_journals(tmp_path):
103 data_dir = _install(tmp_path)
104 _make_default_account(data_dir)
105 _make_journal(data_dir, id="j_1")
106 _make_journal(data_dir, id="j_2")
107 _make_line(data_dir, id="l_1", journal_id="j_1", debit_cents="500", credit_cents="500")
108 _make_line(data_dir, id="l_2", journal_id="j_2", debit_cents="9999", credit_cents="1")
109
110 totals = object_finance.journal_totals("j_1", base_dir=data_dir)
111 assert totals["total_debits_cents"] == 500
112 assert totals["total_credits_cents"] == 500
113
114
115def test_journal_totals_zero_lines_is_balanced_at_zero(tmp_path):
116 data_dir = _install(tmp_path)
117 _make_journal(data_dir, id="j_1")
118
119 totals = object_finance.journal_totals("j_1", base_dir=data_dir)
120 assert totals == {
121 "total_debits_cents": 0,
122 "total_credits_cents": 0,
123 "is_balanced": True,
124 }
125
126
127def test_journal_totals_unknown_journal_id_folds_to_zero(tmp_path):
128 data_dir = _install(tmp_path)
129
130 totals = object_finance.journal_totals("does-not-exist", base_dir=data_dir)
131 assert totals["total_debits_cents"] == 0
132 assert totals["total_credits_cents"] == 0
133 assert totals["is_balanced"] is True
134
135
136# -- trial_balance -----------------------------------------------------------
137
138def test_trial_balance_sums_posted_journals_only(tmp_path):
139 """A draft journal's lines are excluded, matching the source's own
140 report semantics (reconciled against a private predecessor-system
141 audit, not part of this repo: reports filter posted lines).
142 """
143 data_dir = _install(tmp_path)
144 _make_account(data_dir, id="a_cash", name="Cash", code="1000", account_type="asset")
145 _make_account(data_dir, id="a_rev", name="Revenue", code="4000", account_type="income")
146
147 _make_journal(data_dir, id="j_posted", status="posted")
148 _make_line(data_dir, id="l_1", journal_id="j_posted", account_id="a_cash",
149 debit_cents="10000", credit_cents="0")
150 _make_line(data_dir, id="l_2", journal_id="j_posted", account_id="a_rev",
151 debit_cents="0", credit_cents="10000")
152
153 _make_journal(data_dir, id="j_draft", status="draft")
154 _make_line(data_dir, id="l_3", journal_id="j_draft", account_id="a_cash",
155 debit_cents="99999", credit_cents="0")
156
157 rows = object_finance.trial_balance(base_dir=data_dir, owner="u1")
158 by_account = {row["account_id"]: row for row in rows}
159
160 assert by_account["a_cash"]["debit_total_cents"] == 10000
161 assert by_account["a_cash"]["credit_total_cents"] == 0
162 assert by_account["a_rev"]["debit_total_cents"] == 0
163 assert by_account["a_rev"]["credit_total_cents"] == 10000
164 # the draft journal's 99999 never shows up anywhere
165 assert all(row["debit_total_cents"] != 99999 for row in rows)
166
167
168def test_trial_balance_excludes_other_owners_journals(tmp_path):
169 data_dir = _install(tmp_path)
170 _make_account(data_dir, id="a_cash", name="Cash", account_type="asset", owner_id="u1")
171 _make_journal(data_dir, id="j_mine", status="posted", owner_id="u1")
172 _make_line(data_dir, id="l_1", journal_id="j_mine", account_id="a_cash",
173 debit_cents="500", credit_cents="0", owner_id="u1")
174
175 _make_journal(data_dir, id="j_theirs", status="posted", owner_id="u2")
176 _make_line(data_dir, id="l_2", journal_id="j_theirs", account_id="a_cash",
177 debit_cents="70000", credit_cents="0", owner_id="u2")
178
179 rows = object_finance.trial_balance(base_dir=data_dir, owner="u1")
180 assert len(rows) == 1
181 assert rows[0]["account_id"] == "a_cash"
182 assert rows[0]["debit_total_cents"] == 500
183
184
185def test_trial_balance_with_no_posted_journals_is_empty(tmp_path):
186 data_dir = _install(tmp_path)
187 _make_account(data_dir, id="a_cash", name="Cash", account_type="asset")
188 _make_journal(data_dir, id="j_1", status="draft")
189 _make_line(data_dir, id="l_1", journal_id="j_1", account_id="a_cash", debit_cents="500")
190
191 rows = object_finance.trial_balance(base_dir=data_dir, owner="u1")
192 assert rows == []
193
194
195def test_trial_balance_owner_none_folds_every_owner(tmp_path):
196 data_dir = _install(tmp_path)
197 _make_account(data_dir, id="a_cash", name="Cash", account_type="asset")
198 _make_journal(data_dir, id="j_1", status="posted", owner_id="u1")
199 _make_line(data_dir, id="l_1", journal_id="j_1", account_id="a_cash", debit_cents="100", owner_id="u1")
200 _make_journal(data_dir, id="j_2", status="posted", owner_id="u2")
201 _make_line(data_dir, id="l_2", journal_id="j_2", account_id="a_cash", debit_cents="200", owner_id="u2")
202
203 rows = object_finance.trial_balance(base_dir=data_dir, owner=None)
204 assert len(rows) == 1
205 assert rows[0]["debit_total_cents"] == 300
206
207
208def test_trial_balance_rows_carry_account_display_fields(tmp_path):
209 data_dir = _install(tmp_path)
210 _make_account(data_dir, id="a_cash", name="Cash", code="1000", account_type="asset")
211 _make_journal(data_dir, id="j_1", status="posted")
212 _make_line(data_dir, id="l_1", journal_id="j_1", account_id="a_cash", debit_cents="100")
213
214 rows = object_finance.trial_balance(base_dir=data_dir, owner="u1")
215 assert rows[0]["account_name"] == "Cash"
216 assert rows[0]["account_code"] == "1000"
217 assert rows[0]["account_type"] == "asset"
218
219
220def test_trial_balance_sorted_in_statement_order_asset_before_income(tmp_path):
221 data_dir = _install(tmp_path)
222 _make_account(data_dir, id="a_rev", name="Revenue", account_type="income")
223 _make_account(data_dir, id="a_cash", name="Cash", account_type="asset")
224 _make_journal(data_dir, id="j_1", status="posted")
225 _make_line(data_dir, id="l_1", journal_id="j_1", account_id="a_rev", credit_cents="100")
226 _make_line(data_dir, id="l_2", journal_id="j_1", account_id="a_cash", debit_cents="100")
227
228 rows = object_finance.trial_balance(base_dir=data_dir, owner="u1")
229 assert [row["account_id"] for row in rows] == ["a_cash", "a_rev"]
230
231
232# -- Guarded draft->posted transition ---------------------------------------
233#
234# Exercised directly against object_records' own guard, the same pattern
235# tests/test_app_invoices_totals.py uses for invoices.status. Confirms the
236# transition is owner-gated ONLY -- no balance check anywhere in the guard
237# (schemas/fin_journals.json's status field help documents this is
238# deliberate, matching the source's own gap).
239
240def test_owner_may_post_a_draft_journal(tmp_path):
241 data_dir = _install(tmp_path)
242 subject = object_permissions.PermissionSubject(user_id="u1")
243 existing = {"status": "draft", "owner_id": "u1"}
244 updated = {"status": "posted", "owner_id": "u1"}
245
246 object_records._validate_field_transitions(
247 "fin_journals", existing, updated, base_dir=data_dir, roots=None, subject=subject,
248 ) # no exception == allowed
249
250
251def test_non_owner_may_not_post_a_draft_journal(tmp_path):
252 data_dir = _install(tmp_path)
253 subject = object_permissions.PermissionSubject(user_id="someone_else")
254 existing = {"status": "draft", "owner_id": "u1"}
255 updated = {"status": "posted", "owner_id": "u1"}
256
257 with pytest.raises(object_records.TransitionNotAllowedError):
258 object_records._validate_field_transitions(
259 "fin_journals", existing, updated, base_dir=data_dir, roots=None, subject=subject,
260 )
261
262
263def test_posted_is_terminal(tmp_path):
264 data_dir = _install(tmp_path)
265 subject = object_permissions.PermissionSubject(user_id="u1")
266 existing = {"status": "posted", "owner_id": "u1"}
267 updated = {"status": "draft", "owner_id": "u1"}
268
269 with pytest.raises(object_records.InvalidRecordPayloadError):
270 object_records._validate_field_transitions(
271 "fin_journals", existing, updated, base_dir=data_dir, roots=None, subject=subject,
272 )
273
274
275def test_posting_a_wildly_unbalanced_journal_is_still_allowed(tmp_path):
276 """The whole point of this package's parity decision: posting never
277 checks total_debits_cents == total_credits_cents. A journal with
278 debits wildly exceeding credits posts exactly like a balanced one.
279 """
280 data_dir = _install(tmp_path)
281 _make_default_account(data_dir)
282 _make_journal(data_dir, id="j_1", status="draft")
283 _make_line(data_dir, id="l_1", journal_id="j_1", debit_cents="999999", credit_cents="1")
284
285 subject = object_permissions.PermissionSubject(user_id="u1")
286 object_records._validate_field_transitions(
287 "fin_journals", {"status": "draft", "owner_id": "u1"}, {"status": "posted", "owner_id": "u1"},
288 base_dir=data_dir, roots=None, subject=subject,
289 ) # no exception == allowed, despite being wildly unbalanced
290
291 totals = object_finance.journal_totals("j_1", base_dir=data_dir)
292 assert totals["is_balanced"] is False