askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_period_locking.py · 8.5 KB · 192 lines
| 1 | """Closed periods (the q9-parity control this system lacked): settled |
| 2 | books stay settled. |
| 3 | |
| 4 | The scenario every test here guards: the year is filed, and months later |
| 5 | someone -- helpful, hurried, or dishonest -- edits last March. Without |
| 6 | this gate the filed statements and the live books drift apart with nobody |
| 7 | deciding they should; with it, the only paths are a correcting journal in |
| 8 | the CURRENT period, or a deliberate, attributed reopening. |
| 9 | """ |
| 10 | |
| 11 | import pathlib |
| 12 | |
| 13 | from conftest import stage_collection |
| 14 | |
| 15 | import object_execution |
| 16 | import object_records |
| 17 | import python_object_runtime |
| 18 | |
| 19 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 20 | PACKAGES = REPO_ROOT / "packages" |
| 21 | FINANCE_OBJECTS = PACKAGES / "app-finance" / "objects" |
| 22 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 23 | |
| 24 | |
| 25 | def 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 | |
| 37 | def 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 | |
| 46 | def 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 | |
| 54 | def 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 | |
| 62 | def 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 | |
| 68 | def 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 | |
| 76 | def 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 | |
| 89 | def 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 | |
| 100 | def test_a_journal_dated_inside_cannot_be_edited_at_all(tmp_path, monkeypatch): |
| 101 | data_dir = setup_env(tmp_path, monkeypatch) |
| 102 | close_period(data_dir) |
| 103 | make_journal(data_dir, "j-old", date="2026-02-01") |
| 104 | verdict = journal_hook("update", |
| 105 | existing={"id": "j-old", "date": "2026-02-01", |
| 106 | "owner_id": "dan"}, |
| 107 | changes={"description": "just fixing a typo"}) |
| 108 | assert verdict["status"] == 409 # settled means settled |
| 109 | |
| 110 | |
| 111 | def test_a_date_cannot_be_moved_into_a_closed_period(tmp_path, monkeypatch): |
| 112 | data_dir = setup_env(tmp_path, monkeypatch) |
| 113 | close_period(data_dir) |
| 114 | verdict = journal_hook("update", |
| 115 | existing={"id": "j-new", "date": "2026-05-01", |
| 116 | "owner_id": "dan"}, |
| 117 | changes={"date": "2026-02-01"}) |
| 118 | assert verdict["status"] == 409 # backdating is the classic move |
| 119 | |
| 120 | |
| 121 | def test_other_owners_and_other_entities_are_untouched(tmp_path, monkeypatch): |
| 122 | data_dir = setup_env(tmp_path, monkeypatch) |
| 123 | close_period(data_dir, owner="dan") |
| 124 | other = journal_hook("create", record={"id": "j1", "date": "2026-02-14", |
| 125 | "owner_id": "pat"}) |
| 126 | assert other is None # dan's filing binds only dan |
| 127 | |
| 128 | stage_collection(data_dir, "app-entities", "entities") |
| 129 | object_records.create_collection_record( |
| 130 | "entities", {"id": "ent-2", "name": "Second Books", "owner_id": "dan"}, |
| 131 | base_dir=data_dir) |
| 132 | scoped = journal_hook("create", record={"id": "j2", "date": "2026-02-14", |
| 133 | "owner_id": "dan", "entity_id": "ent-2"}) |
| 134 | assert scoped is None # entity-blank period != ent-2 books |
| 135 | |
| 136 | |
| 137 | def test_reopening_is_deleting_the_row_and_then_writes_flow_again(tmp_path, monkeypatch): |
| 138 | data_dir = setup_env(tmp_path, monkeypatch) |
| 139 | close_period(data_dir, pid="cp-q1") |
| 140 | blocked = journal_hook("create", record={"id": "j1", "date": "2026-02-14", |
| 141 | "owner_id": "dan"}) |
| 142 | assert blocked["status"] == 409 |
| 143 | object_records.delete_collection_record("fin_closed_periods", "cp-q1", |
| 144 | base_dir=data_dir, actor="dan") |
| 145 | reopened = journal_hook("create", record={"id": "j1", "date": "2026-02-14", |
| 146 | "owner_id": "dan"}) |
| 147 | assert reopened is None # deliberate, attributed, effective |
| 148 | |
| 149 | |
| 150 | # --- the line-side gate (the amounts live in the lines) ------------------------ |
| 151 | |
| 152 | def test_lines_on_a_closed_period_journal_are_frozen(tmp_path, monkeypatch): |
| 153 | data_dir = setup_env(tmp_path, monkeypatch) |
| 154 | make_journal(data_dir, "j-feb", date="2026-02-01", status="posted") |
| 155 | close_period(data_dir) |
| 156 | add = line_hook("create", record={"id": "l1", "journal_id": "j-feb", |
| 157 | "account_id": "acct-cash", |
| 158 | "debit_cents": "100", "credit_cents": "0"}) |
| 159 | assert add["status"] == 409 |
| 160 | assert "new journal in the current period" in add["error"] |
| 161 | edit = line_hook("update", |
| 162 | existing={"id": "l0", "journal_id": "j-feb"}, |
| 163 | changes={"debit_cents": "999"}) |
| 164 | assert edit["status"] == 409 |
| 165 | |
| 166 | |
| 167 | def test_lines_on_open_period_journals_flow_normally(tmp_path, monkeypatch): |
| 168 | data_dir = setup_env(tmp_path, monkeypatch) |
| 169 | close_period(data_dir) |
| 170 | make_journal(data_dir, "j-may", date="2026-05-01") |
| 171 | ok = line_hook("create", record={"id": "l1", "journal_id": "j-may", |
| 172 | "account_id": "acct-cash", |
| 173 | "debit_cents": "100", "credit_cents": "0"}) |
| 174 | assert ok is None |
| 175 | |
| 176 | |
| 177 | # --- the two gates coexist ------------------------------------------------------ |
| 178 | |
| 179 | def test_balance_gate_still_holds_after_the_period_gate_joined_it(tmp_path, monkeypatch): |
| 180 | data_dir = setup_env(tmp_path, monkeypatch) |
| 181 | make_journal(data_dir, "j-open", date="2026-06-01") |
| 182 | object_records.create_collection_record( |
| 183 | "fin_journal_lines", |
| 184 | {"id": "l1", "journal_id": "j-open", "account_id": "acct-cash", |
| 185 | "debit_cents": "500", "credit_cents": "0", "owner_id": "dan"}, |
| 186 | base_dir=data_dir) |
| 187 | verdict = journal_hook("update", |
| 188 | existing={"id": "j-open", "date": "2026-06-01", |
| 189 | "status": "draft", "owner_id": "dan"}, |
| 190 | changes={"status": "posted"}) |
| 191 | assert verdict["status"] == 409 |
| 192 | assert "must balance" in verdict["error"] |