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_orders_totals.py · 11.6 KB · 301 lines
| 1 | """Behavior tests for packages/app-orders: the order_totals HANDLES |
| 2 | handler (integer-cents arithmetic, floor-not-round tax, idempotence, |
| 3 | delete handling) and the guarded status transitions on orders.status. |
| 4 | |
| 5 | Mirrors tests/test_app_invoices_totals.py. Structural/manifest/permission |
| 6 | tests live in tests/test_app_orders_package.py. |
| 7 | """ |
| 8 | |
| 9 | import os |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | import object_execution |
| 15 | import object_packages |
| 16 | import object_permissions |
| 17 | import object_record_changes |
| 18 | import object_records |
| 19 | import python_object_runtime |
| 20 | |
| 21 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 22 | |
| 23 | |
| 24 | def _install(tmp_path): |
| 25 | """Install app-orders 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 order_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-orders", 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_order_totals", |
| 49 | method="EVENT", |
| 50 | payload={ |
| 51 | "event": f"order_lines.record.{action}", |
| 52 | "collection": "order_lines", |
| 53 | "record_id": record_id, |
| 54 | "action": action, |
| 55 | }, |
| 56 | ), |
| 57 | roots=[object_root], |
| 58 | ) |
| 59 | |
| 60 | |
| 61 | def _make_order(data_dir, **overrides): |
| 62 | record = { |
| 63 | "id": "ord_1", |
| 64 | "number": "SO-0001", |
| 65 | "customer_name": "Example Consulting LLC", |
| 66 | "owner_id": "u1", |
| 67 | } |
| 68 | record.update(overrides) |
| 69 | return object_records.create_collection_record("orders", record, base_dir=data_dir, actor="test") |
| 70 | |
| 71 | |
| 72 | def _make_line(data_dir, **overrides): |
| 73 | record = { |
| 74 | "order_id": "ord_1", |
| 75 | "description": "Line item", |
| 76 | "quantity": "1", |
| 77 | "unit_price_cents": "100", |
| 78 | "owner_id": "u1", |
| 79 | } |
| 80 | record.update(overrides) |
| 81 | return object_records.create_collection_record("order_lines", record, base_dir=data_dir, actor="test") |
| 82 | |
| 83 | |
| 84 | def test_line_total_is_quantity_times_unit_price_floored(tmp_path): |
| 85 | data_dir, object_root, runtime = _install(tmp_path) |
| 86 | _make_order(data_dir) |
| 87 | _make_line(data_dir, id="ol_1", quantity="3", unit_price_cents="1999") |
| 88 | |
| 89 | result = _fire(runtime, object_root, "ol_1", "created") |
| 90 | assert result.ok is True |
| 91 | |
| 92 | line = object_records.get_collection_record("order_lines", "ol_1", base_dir=data_dir) |
| 93 | assert line["line_total_cents"] == "5997" # 3 * 1999 |
| 94 | |
| 95 | |
| 96 | def test_line_tax_uses_basis_points_floored_not_rounded(tmp_path): |
| 97 | """8.25% (825 bps) of 5997 cents is 494.7825 cents -- must floor to |
| 98 | 494, never round to 495 ("exact arithmetic, floor not round"). |
| 99 | """ |
| 100 | data_dir, object_root, runtime = _install(tmp_path) |
| 101 | _make_order(data_dir) |
| 102 | _make_line(data_dir, id="ol_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 103 | |
| 104 | _fire(runtime, object_root, "ol_1", "created") |
| 105 | |
| 106 | line = object_records.get_collection_record("order_lines", "ol_1", base_dir=data_dir) |
| 107 | assert line["line_total_cents"] == "5997" |
| 108 | assert line["line_tax_cents"] == "494" |
| 109 | |
| 110 | |
| 111 | def test_multi_line_order_totals_match_hand_computed_cents(tmp_path): |
| 112 | """The worked example this package's report cites: |
| 113 | |
| 114 | Line 1: qty 3, unit_price_cents 1999, tax_rate_bps 825 |
| 115 | line_total = 3 * 1999 = 5997 |
| 116 | line_tax = 5997 * 825 // 10000 = 494 (floor of 494.7825) |
| 117 | Line 2: qty 1, unit_price_cents 5000, tax_rate_bps 0 |
| 118 | line_total = 5000 |
| 119 | line_tax = 0 |
| 120 | subtotal_cents = 5997 + 5000 = 10997 |
| 121 | tax_cents = 494 + 0 = 494 |
| 122 | total_cents = 10997 + 494 = 11491 |
| 123 | """ |
| 124 | data_dir, object_root, runtime = _install(tmp_path) |
| 125 | _make_order(data_dir) |
| 126 | _make_line(data_dir, id="ol_1", description="Widgets", |
| 127 | quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 128 | _make_line(data_dir, id="ol_2", description="Shipping", |
| 129 | quantity="1", unit_price_cents="5000", tax_rate_bps="0") |
| 130 | |
| 131 | _fire(runtime, object_root, "ol_1", "created") |
| 132 | _fire(runtime, object_root, "ol_2", "created") |
| 133 | |
| 134 | order = object_records.get_collection_record("orders", "ord_1", base_dir=data_dir) |
| 135 | assert order["subtotal_cents"] == "10997" |
| 136 | assert order["tax_cents"] == "494" |
| 137 | assert order["total_cents"] == "11491" |
| 138 | |
| 139 | |
| 140 | def test_fractional_quantity_uses_decimal_not_float(tmp_path): |
| 141 | """2.5 hours at 10000 cents/hr must floor to exactly 25000 cents -- |
| 142 | Decimal arithmetic, never a bare float multiplication that could |
| 143 | introduce binary rounding error. |
| 144 | """ |
| 145 | data_dir, object_root, runtime = _install(tmp_path) |
| 146 | _make_order(data_dir) |
| 147 | _make_line(data_dir, id="ol_1", quantity="2.5", unit_price_cents="10000") |
| 148 | |
| 149 | _fire(runtime, object_root, "ol_1", "created") |
| 150 | |
| 151 | line = object_records.get_collection_record("order_lines", "ol_1", base_dir=data_dir) |
| 152 | assert line["line_total_cents"] == "25000" |
| 153 | |
| 154 | |
| 155 | def test_recompute_is_idempotent_and_skips_a_no_op_write(tmp_path): |
| 156 | data_dir, object_root, runtime = _install(tmp_path) |
| 157 | _make_order(data_dir) |
| 158 | _make_line(data_dir, id="ol_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 159 | |
| 160 | first = _fire(runtime, object_root, "ol_1", "created") |
| 161 | assert first.result["changed"] is True |
| 162 | |
| 163 | changes_before = object_record_changes.list_record_changes( |
| 164 | "orders", record_id="ord_1", base_dir=data_dir |
| 165 | )["total"] |
| 166 | |
| 167 | second = _fire(runtime, object_root, "ol_1", "updated") |
| 168 | assert second.result["changed"] is False |
| 169 | |
| 170 | changes_after = object_record_changes.list_record_changes( |
| 171 | "orders", record_id="ord_1", base_dir=data_dir |
| 172 | )["total"] |
| 173 | assert changes_after == changes_before # no redundant write/record_change |
| 174 | |
| 175 | |
| 176 | def test_deleting_a_line_recovers_order_id_from_record_changes_and_resums(tmp_path): |
| 177 | """The deleted line is already gone by the time this handler's |
| 178 | post-commit dispatch fires -- it must recover order_id from the |
| 179 | record_changes log's own 'before' snapshot, not the (now 404) live |
| 180 | record. |
| 181 | """ |
| 182 | data_dir, object_root, runtime = _install(tmp_path) |
| 183 | _make_order(data_dir) |
| 184 | _make_line(data_dir, id="ol_1", quantity="3", unit_price_cents="1999", tax_rate_bps="825") |
| 185 | _make_line(data_dir, id="ol_2", quantity="1", unit_price_cents="5000") |
| 186 | _fire(runtime, object_root, "ol_1", "created") |
| 187 | _fire(runtime, object_root, "ol_2", "created") |
| 188 | |
| 189 | order = object_records.get_collection_record("orders", "ord_1", base_dir=data_dir) |
| 190 | assert order["total_cents"] == "11491" |
| 191 | |
| 192 | object_records.delete_collection_record("order_lines", "ol_2", base_dir=data_dir, actor="test") |
| 193 | result = _fire(runtime, object_root, "ol_2", "deleted") |
| 194 | assert result.ok is True |
| 195 | assert result.result["order_id"] == "ord_1" |
| 196 | |
| 197 | order_after = object_records.get_collection_record("orders", "ord_1", base_dir=data_dir) |
| 198 | assert order_after["subtotal_cents"] == "5997" |
| 199 | assert order_after["tax_cents"] == "494" |
| 200 | assert order_after["total_cents"] == "6491" |
| 201 | |
| 202 | |
| 203 | def test_zero_lines_leaves_order_at_zero_totals(tmp_path): |
| 204 | data_dir, object_root, runtime = _install(tmp_path) |
| 205 | _make_order(data_dir) |
| 206 | |
| 207 | order = object_records.get_collection_record("orders", "ord_1", base_dir=data_dir) |
| 208 | assert order["subtotal_cents"] == "0" |
| 209 | assert order["total_cents"] == "0" |
| 210 | |
| 211 | |
| 212 | def test_event_for_an_unrelated_collection_is_a_no_op(tmp_path): |
| 213 | data_dir, object_root, runtime = _install(tmp_path) |
| 214 | result = object_execution.execute_object( |
| 215 | runtime, |
| 216 | object_execution.ObjectExecutionRequest( |
| 217 | "system_order_totals", |
| 218 | method="EVENT", |
| 219 | payload={"event": "notes.record.created", "collection": "notes", |
| 220 | "record_id": "n1", "action": "created"}, |
| 221 | ), |
| 222 | roots=[object_root], |
| 223 | ) |
| 224 | assert result.ok is True |
| 225 | assert result.result["skipped"] == "not an order_lines event" |
| 226 | |
| 227 | |
| 228 | # -- Guarded status transitions ------------------------------------------- |
| 229 | # |
| 230 | # Exercised directly against object_records' own guard (the same function |
| 231 | # update_collection_record calls internally) rather than round-tripping |
| 232 | # through the full HTTP server for one field's worth of behavior -- |
| 233 | # requires the schema actually installed in a data dir first, since |
| 234 | # get_schema resolves from base_dir, not from package source. |
| 235 | |
| 236 | def test_owner_may_move_draft_to_confirmed(tmp_path): |
| 237 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 238 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 239 | existing = {"status": "draft", "owner_id": "u1"} |
| 240 | updated = {"status": "confirmed", "owner_id": "u1"} |
| 241 | |
| 242 | object_records._validate_field_transitions( |
| 243 | "orders", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 244 | ) # no exception == allowed |
| 245 | |
| 246 | |
| 247 | def test_non_owner_may_not_move_draft_to_confirmed(tmp_path): |
| 248 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 249 | subject = object_permissions.PermissionSubject(user_id="someone_else") |
| 250 | existing = {"status": "draft", "owner_id": "u1"} |
| 251 | updated = {"status": "confirmed", "owner_id": "u1"} |
| 252 | |
| 253 | with pytest.raises(object_records.TransitionNotAllowedError): |
| 254 | object_records._validate_field_transitions( |
| 255 | "orders", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 256 | ) |
| 257 | |
| 258 | |
| 259 | def test_delivered_and_cancelled_are_terminal(tmp_path): |
| 260 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 261 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 262 | |
| 263 | for terminal_status in ("delivered", "cancelled"): |
| 264 | existing = {"status": terminal_status, "owner_id": "u1"} |
| 265 | updated = {"status": "draft", "owner_id": "u1"} |
| 266 | with pytest.raises(object_records.InvalidRecordPayloadError): |
| 267 | object_records._validate_field_transitions( |
| 268 | "orders", existing, updated, base_dir=data_dir, roots=None, subject=subject, |
| 269 | ) |
| 270 | |
| 271 | |
| 272 | def test_full_happy_path_lifecycle_is_legal(tmp_path): |
| 273 | """draft -> confirmed -> processing -> shipped -> delivered, the real |
| 274 | order lifecycle end to end. |
| 275 | """ |
| 276 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 277 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 278 | |
| 279 | path = ["draft", "confirmed", "processing", "shipped", "delivered"] |
| 280 | for before, after in zip(path, path[1:]): |
| 281 | object_records._validate_field_transitions( |
| 282 | "orders", {"status": before, "owner_id": "u1"}, {"status": after, "owner_id": "u1"}, |
| 283 | base_dir=data_dir, roots=None, subject=subject, |
| 284 | ) |
| 285 | |
| 286 | |
| 287 | def test_cancel_is_legal_from_draft_confirmed_and_processing_but_not_shipped(tmp_path): |
| 288 | data_dir, _object_root, _runtime = _install(tmp_path) |
| 289 | subject = object_permissions.PermissionSubject(user_id="u1") |
| 290 | |
| 291 | for cancellable in ("draft", "confirmed", "processing"): |
| 292 | object_records._validate_field_transitions( |
| 293 | "orders", {"status": cancellable, "owner_id": "u1"}, {"status": "cancelled", "owner_id": "u1"}, |
| 294 | base_dir=data_dir, roots=None, subject=subject, |
| 295 | ) |
| 296 | |
| 297 | with pytest.raises(object_records.InvalidRecordPayloadError): |
| 298 | object_records._validate_field_transitions( |
| 299 | "orders", {"status": "shipped", "owner_id": "u1"}, {"status": "cancelled", "owner_id": "u1"}, |
| 300 | base_dir=data_dir, roots=None, subject=subject, |
| 301 | ) |