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