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_compose_posted_journal.py · 5.7 KB · 146 lines
| 1 | """object_finance.compose_posted_journal: the shared composer every |
| 2 | generated journal goes through (extracted at the doctrine-#4 threshold -- |
| 3 | plan/inventory-adjustments-spec.md section 5). Callers own policy; these |
| 4 | tests pin the mechanics: idempotency by provenance, draft -> lines -> |
| 5 | re-read -> verify -> post, and the fail-soft shapes.""" |
| 6 | |
| 7 | import pathlib |
| 8 | |
| 9 | import object_finance |
| 10 | import object_records |
| 11 | from conftest import stage_collection |
| 12 | |
| 13 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 14 | PACKAGES = REPO_ROOT / "packages" |
| 15 | |
| 16 | |
| 17 | def setup_env(tmp_path, monkeypatch): |
| 18 | data_dir = tmp_path / "data" |
| 19 | for pkg, name in (("app-finance", "fin_journals"), |
| 20 | ("app-finance", "fin_journal_lines"), |
| 21 | ("app-finance", "fin_accounts"), |
| 22 | ("app-entities", "entities")): |
| 23 | stage_collection(data_dir, pkg, name) |
| 24 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 25 | for acct in ("a-dr", "a-cr", "a", "b"): |
| 26 | object_records.create_collection_record( |
| 27 | "fin_accounts", |
| 28 | {"id": acct, "name": acct, "account_type": "asset", "owner_id": "dan"}, |
| 29 | base_dir=data_dir) |
| 30 | for ent in ("ent-1", "ent-2"): |
| 31 | object_records.create_collection_record( |
| 32 | "entities", {"id": ent, "name": ent, "owner_id": "dan"}, |
| 33 | base_dir=data_dir) |
| 34 | return data_dir |
| 35 | |
| 36 | |
| 37 | def compose(data_dir, **overrides): |
| 38 | kwargs = dict( |
| 39 | generated_from="test/1", |
| 40 | date="2026-07-24", |
| 41 | description="Test entry", |
| 42 | lines=[ |
| 43 | {"account_id": "a-dr", "debit_cents": 500, "credit_cents": 0}, |
| 44 | {"account_id": "a-cr", "debit_cents": 0, "credit_cents": 500}, |
| 45 | ], |
| 46 | owner_id="dan", |
| 47 | ) |
| 48 | kwargs.update(overrides) |
| 49 | return object_finance.compose_posted_journal(data_dir, **kwargs) |
| 50 | |
| 51 | |
| 52 | def journals(data_dir): |
| 53 | return object_records.read_collection_records("fin_journals", base_dir=data_dir) |
| 54 | |
| 55 | |
| 56 | def lines_for(data_dir, journal_id): |
| 57 | return [l for l in object_records.read_collection_records( |
| 58 | "fin_journal_lines", base_dir=data_dir) |
| 59 | if l["journal_id"] == journal_id] |
| 60 | |
| 61 | |
| 62 | def test_balanced_entry_is_created_and_posted_with_provenance(tmp_path, monkeypatch): |
| 63 | data_dir = setup_env(tmp_path, monkeypatch) |
| 64 | result = compose(data_dir) |
| 65 | assert result["ok"] and result["posted"] |
| 66 | journal = journals(data_dir)[0] |
| 67 | assert journal["status"] == "posted" |
| 68 | assert journal["generated_from"] == "test/1" |
| 69 | assert journal["kind"] == "standard" |
| 70 | landed = lines_for(data_dir, result["journal_id"]) |
| 71 | assert sorted((l["debit_cents"], l["credit_cents"]) for l in landed) == \ |
| 72 | [("0", "500"), ("500", "0")] |
| 73 | |
| 74 | |
| 75 | def test_replay_is_idempotent_by_provenance(tmp_path, monkeypatch): |
| 76 | data_dir = setup_env(tmp_path, monkeypatch) |
| 77 | first = compose(data_dir) |
| 78 | again = compose(data_dir) |
| 79 | assert "already composed" in again["skipped"] |
| 80 | assert again["journal_id"] == first["journal_id"] |
| 81 | assert again["posted"] is True |
| 82 | assert len(journals(data_dir)) == 1 |
| 83 | |
| 84 | |
| 85 | def test_unbalanced_entry_stays_draft_with_note(tmp_path, monkeypatch): |
| 86 | data_dir = setup_env(tmp_path, monkeypatch) |
| 87 | result = compose(data_dir, lines=[ |
| 88 | {"account_id": "a-dr", "debit_cents": 500, "credit_cents": 0}, |
| 89 | {"account_id": "a-cr", "debit_cents": 0, "credit_cents": 300}, |
| 90 | ]) |
| 91 | assert result["posted"] is False |
| 92 | assert "did not balance" in result["note"] |
| 93 | assert journals(data_dir)[0]["status"] == "draft" |
| 94 | |
| 95 | |
| 96 | def test_post_false_composes_but_leaves_draft(tmp_path, monkeypatch): |
| 97 | data_dir = setup_env(tmp_path, monkeypatch) |
| 98 | result = compose(data_dir, post=False) |
| 99 | assert result["posted"] is False |
| 100 | assert "post not requested" in result["note"] |
| 101 | assert journals(data_dir)[0]["status"] == "draft" |
| 102 | |
| 103 | |
| 104 | def test_zero_or_empty_lines_compose_nothing(tmp_path, monkeypatch): |
| 105 | data_dir = setup_env(tmp_path, monkeypatch) |
| 106 | assert compose(data_dir, lines=[])["skipped"] == "zero amount" |
| 107 | assert compose(data_dir, lines=[ |
| 108 | {"account_id": "a", "debit_cents": 0, "credit_cents": 0}, |
| 109 | ])["skipped"] == "zero amount" |
| 110 | assert journals(data_dir) == [] |
| 111 | |
| 112 | |
| 113 | def test_unusable_amounts_error_without_writing(tmp_path, monkeypatch): |
| 114 | data_dir = setup_env(tmp_path, monkeypatch) |
| 115 | bad = compose(data_dir, lines=[{"account_id": "a", "debit_cents": "abc"}]) |
| 116 | assert bad["ok"] is False |
| 117 | negative = compose(data_dir, lines=[ |
| 118 | {"account_id": "a", "debit_cents": -500, "credit_cents": 0}, |
| 119 | {"account_id": "b", "debit_cents": 0, "credit_cents": -500}, |
| 120 | ]) |
| 121 | assert negative["ok"] is False |
| 122 | assert journals(data_dir) == [] |
| 123 | |
| 124 | |
| 125 | def test_memo_kind_and_per_line_entity_land(tmp_path, monkeypatch): |
| 126 | data_dir = setup_env(tmp_path, monkeypatch) |
| 127 | result = compose(data_dir, kind="adjusting", entity_id="ent-1", lines=[ |
| 128 | {"account_id": "a-dr", "debit_cents": 200, "credit_cents": 0, |
| 129 | "memo": "note", "entity_id": "ent-2"}, |
| 130 | {"account_id": "a-cr", "debit_cents": 0, "credit_cents": 200}, |
| 131 | ]) |
| 132 | journal = journals(data_dir)[0] |
| 133 | assert journal["kind"] == "adjusting" |
| 134 | assert journal["entity_id"] == "ent-1" |
| 135 | by_account = {l["account_id"]: l for l in lines_for(data_dir, result["journal_id"])} |
| 136 | assert by_account["a-dr"]["memo"] == "note" |
| 137 | assert by_account["a-dr"]["entity_id"] == "ent-2" # per-line override |
| 138 | assert by_account["a-cr"]["entity_id"] == "ent-1" # journal default |
| 139 | |
| 140 | |
| 141 | def test_find_journal_by_provenance(tmp_path, monkeypatch): |
| 142 | data_dir = setup_env(tmp_path, monkeypatch) |
| 143 | assert object_finance.find_journal_by_provenance(data_dir, "test/1") is None |
| 144 | created = compose(data_dir) |
| 145 | found = object_finance.find_journal_by_provenance(data_dir, "test/1") |
| 146 | assert found["id"] == created["journal_id"] |