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.
test_fail_open_reads.py · 10.7 KB · 221 lines
| 1 | """Reads in money and stock paths that turned "could not read" into "empty". |
| 2 | |
| 3 | Each of these handlers or actions caught any error around a collection read |
| 4 | and carried on as if the collection held nothing, or was not installed. |
| 5 | Not installed is a fact and still reads as empty; installed but unreadable |
| 6 | is an unknown, and now refuses (an action: 503, nothing written) or says |
| 7 | what it did not do (a reaction, which cannot refuse a write that has |
| 8 | already happened). |
| 9 | """ |
| 10 | |
| 11 | import pathlib |
| 12 | import sys |
| 13 | |
| 14 | from conftest import stage_collection |
| 15 | |
| 16 | import object_records |
| 17 | import test_bank_reconciliation as tbr |
| 18 | import test_disputes as td |
| 19 | import test_findings_money_gates as tmg |
| 20 | import test_tm_billing as ttm |
| 21 | from test_findings_money_gates import failing_reads |
| 22 | |
| 23 | PACKAGES = pathlib.Path(__file__).resolve().parents[1] / "packages" |
| 24 | |
| 25 | |
| 26 | # --- bank matcher: one payment, one bank line ---------------------------------- |
| 27 | |
| 28 | def test_a_payment_another_line_holds_is_not_auto_matched_again(tmp_path, monkeypatch): |
| 29 | """_already_matched read bank_lines a second time and folded a failure |
| 30 | to "nothing is claimed", so a payment already matched to one bank line |
| 31 | could be auto-matched to a second. It now uses the lines it read.""" |
| 32 | data_dir = tbr.setup_env(tmp_path, monkeypatch) |
| 33 | tbr.make_invoice(data_dir) |
| 34 | tbr.make_payment(data_dir, "p1", cents=150000, reference="INV-1001") |
| 35 | tbr.make_line(data_dir, "L0", amount=150000, desc="PAYMENT INV-1001", |
| 36 | match_status="matched", matched_to="payments/p1") |
| 37 | tbr.make_line(data_dir, "L1", amount=150000, desc="PAYMENT INV-1001") |
| 38 | |
| 39 | real = object_records.read_collection_records |
| 40 | calls = [] |
| 41 | |
| 42 | def second_read_fails(collection, *args, **kwargs): |
| 43 | if collection == "bank_lines" and sys._getframe(1).f_code.co_filename.endswith( |
| 44 | "bank_matcher.py"): |
| 45 | calls.append(collection) |
| 46 | if len(calls) > 1: |
| 47 | raise OSError("simulated: bank_lines unreadable") |
| 48 | return real(collection, *args, **kwargs) |
| 49 | |
| 50 | monkeypatch.setattr(object_records, "read_collection_records", second_read_fails) |
| 51 | result = tbr.run_object("system_bank_matcher", {}) |
| 52 | monkeypatch.setattr(object_records, "read_collection_records", real) |
| 53 | |
| 54 | assert result["auto_matched"] == 0, result |
| 55 | assert tbr.line(data_dir, "L1")["matched_to"] == "" |
| 56 | |
| 57 | |
| 58 | # --- gift cards: issued when the money is here --------------------------------- |
| 59 | |
| 60 | def gift_card_sale(tmp_path, monkeypatch): |
| 61 | data = tmg.wallet_env(tmp_path, monkeypatch) |
| 62 | |
| 63 | def make(collection, record): |
| 64 | return object_records.create_collection_record(collection, record, base_dir=data) |
| 65 | |
| 66 | make("products", {"id": "gc", "name": "Gift Card $50", "price_cents": "5000", |
| 67 | "is_gift_card": "true", "product_type": "digital", "owner_id": "shop"}) |
| 68 | make("invoices", {"id": "inv1", "number": "INV-1", "customer_name": "Ada", |
| 69 | "total_cents": "5000", "status": "sent", "owner_id": "shop"}) |
| 70 | make("orders", {"id": "o1", "number": "SO-1", "customer_name": "Ada", |
| 71 | "invoice_id": "inv1", "owner_id": "shop"}) |
| 72 | make("order_lines", {"id": "ol1", "order_id": "o1", "product_id": "gc", |
| 73 | "description": "Gift Card $50", "quantity": "1", |
| 74 | "unit_price_cents": "5000", "line_total_cents": "5000", |
| 75 | "owner_id": "shop"}) |
| 76 | payment = make("payments", {"id": "pay1", "invoice_id": "inv1", "amount_cents": "5000", |
| 77 | "method": "card", "received_on": "2026-07-24", |
| 78 | "status": "received", "owner_id": "shop"}) |
| 79 | event = {"event": "payments.record.created", "collection": "payments", |
| 80 | "record_id": "pay1", "action": "create", "record": payment} |
| 81 | return data, event |
| 82 | |
| 83 | |
| 84 | def gift_cards(data): |
| 85 | return [w for w in object_records.read_collection_records("wallets", base_dir=data) |
| 86 | if w.get("kind") == "gift_card"] |
| 87 | |
| 88 | |
| 89 | def test_unreadable_refunds_open_no_gift_card(tmp_path, monkeypatch): |
| 90 | """Refunds that could not be read counted as none, so a card was opened |
| 91 | for money that may already have gone back. Unknown is not settled; the |
| 92 | next payment event opens it once refunds read again.""" |
| 93 | data, event = gift_card_sale(tmp_path, monkeypatch) |
| 94 | with failing_reads("refunds", "gift_card_issue.py") as fired: |
| 95 | result = tmg.run_object("system_gift_card_issue", dict(event), "app-billing", |
| 96 | method="EVENT").result |
| 97 | assert fired |
| 98 | assert "refunds unreadable" in result["skipped"], result |
| 99 | assert gift_cards(data) == [] |
| 100 | |
| 101 | again = tmg.run_object("system_gift_card_issue", dict(event), "app-billing", |
| 102 | method="EVENT").result |
| 103 | assert again.get("issued") == 1, again |
| 104 | assert len(gift_cards(data)) == 1 |
| 105 | |
| 106 | |
| 107 | # --- the books: a movement booked once, on create ------------------------------ |
| 108 | |
| 109 | def books_env(tmp_path, monkeypatch, *, ledger): |
| 110 | data = tmp_path / "data" |
| 111 | for package, name in (("app-finance", "fin_journals"), |
| 112 | ("app-finance", "fin_journal_lines"), |
| 113 | ("app-finance", "fin_accounts"), |
| 114 | ("app-settings", "app_settings"), *ledger): |
| 115 | stage_collection(data, package, name) |
| 116 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data)) |
| 117 | return data |
| 118 | |
| 119 | |
| 120 | def test_unreadable_books_are_not_books_that_are_not_installed_for_stock( |
| 121 | tmp_path, monkeypatch): |
| 122 | """system_stock_books read an unreadable fin_journals as "books not |
| 123 | installed" and skipped; it fires once, on create, so the loss was never |
| 124 | booked and nothing said so.""" |
| 125 | data = books_env(tmp_path, monkeypatch, ledger=( |
| 126 | ("app-catalog", "products"), ("app-catalog", "locations"), |
| 127 | ("app-catalog", "stock_moves"))) |
| 128 | object_records.create_collection_record( |
| 129 | "products", {"id": "p1", "name": "Mug", "owner_id": "shop"}, base_dir=data) |
| 130 | object_records.create_collection_record( |
| 131 | "locations", {"id": "shelf", "name": "Shelf", "location_type": "warehouse", |
| 132 | "owner_id": "shop"}, base_dir=data) |
| 133 | object_records.create_collection_record( |
| 134 | "stock_moves", {"id": "m1", "product_id": "p1", "from_location_id": "shelf", |
| 135 | "quantity": "1", "unit_cost_cents": "500", "reason": "breakage", |
| 136 | "occurred_at": "2026-07-01", "owner_id": "shop"}, base_dir=data) |
| 137 | event = {"event": "stock_moves.record.created", "collection": "stock_moves", |
| 138 | "record_id": "m1", "action": "create"} |
| 139 | with failing_reads("fin_journals", "stock_books.py") as fired: |
| 140 | result = tmg.run_object("system_stock_books", dict(event), "app-catalog", |
| 141 | method="EVENT").result |
| 142 | assert fired |
| 143 | assert result["ok"] is False and result["books_unreadable"], result |
| 144 | assert "stock_moves/m1" in result["error"] |
| 145 | assert "not installed" not in result["skipped"] |
| 146 | |
| 147 | |
| 148 | def test_unreadable_books_are_not_books_that_are_not_installed_for_wallets( |
| 149 | tmp_path, monkeypatch): |
| 150 | data = books_env(tmp_path, monkeypatch, ledger=( |
| 151 | ("app-billing", "wallets"), ("app-billing", "wallet_entries"))) |
| 152 | object_records.create_collection_record( |
| 153 | "wallets", {"id": "w1", "owner_id": "ada", "is_active": "true"}, base_dir=data) |
| 154 | object_records.create_collection_record( |
| 155 | "wallet_entries", {"id": "e1", "wallet_id": "w1", "amount_minor": "500", |
| 156 | "kind": "topup", "owner_id": "ada"}, base_dir=data) |
| 157 | event = {"event": "wallet_entries.record.created", "collection": "wallet_entries", |
| 158 | "record_id": "e1", "action": "create"} |
| 159 | with failing_reads("fin_journal_lines", "wallet_books.py") as fired: |
| 160 | result = tmg.run_object("system_wallet_books", dict(event), "app-billing", |
| 161 | method="EVENT").result |
| 162 | assert fired |
| 163 | assert result["ok"] is False and result["books_unreadable"], result |
| 164 | assert "wallet_entries/e1" in result["error"] |
| 165 | |
| 166 | |
| 167 | def test_books_that_are_not_installed_still_skip_quietly(tmp_path, monkeypatch): |
| 168 | data = tmp_path / "data" |
| 169 | stage_collection(data, "app-billing", "wallet_entries") |
| 170 | stage_collection(data, "app-settings", "app_settings") |
| 171 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data)) |
| 172 | event = {"event": "wallet_entries.record.created", "collection": "wallet_entries", |
| 173 | "record_id": "e1", "action": "create"} |
| 174 | result = tmg.run_object("system_wallet_books", dict(event), "app-billing", |
| 175 | method="EVENT").result |
| 176 | assert result == {"ok": True, "skipped": "books not installed (fin_journals absent)"} |
| 177 | |
| 178 | |
| 179 | # --- disputes: compensated once ------------------------------------------------ |
| 180 | |
| 181 | def test_an_unreadable_order_history_sends_no_second_replacement(tmp_path, monkeypatch): |
| 182 | """_already_replaced read unreadable orders as "not replaced yet", and |
| 183 | _copy_lines read unreadable order lines as none, so a retry could send |
| 184 | a second replacement, or a first one with no goods on it. Now 503, |
| 185 | nothing written, the claim released, and the retry replaces once.""" |
| 186 | data_dir, objects_root = td.shop_with_a_dispute(tmp_path, monkeypatch) |
| 187 | for unreadable in ("orders", "order_lines"): |
| 188 | with failing_reads(unreadable, "resolve_dispute.py") as fired: |
| 189 | refused = td.resolve(objects_root, dispute_id="dis-1", |
| 190 | resolution_kind="replacement", today="2026-07-20") |
| 191 | assert fired, unreadable |
| 192 | assert refused.get("status") == 503, (unreadable, refused) |
| 193 | assert len(td.orders(data_dir)) == 1, unreadable # the original only |
| 194 | |
| 195 | done = td.resolve(objects_root, dispute_id="dis-1", |
| 196 | resolution_kind="replacement", today="2026-07-20") |
| 197 | assert done["ok"] is True |
| 198 | lines = [row for row in object_records.read_collection_records( |
| 199 | "order_lines", base_dir=data_dir) if row["order_id"] == done["order_id"]] |
| 200 | assert len(lines) == 1 and len(td.orders(data_dir)) == 2 |
| 201 | |
| 202 | |
| 203 | # --- time and materials: an invoice with everything owed on it ----------------- |
| 204 | |
| 205 | def test_unreadable_expenses_raise_no_invoice(tmp_path, monkeypatch): |
| 206 | """Expenses that could not be read were "no expenses": the invoice went |
| 207 | out with the hours alone. Now 503 and no invoice.""" |
| 208 | data_dir = ttm.setup_env(tmp_path, monkeypatch) |
| 209 | stage_collection(data_dir, "app-finance", "expenses") |
| 210 | ttm.rate_card(data_dir, "house", 15000) |
| 211 | ttm.log_time(data_dir, "t1", status="submitted", seconds=ttm.HOUR) |
| 212 | ttm.approve(data_dir, "t1") |
| 213 | |
| 214 | for unreadable in ("expenses", "time_logs"): |
| 215 | with failing_reads(unreadable, "generate_tm_invoice.py") as fired: |
| 216 | refused = ttm.generate({"project_id": "proj-acme"}) |
| 217 | assert fired, unreadable |
| 218 | assert refused.get("status") == 503, (unreadable, refused) |
| 219 | assert ttm.invoices(data_dir) == [] |
| 220 | |
| 221 | assert ttm.generate({"project_id": "proj-acme"})["invoiced"] == 1 |