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_app_invoices_totals.py · 12.7 KB · 318 lines
| 1 | """Behavior tests for packages/app-invoices: the invoice_totals HANDLES |
| 2 | handler (integer-cents arithmetic, floor-not-round tax, idempotence, |
| 3 | delete handling) and the guarded status transitions on invoices.status. |
| 4 | |
| 5 | Structural/manifest/permission tests live in tests/test_app_invoices_package.py. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | import object_execution |
| 14 | import object_packages |
| 15 | import object_permissions |
| 16 | import object_record_changes |
| 17 | import object_records |
| 18 | import python_object_runtime |
| 19 | |
| 20 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 21 | |
| 22 | |
| 23 | def _install(tmp_path): |
| 24 | """Install app-invoices (+ app-contacts, for the customer_id relation |
| 25 | target) into an isolated data dir/object root and return |
| 26 | (data_dir, object_root, runtime). |
| 27 | """ |
| 28 | data_dir = tmp_path / "data" |
| 29 | object_root = tmp_path / "objects" |
| 30 | object_root.mkdir() |
| 31 | # conftest's isolated_default_data_dir fixture already points |
| 32 | # DBBASIC_DATA_DIR at tmp_path / "data" for this test process; keep |
| 33 | # them in sync so invoice_totals.py's own env-based _data_dir() |
| 34 | # resolves to the same directory this test writes into. |
| 35 | os.environ["DBBASIC_DATA_DIR"] = str(data_dir) |
| 36 | |
| 37 | object_packages.install_package( |
| 38 | "app-invoices", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root] |
| 39 | ) |
| 40 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir) |
| 41 | return data_dir, object_root, runtime |
| 42 | |
| 43 | |
| 44 | def _fire(runtime, object_root, record_id, action): |
| 45 | return object_execution.execute_object( |
| 46 | runtime, |
| 47 | object_execution.ObjectExecutionRequest( |
| 48 | "system_invoice_totals", |
| 49 | method="EVENT", |
| 50 | payload={ |
| 51 | "event": f"invoice_lines.record.{action}", |
| 52 | "collection": "invoice_lines", |
| 53 | "record_id": record_id, |
| 54 | "action": action, |
| 55 | }, |
| 56 | ), |
| 57 | roots=[object_root], |
| 58 | ) |
| 59 | |
| 60 | |
| 61 | def _add_payments_substrate(data_dir, paid_cents=None): |
| 62 | """invoices v2 derives amount_paid/balance_due from the payments/refunds |
| 63 | collections (app-payments). Give the env those schemas + collections, and |
| 64 | optionally one real received payment -- the thing the old test faked by |
| 65 | writing amount_paid_cents directly ("simulating a future partial-payment |
| 66 | write"; the future arrived).""" |
| 67 | import pathlib, shutil |
| 68 | packages = pathlib.Path(__file__).resolve().parents[1] / "packages" |
| 69 | schema_dir = data_dir / "schemas" |
| 70 | for name in ("payments", "refunds"): |
| 71 | shutil.copy(packages / "app-payments" / "schemas" / f"{name}.json", |
| 72 | schema_dir / f"{name}.json") |
| 73 | for coll, hdr in ( |
| 74 | ("payments", "id\tinvoice_id\tamount_cents\tmethod\treceived_on\treference\tnotes\tstatus\trefunded_cents\towner_id\tcreated_at\n"), |
| 75 | ("refunds", "id\tpayment_id\tinvoice_id\tamount_cents\treason\trefunded_on\towner_id\tcreated_at\n"), |
| 76 | ): |
| 77 | d = data_dir / "collections" / coll |
| 78 | d.mkdir(parents=True, exist_ok=True) |
| 79 | (d / "records.tsv").write_text(hdr) |
| 80 | if paid_cents is not None: |
| 81 | object_records.create_collection_record( |
| 82 | "payments", |
| 83 | {"id": "pay_1", "invoice_id": "inv_1", "amount_cents": str(paid_cents), |
| 84 | "method": "transfer", "received_on": "2026-07-24", "owner_id": "u1"}, |
| 85 | base_dir=data_dir) |
| 86 | |
| 87 | |
| 88 | def _make_invoice(data_dir, **overrides): |
| 89 | record = { |
| 90 | "id": "inv_1", |
| 91 | "number": "INV-0001", |
| 92 | "customer_name": "Example Consulting LLC", |
| 93 | "owner_id": "u1", |
| 94 | } |
| 95 | record.update(overrides) |
| 96 | return object_records.create_collection_record("invoices", record, base_dir=data_dir, actor="test") |
| 97 | |
| 98 | |
| 99 | def _make_line(data_dir, **overrides): |
| 100 | record = { |
| 101 | "invoice_id": "inv_1", |
| 102 | "description": "Line item", |
| 103 | "quantity": "1", |
| 104 | "unit_price_cents": "100", |
| 105 | "owner_id": "u1", |
| 106 | } |
| 107 | record.update(overrides) |
| 108 | return object_records.create_collection_record("invoice_lines", record, base_dir=data_dir, actor="test") |
| 109 | |
| 110 | |
| 111 | def test_line_total_is_quantity_times_unit_price_floored(tmp_path): |
| 112 | data_dir, object_root, runtime = _install(tmp_path) |
| 113 | _make_invoice(data_dir) |
| 114 | _make_line(data_dir, id="il_1", quantity="3", unit_price_cents="1999") |
| 115 | |
| 116 | result = _fire(runtime, object_root, "il_1", "created") |
| 117 | assert result.ok is True |
| 118 | |
| 119 | line = object_records.get_collection_record("invoice_lines", "il_1", base_dir=data_dir) |
| 120 | assert line["line_total_cents"] == "5997" # 3 * 1999 |
| 121 | |
| 122 | |
| 123 | def test_line_tax_uses_basis_points_floored_not_rounded(tmp_path): |
| 124 | """8.25% (825 bps) of 5997 cents is 494.7825 cents -- must floor to |
| 125 | 494, never round to 495, per 20-invoice-spec.md's 'exact arithmetic, |
| 126 | floor not round' instruction. |
| 127 | """ |
| 128 | data_dir, object_root, runtime = _install(tmp_path) |
| 129 | _make_invoice(data_dir) |
| 130 | _make_line(data_dir, id="il_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 131 | |
| 132 | _fire(runtime, object_root, "il_1", "created") |
| 133 | |
| 134 | line = object_records.get_collection_record("invoice_lines", "il_1", base_dir=data_dir) |
| 135 | assert line["line_total_cents"] == "5997" |
| 136 | assert line["line_tax_cents"] == "494" |
| 137 | |
| 138 | |
| 139 | def test_multi_line_invoice_totals_match_hand_computed_cents(tmp_path): |
| 140 | """The worked example this package's report cites: |
| 141 | |
| 142 | Line 1: qty 3, unit_price_cents 1999, tax_rate_bps 825 |
| 143 | line_total = 3 * 1999 = 5997 |
| 144 | line_tax = 5997 * 825 // 10000 = 494 (floor of 494.7825) |
| 145 | Line 2: qty 1, unit_price_cents 5000, tax_rate_bps 0 |
| 146 | line_total = 5000 |
| 147 | line_tax = 0 |
| 148 | subtotal_cents = 5997 + 5000 = 10997 |
| 149 | tax_cents = 494 + 0 = 494 |
| 150 | total_cents = 10997 + 494 = 11491 |
| 151 | amount_paid_cents = 2000 (a real received payment record now -- |
| 152 | rollup/formula territory, app-payments) |
| 153 | balance_due_cents = 11491 - 2000 = 9491 |
| 154 | """ |
| 155 | data_dir, object_root, runtime = _install(tmp_path) |
| 156 | _make_invoice(data_dir) |
| 157 | _add_payments_substrate(data_dir, paid_cents=2000) |
| 158 | _make_line(data_dir, id="il_1", description="Consulting hours", |
| 159 | quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 160 | _make_line(data_dir, id="il_2", description="Software license", |
| 161 | quantity="1", unit_price_cents="5000", tax_rate_bps="0") |
| 162 | |
| 163 | _fire(runtime, object_root, "il_1", "created") |
| 164 | _fire(runtime, object_root, "il_2", "created") |
| 165 | |
| 166 | invoice = object_records.get_collection_record("invoices", "inv_1", base_dir=data_dir) |
| 167 | assert invoice["subtotal_cents"] == "10997" |
| 168 | assert invoice["tax_cents"] == "494" |
| 169 | assert invoice["total_cents"] == "11491" |
| 170 | assert invoice["balance_due_cents"] == "9491" |
| 171 | |
| 172 | |
| 173 | def test_fractional_quantity_uses_decimal_not_float(tmp_path): |
| 174 | """2.5 hours at 10000 cents/hr must floor to exactly 25000 cents -- |
| 175 | Decimal arithmetic, never a bare float multiplication that could |
| 176 | introduce binary rounding error. |
| 177 | """ |
| 178 | data_dir, object_root, runtime = _install(tmp_path) |
| 179 | _make_invoice(data_dir) |
| 180 | _make_line(data_dir, id="il_1", quantity="2.5", unit_price_cents="10000") |
| 181 | |
| 182 | _fire(runtime, object_root, "il_1", "created") |
| 183 | |
| 184 | line = object_records.get_collection_record("invoice_lines", "il_1", base_dir=data_dir) |
| 185 | assert line["line_total_cents"] == "25000" |
| 186 | |
| 187 | |
| 188 | def test_recompute_is_idempotent_and_skips_a_no_op_write(tmp_path): |
| 189 | data_dir, object_root, runtime = _install(tmp_path) |
| 190 | _make_invoice(data_dir) |
| 191 | _make_line(data_dir, id="il_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 192 | |
| 193 | first = _fire(runtime, object_root, "il_1", "created") |
| 194 | assert first.result["changed"] is True |
| 195 | |
| 196 | changes_before = object_record_changes.list_record_changes( |
| 197 | "invoices", record_id="inv_1", base_dir=data_dir |
| 198 | )["total"] |
| 199 | |
| 200 | second = _fire(runtime, object_root, "il_1", "updated") |
| 201 | assert second.result["changed"] is False |
| 202 | |
| 203 | changes_after = object_record_changes.list_record_changes( |
| 204 | "invoices", record_id="inv_1", base_dir=data_dir |
| 205 | )["total"] |
| 206 | assert changes_after == changes_before # no redundant write/record_change |
| 207 | |
| 208 | |
| 209 | def test_deleting_a_line_recovers_invoice_id_from_record_changes_and_resums(tmp_path): |
| 210 | """The deleted line is already gone by the time this handler's |
| 211 | post-commit dispatch fires -- it must recover invoice_id from the |
| 212 | record_changes log's own 'before' snapshot, not the (now 404) live |
| 213 | record. |
| 214 | """ |
| 215 | data_dir, object_root, runtime = _install(tmp_path) |
| 216 | _make_invoice(data_dir) |
| 217 | _make_line(data_dir, id="il_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 218 | _make_line(data_dir, id="il_2", quantity="1", unit_price_cents="5000") |
| 219 | _fire(runtime, object_root, "il_1", "created") |
| 220 | _fire(runtime, object_root, "il_2", "created") |
| 221 | |
| 222 | invoice = object_records.get_collection_record("invoices", "inv_1", base_dir=data_dir) |
| 223 | assert invoice["total_cents"] == "11491" |
| 224 | |
| 225 | object_records.delete_collection_record("invoice_lines", "il_2", base_dir=data_dir, actor="test") |
| 226 | result = _fire(runtime, object_root, "il_2", "deleted") |
| 227 | assert result.ok is True |
| 228 | assert result.result["invoice_id"] == "inv_1" |
| 229 | |
| 230 | invoice_after = object_records.get_collection_record("invoices", "inv_1", base_dir=data_dir) |
| 231 | assert invoice_after["subtotal_cents"] == "5997" |
| 232 | assert invoice_after["tax_cents"] == "494" |
| 233 | assert invoice_after["total_cents"] == "6491" |
| 234 | |
| 235 | |
| 236 | def test_zero_lines_leaves_invoice_at_zero_totals(tmp_path): |
| 237 | data_dir, object_root, runtime = _install(tmp_path) |
| 238 | _add_payments_substrate(data_dir) # balance_due derives via payments now |
| 239 | _make_invoice(data_dir) |
| 240 | |
| 241 | invoice = object_records.get_collection_record("invoices", "inv_1", base_dir=data_dir) |
| 242 | assert invoice["subtotal_cents"] == "0" |
| 243 | assert invoice["total_cents"] == "0" |
| 244 | assert invoice["balance_due_cents"] == "0" |
| 245 | |
| 246 | |
| 247 | def test_event_for_an_unrelated_collection_is_a_no_op(tmp_path): |
| 248 | data_dir, object_root, runtime = _install(tmp_path) |
| 249 | result = object_execution.execute_object( |
| 250 | runtime, |
| 251 | object_execution.ObjectExecutionRequest( |
| 252 | "system_invoice_totals", |
| 253 | method="EVENT", |
| 254 | payload={"event": "notes.record.created", "collection": "notes", |
| 255 | "record_id": "n1", "action": "created"}, |
| 256 | ), |
| 257 | roots=[object_root], |
| 258 | ) |
| 259 | assert result.ok is True |
| 260 | assert result.result["skipped"] == "not an invoice_lines event" |
| 261 | |
| 262 | |
| 263 | # -- Guarded status transitions ------------------------------------------- |
| 264 | # |
| 265 | # Exercised directly against object_records' own guard (the same function |
| 266 | # update_collection_record calls internally) rather than round-tripping |
| 267 | # through the full HTTP server for one field's worth of behavior -- |
| 268 | # requires the schema actually installed in a data dir first, since |
| 269 | # get_schema resolves from base_dir, not from package source. |
| 270 | |
| 271 | def test_owner_may_move_draft_to_sent(tmp_path): |
| 272 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 273 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 274 | existing = {"status": "draft", "owner_id": "u1"} |
| 275 | updated = {"status": "sent", "owner_id": "u1"} |
| 276 | |
| 277 | object_records._validate_field_transitions( |
| 278 | "invoices", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 279 | ) # no exception == allowed |
| 280 | |
| 281 | |
| 282 | def test_non_owner_may_not_move_draft_to_sent(tmp_path): |
| 283 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 284 | subject = object_permissions.PermissionSubject(user_id="someone_else") |
| 285 | existing = {"status": "draft", "owner_id": "u1"} |
| 286 | updated = {"status": "sent", "owner_id": "u1"} |
| 287 | |
| 288 | with pytest.raises(object_records.TransitionNotAllowedError): |
| 289 | object_records._validate_field_transitions( |
| 290 | "invoices", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 291 | ) |
| 292 | |
| 293 | |
| 294 | def test_paid_and_void_are_terminal(tmp_path): |
| 295 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 296 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 297 | |
| 298 | for terminal_status in ("paid", "void"): |
| 299 | existing = {"status": terminal_status, "owner_id": "u1"} |
| 300 | updated = {"status": "draft", "owner_id": "u1"} |
| 301 | with pytest.raises(object_records.InvalidRecordPayloadError): |
| 302 | object_records._validate_field_transitions( |
| 303 | "invoices", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 304 | ) |
| 305 | |
| 306 | |
| 307 | def test_sent_to_partial_then_partial_to_paid_is_a_legal_path(tmp_path): |
| 308 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 309 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 310 | |
| 311 | object_records._validate_field_transitions( |
| 312 | "invoices", {"status": "sent", "owner_id": "u1"}, {"status": "partial", "owner_id": "u1"}, |
| 313 | base_dir=data_dir, roots=None, subject=subject, |
| 314 | ) |
| 315 | object_records._validate_field_transitions( |
| 316 | "invoices", {"status": "partial", "owner_id": "u1"}, {"status": "paid", "owner_id": "u1"}, |
| 317 | base_dir=data_dir, roots=None, subject=subject, |
| 318 | ) |