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_period_locking.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_period_locking.py · 8.6 KB · 194 lines
1"""Closed periods (the q9-parity control this system lacked): settled
2books stay settled.
3
4The scenario every test here guards: the year is filed, and months later
5someone -- helpful, hurried, or dishonest -- edits last March. Without
6this gate the filed statements and the live books drift apart with nobody
7deciding they should; with it, the only paths are a correcting journal in
8the CURRENT period, or a deliberate, attributed reopening.
9"""
10
11import pathlib
12
13from conftest import stage_collection
14
15import object_execution
16import object_records
17import python_object_runtime
18
19REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
20PACKAGES = REPO_ROOT / "packages"
21FINANCE_OBJECTS = PACKAGES / "app-finance" / "objects"
22RUNTIME = python_object_runtime.PythonObjectRuntime()
23
24
25def setup_env(tmp_path, monkeypatch):
26 data_dir = tmp_path / "data"
27 for name in ("fin_journals", "fin_journal_lines", "fin_accounts",
28 "fin_closed_periods"):
29 stage_collection(data_dir, "app-finance", name)
30 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
31 object_records.create_collection_record(
32 "fin_accounts", {"id": "acct-cash", "name": "Cash", "account_type": "asset",
33 "owner_id": "dan"}, base_dir=data_dir)
34 return data_dir
35
36
37def close_period(data_dir, *, start="2026-01-01", end="2026-03-31", owner="dan",
38 entity="", reason="Q1 filed", pid="cp1"):
39 return object_records.create_collection_record(
40 "fin_closed_periods",
41 {"id": pid, "start_date": start, "end_date": end, "reason": reason,
42 "owner_id": owner, **({"entity_id": entity} if entity else {})},
43 base_dir=data_dir)
44
45
46def make_journal(data_dir, jid, *, date, owner="dan", status="draft"):
47 return object_records.create_collection_record(
48 "fin_journals",
49 {"id": jid, "date": date, "description": f"J {jid}", "status": status,
50 "kind": "standard", "owner_id": owner},
51 base_dir=data_dir)
52
53
54def hook(object_id, payload):
55 return object_execution.execute_object(
56 RUNTIME,
57 object_execution.ObjectExecutionRequest(
58 object_id, method="BEFORE_WRITE", payload=payload),
59 roots=[FINANCE_OBJECTS]).result
60
61
62def journal_hook(action, *, record=None, existing=None, changes=None):
63 return hook("hook_fin_journals", {
64 "action": action, "collection": "fin_journals",
65 "record": record or {}, "existing": existing or {}, "changes": changes or {}})
66
67
68def line_hook(action, *, record=None, existing=None, changes=None):
69 return hook("hook_fin_journal_lines", {
70 "action": action, "collection": "fin_journal_lines",
71 "record": record or {}, "existing": existing or {}, "changes": changes or {}})
72
73
74# --- the journal-side gate ----------------------------------------------------
75
76def test_a_journal_cannot_be_created_inside_a_closed_period(tmp_path, monkeypatch):
77 data_dir = setup_env(tmp_path, monkeypatch)
78 close_period(data_dir)
79 verdict = journal_hook("create", record={
80 "id": "j1", "date": "2026-02-14", "owner_id": "dan"})
81 assert verdict["status"] == 409
82 assert "are closed" in verdict["error"]
83 assert "Q1 filed" in verdict["error"] # the reason travels
84 # And the honest paths are named in the message itself.
85 assert "current period" in verdict["error"]
86 assert "reopen" in verdict["error"]
87
88
89def test_dates_outside_the_period_pass_including_the_boundaries(tmp_path, monkeypatch):
90 data_dir = setup_env(tmp_path, monkeypatch)
91 close_period(data_dir) # Jan 1 .. Mar 31 inclusive
92 ok = journal_hook("create", record={"id": "j1", "date": "2026-04-01",
93 "owner_id": "dan"})
94 assert ok is None
95 on_end = journal_hook("create", record={"id": "j2", "date": "2026-03-31",
96 "owner_id": "dan"})
97 assert on_end["status"] == 409 # end_date is INSIDE
98
99
100def test_a_journal_dated_inside_cannot_be_edited_at_all(tmp_path, monkeypatch):
101 data_dir = setup_env(tmp_path, monkeypatch)
102 # Written before the close: once February is closed no writer can
103 # create a journal dated in it (the locked check), this one included.
104 make_journal(data_dir, "j-old", date="2026-02-01")
105 close_period(data_dir)
106 verdict = journal_hook("update",
107 existing={"id": "j-old", "date": "2026-02-01",
108 "owner_id": "dan"},
109 changes={"description": "just fixing a typo"})
110 assert verdict["status"] == 409 # settled means settled
111
112
113def test_a_date_cannot_be_moved_into_a_closed_period(tmp_path, monkeypatch):
114 data_dir = setup_env(tmp_path, monkeypatch)
115 close_period(data_dir)
116 verdict = journal_hook("update",
117 existing={"id": "j-new", "date": "2026-05-01",
118 "owner_id": "dan"},
119 changes={"date": "2026-02-01"})
120 assert verdict["status"] == 409 # backdating is the classic move
121
122
123def test_other_owners_and_other_entities_are_untouched(tmp_path, monkeypatch):
124 data_dir = setup_env(tmp_path, monkeypatch)
125 close_period(data_dir, owner="dan")
126 other = journal_hook("create", record={"id": "j1", "date": "2026-02-14",
127 "owner_id": "pat"})
128 assert other is None # dan's filing binds only dan
129
130 stage_collection(data_dir, "app-entities", "entities")
131 object_records.create_collection_record(
132 "entities", {"id": "ent-2", "name": "Second Books", "owner_id": "dan"},
133 base_dir=data_dir)
134 scoped = journal_hook("create", record={"id": "j2", "date": "2026-02-14",
135 "owner_id": "dan", "entity_id": "ent-2"})
136 assert scoped is None # entity-blank period != ent-2 books
137
138
139def test_reopening_is_deleting_the_row_and_then_writes_flow_again(tmp_path, monkeypatch):
140 data_dir = setup_env(tmp_path, monkeypatch)
141 close_period(data_dir, pid="cp-q1")
142 blocked = journal_hook("create", record={"id": "j1", "date": "2026-02-14",
143 "owner_id": "dan"})
144 assert blocked["status"] == 409
145 object_records.delete_collection_record("fin_closed_periods", "cp-q1",
146 base_dir=data_dir, actor="dan")
147 reopened = journal_hook("create", record={"id": "j1", "date": "2026-02-14",
148 "owner_id": "dan"})
149 assert reopened is None # deliberate, attributed, effective
150
151
152# --- the line-side gate (the amounts live in the lines) ------------------------
153
154def test_lines_on_a_closed_period_journal_are_frozen(tmp_path, monkeypatch):
155 data_dir = setup_env(tmp_path, monkeypatch)
156 make_journal(data_dir, "j-feb", date="2026-02-01", status="posted")
157 close_period(data_dir)
158 add = line_hook("create", record={"id": "l1", "journal_id": "j-feb",
159 "account_id": "acct-cash",
160 "debit_cents": "100", "credit_cents": "0"})
161 assert add["status"] == 409
162 assert "new journal in the current period" in add["error"]
163 edit = line_hook("update",
164 existing={"id": "l0", "journal_id": "j-feb"},
165 changes={"debit_cents": "999"})
166 assert edit["status"] == 409
167
168
169def test_lines_on_open_period_journals_flow_normally(tmp_path, monkeypatch):
170 data_dir = setup_env(tmp_path, monkeypatch)
171 close_period(data_dir)
172 make_journal(data_dir, "j-may", date="2026-05-01")
173 ok = line_hook("create", record={"id": "l1", "journal_id": "j-may",
174 "account_id": "acct-cash",
175 "debit_cents": "100", "credit_cents": "0"})
176 assert ok is None
177
178
179# --- the two gates coexist ------------------------------------------------------
180
181def test_balance_gate_still_holds_after_the_period_gate_joined_it(tmp_path, monkeypatch):
182 data_dir = setup_env(tmp_path, monkeypatch)
183 make_journal(data_dir, "j-open", date="2026-06-01")
184 object_records.create_collection_record(
185 "fin_journal_lines",
186 {"id": "l1", "journal_id": "j-open", "account_id": "acct-cash",
187 "debit_cents": "500", "credit_cents": "0", "owner_id": "dan"},
188 base_dir=data_dir)
189 verdict = journal_hook("update",
190 existing={"id": "j-open", "date": "2026-06-01",
191 "status": "draft", "owner_id": "dan"},
192 changes={"status": "posted"})
193 assert verdict["status"] == 409
194 assert "must balance" in verdict["error"]