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_reorder.py · 21.2 KB · 485 lines
| 1 | """Backorders and reorder points: selling what is not on the shelf, and |
| 2 | noticing before the shelf is empty. |
| 3 | |
| 4 | Three claims. |
| 5 | |
| 6 | **The default is yesterday.** products.backorder_policy defaults to |
| 7 | `refuse`, so a catalogue that has never heard of the field -- which is |
| 8 | every catalogue on every box today -- behaves byte-for-byte as it did. |
| 9 | That regression is the first test in this file and it is the one that |
| 10 | makes the rest of the feature safe to ship. |
| 11 | |
| 12 | **Refusing while remembering beats refusing and forgetting.** `notify` |
| 13 | still turns the customer away, and writes down that they came, because |
| 14 | losing the sale is survivable and losing the customer twice is not. |
| 15 | |
| 16 | **The pass suggests; the person decides.** system_reorder_check folds the |
| 17 | stock ledger and writes a suggestion. It does not raise a purchase order |
| 18 | and it never will: the fold can see the shelf and cannot see the lead |
| 19 | time, the case size, the supplier's minimum or the cash in the bank, and |
| 20 | a machine that orders on its own is how a business ends up with forty |
| 21 | pallets of the wrong thing. |
| 22 | |
| 23 | The two halves that are NOT done are held at the bottom as strict xfails, |
| 24 | the same way tests/test_dropship.py held its two: each needs one |
| 25 | condition in a package this slice does not own (app-orders' order_lines |
| 26 | column, app-shipping's fulfilment gate), and a green test over a shelf |
| 27 | that silently ships is worse than a red one. |
| 28 | """ |
| 29 | |
| 30 | import pathlib |
| 31 | import shutil |
| 32 | |
| 33 | import pytest |
| 34 | from conftest import stage_collection |
| 35 | |
| 36 | import object_cart |
| 37 | import object_execution |
| 38 | import object_ids |
| 39 | import object_records |
| 40 | import python_object_runtime |
| 41 | |
| 42 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 43 | PACKAGES = REPO_ROOT / "packages" |
| 44 | SHOP_OBJECTS = PACKAGES / "app-shop" / "objects" |
| 45 | CATALOG_OBJECTS = PACKAGES / "app-catalog" / "objects" |
| 46 | SHIPPING_OBJECTS = PACKAGES / "app-shipping" / "objects" |
| 47 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 48 | |
| 49 | TOKEN = "sess-back" |
| 50 | TODAY = "2026-06-15" |
| 51 | |
| 52 | |
| 53 | # --- the pure decision, with no data directory in sight ----------------------- |
| 54 | |
| 55 | def test_a_product_that_has_never_heard_of_the_field_still_refuses(): |
| 56 | """The whole safety of this feature is in one default. A blank, a |
| 57 | missing column and a value typed wrong must all read as `refuse`.""" |
| 58 | for product in ({}, {"backorder_policy": ""}, |
| 59 | {"backorder_policy": "yes please"}, None): |
| 60 | assert object_cart.backorder_policy(product) == "refuse" |
| 61 | assert object_cart.backorder_policy({"backorder_policy": "allow"}) == "allow" |
| 62 | |
| 63 | |
| 64 | def test_out_of_stock_splits_three_ways_and_only_one_of_them_sells(): |
| 65 | items = [{"id": "a", "product_id": "strict", "quantity": "5"}, |
| 66 | {"id": "b", "product_id": "loose", "quantity": "5"}, |
| 67 | {"id": "c", "product_id": "curious", "quantity": "5"}] |
| 68 | products = { |
| 69 | "strict": {"is_active": "true", "backorder_policy": "refuse"}, |
| 70 | "loose": {"is_active": "true", "backorder_policy": "allow"}, |
| 71 | "curious": {"is_active": "true", "backorder_policy": "notify"}, |
| 72 | } |
| 73 | on_hand = {"strict": 1, "loose": 1, "curious": 1} |
| 74 | blockers = object_cart.checkout_blockers( |
| 75 | items, products, on_hand, tracked=set(products)) |
| 76 | |
| 77 | assert [row["product_id"] for row in blockers["unavailable"]] == [ |
| 78 | "strict", "curious"] |
| 79 | assert [row["product_id"] for row in blockers["backordered"]] == ["loose"] |
| 80 | assert [row["product_id"] for row in blockers["notify"]] == ["curious"] |
| 81 | assert blockers["can_checkout"] is False # two of them still block |
| 82 | |
| 83 | # With only the backorderable line short, the basket goes through. |
| 84 | only_loose = object_cart.checkout_blockers( |
| 85 | [items[1]], products, on_hand, tracked=set(products)) |
| 86 | assert only_loose["can_checkout"] is True |
| 87 | assert only_loose["backordered"][0]["short_by"] == "4" |
| 88 | |
| 89 | |
| 90 | # --- the shop ----------------------------------------------------------------- |
| 91 | |
| 92 | def setup_env(tmp_path, monkeypatch, *, with_shipping=False): |
| 93 | data_dir = tmp_path / "data" |
| 94 | for pkg, name in (("app-shop", "carts"), ("app-shop", "cart_items"), |
| 95 | ("app-catalog", "products"), ("app-catalog", "locations"), |
| 96 | ("app-catalog", "stock_moves"), |
| 97 | ("app-catalog", "backorders"), |
| 98 | ("app-catalog", "reorder_suggestions"), |
| 99 | ("app-orders", "orders"), ("app-orders", "order_lines"), |
| 100 | ("app-invoices", "invoices"), |
| 101 | ("app-invoices", "invoice_lines"), |
| 102 | ("app-payments", "payments")): |
| 103 | stage_collection(data_dir, pkg, name) |
| 104 | if with_shipping: |
| 105 | for name in ("shipments", "shipment_lines"): |
| 106 | stage_collection(data_dir, "app-shipping", name) |
| 107 | stage_collection(data_dir, "app-settings", "app_settings") |
| 108 | |
| 109 | objects_root = tmp_path / "objects" |
| 110 | sources = [SHOP_OBJECTS, CATALOG_OBJECTS] |
| 111 | if with_shipping: |
| 112 | sources.append(SHIPPING_OBJECTS) |
| 113 | for source in sources: |
| 114 | shutil.copytree(source, objects_root, dirs_exist_ok=True) |
| 115 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 116 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root)) |
| 117 | object_records.create_collection_record( |
| 118 | "locations", |
| 119 | {"id": "loc-shelf", "name": "Shelf", "location_type": "warehouse", |
| 120 | "owner_id": "shop"}, base_dir=data_dir) |
| 121 | return data_dir, objects_root |
| 122 | |
| 123 | |
| 124 | def product(data_dir, product_id, name, cents, **fields): |
| 125 | record = {"id": product_id, "name": name, "sku": product_id.upper(), |
| 126 | "product_type": "physical", "price_cents": str(cents), |
| 127 | "currency": "USD", "is_active": "true", "owner_id": "shop"} |
| 128 | record.update({k: str(v) for k, v in fields.items()}) |
| 129 | return object_records.create_collection_record("products", record, |
| 130 | base_dir=data_dir) |
| 131 | |
| 132 | |
| 133 | def stock_in(data_dir, product_id, quantity): |
| 134 | return object_records.create_collection_record( |
| 135 | "stock_moves", |
| 136 | {"id": object_ids.new_uuid4(), "product_id": product_id, |
| 137 | "to_location_id": "loc-shelf", "quantity": str(quantity), |
| 138 | "reason": "purchase", "occurred_at": "2026-06-01", "owner_id": "shop"}, |
| 139 | base_dir=data_dir) |
| 140 | |
| 141 | |
| 142 | def run(object_id, payload, objects_root, *, method="POST"): |
| 143 | return object_execution.execute_object( |
| 144 | RUNTIME, |
| 145 | object_execution.ObjectExecutionRequest(object_id, method=method, |
| 146 | payload=payload), |
| 147 | roots=[objects_root]).result |
| 148 | |
| 149 | |
| 150 | def cart(objects_root, action="get", **payload): |
| 151 | return run("action_cart", {"session_token": TOKEN, "action": action, |
| 152 | **payload}, objects_root) |
| 153 | |
| 154 | |
| 155 | def checkout(objects_root, **payload): |
| 156 | payload.setdefault("today", TODAY) |
| 157 | return run("action_checkout", {"session_token": TOKEN, **payload}, |
| 158 | objects_root) |
| 159 | |
| 160 | |
| 161 | def rows(data_dir, collection): |
| 162 | return object_records.read_collection_records(collection, base_dir=data_dir) |
| 163 | |
| 164 | |
| 165 | # --- backorders --------------------------------------------------------------- |
| 166 | |
| 167 | def test_refuse_behaves_exactly_as_the_shop_behaved_yesterday(tmp_path, monkeypatch): |
| 168 | """The regression that makes the rest of this shippable: a product |
| 169 | with no policy set is refused with the same numbers, in the same |
| 170 | shape, and writes no backorder row at all.""" |
| 171 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 172 | product(data_dir, "p1", "Enamel Mug", 1000) |
| 173 | stock_in(data_dir, "p1", 1) |
| 174 | cart(objects_root, "add", product_id="p1", quantity="3") |
| 175 | |
| 176 | refused = checkout(objects_root, customer_email="ada@example.com") |
| 177 | assert refused["status"] == 409 |
| 178 | assert refused["error"] == "Some items cannot be ordered right now." |
| 179 | short = refused["unavailable"][0] |
| 180 | assert short["product_id"] == "p1" |
| 181 | assert short["wanted"] == "3" and short["available"] == "1" |
| 182 | assert short["short_by"] == "2" |
| 183 | assert not rows(data_dir, "orders") |
| 184 | assert not rows(data_dir, "backorders") |
| 185 | |
| 186 | |
| 187 | def test_allow_takes_the_order_and_marks_what_is_owed(tmp_path, monkeypatch): |
| 188 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 189 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="allow") |
| 190 | stock_in(data_dir, "p1", 1) |
| 191 | cart(objects_root, "add", product_id="p1", quantity="3") |
| 192 | |
| 193 | placed = checkout(objects_root, customer_email="ada@example.com") |
| 194 | assert placed["ok"] is True |
| 195 | assert placed["total_cents"] == 3000 # the whole line is sold |
| 196 | assert placed["backordered"][0]["short_by"] == "2" |
| 197 | assert "backordered" in placed["note"] |
| 198 | |
| 199 | waiting = rows(data_dir, "backorders") |
| 200 | assert len(waiting) == 1 |
| 201 | assert waiting[0]["kind"] == "backorder" |
| 202 | assert waiting[0]["quantity"] == "2" |
| 203 | assert waiting[0]["status"] == "open" |
| 204 | assert waiting[0]["order_id"] == placed["order_id"] |
| 205 | assert waiting[0]["customer_email"] == "ada@example.com" |
| 206 | assert waiting[0]["requested_on"] == TODAY |
| 207 | # The row names the LINE, which is what lets fulfilment leave it behind |
| 208 | # while the rest of the order goes out today. |
| 209 | line = rows(data_dir, "order_lines")[0] |
| 210 | assert waiting[0]["order_line_id"] == line["id"] |
| 211 | |
| 212 | |
| 213 | def test_notify_still_refuses_but_writes_down_who_asked(tmp_path, monkeypatch): |
| 214 | """Refusing while remembering beats refusing and forgetting, which is |
| 215 | how the same customer is lost twice.""" |
| 216 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 217 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="notify") |
| 218 | stock_in(data_dir, "p1", 0) |
| 219 | cart(objects_root, "add", product_id="p1", quantity="2") |
| 220 | |
| 221 | refused = checkout(objects_root, customer_email="ada@example.com") |
| 222 | assert refused["status"] == 409 |
| 223 | assert refused["unavailable"] # still a refusal |
| 224 | assert not rows(data_dir, "orders") |
| 225 | |
| 226 | interest = rows(data_dir, "backorders") |
| 227 | assert len(interest) == 1 |
| 228 | assert interest[0]["kind"] == "notify" |
| 229 | assert interest[0]["order_id"] == "" # nothing was sold |
| 230 | assert interest[0]["customer_email"] == "ada@example.com" |
| 231 | assert refused["interest_recorded"][0]["product_id"] == "p1" |
| 232 | |
| 233 | # And pressing the button five times is one person waiting, not five. |
| 234 | checkout(objects_root, customer_email="ada@example.com") |
| 235 | checkout(objects_root, customer_email="ADA@example.com") |
| 236 | assert len(rows(data_dir, "backorders")) == 1 |
| 237 | |
| 238 | |
| 239 | def test_a_preview_of_a_refused_basket_files_no_interest(tmp_path, monkeypatch): |
| 240 | """A preview is a look. The rule that a preview writes nothing has to |
| 241 | hold on the refusal path too, or a storefront that quotes before it |
| 242 | submits files a waiting-list entry for everybody who glanced at an |
| 243 | empty shelf.""" |
| 244 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 245 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="notify") |
| 246 | stock_in(data_dir, "p1", 0) |
| 247 | cart(objects_root, "add", product_id="p1", quantity="1") |
| 248 | |
| 249 | quoted = checkout(objects_root, preview="true", |
| 250 | customer_email="ada@example.com") |
| 251 | assert quoted["status"] == 409 |
| 252 | assert rows(data_dir, "backorders") == [] |
| 253 | |
| 254 | |
| 255 | def test_interest_with_nowhere_to_send_it_is_not_recorded(tmp_path, monkeypatch): |
| 256 | """A record of interest with no way to reach the interested party is a |
| 257 | record of nothing.""" |
| 258 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 259 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="notify") |
| 260 | stock_in(data_dir, "p1", 0) |
| 261 | cart(objects_root, "add", product_id="p1", quantity="1") |
| 262 | |
| 263 | refused = checkout(objects_root) # no email at all |
| 264 | assert refused["status"] == 409 |
| 265 | assert rows(data_dir, "backorders") == [] |
| 266 | assert "interest_recorded" not in refused |
| 267 | |
| 268 | |
| 269 | def test_the_backorder_count_is_the_open_ones_and_names_the_longest_wait(tmp_path, monkeypatch): |
| 270 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 271 | product(data_dir, "p1", "Mug", 1000) |
| 272 | for row_id, status, day in (("b1", "open", "2026-06-01"), |
| 273 | ("b2", "open", "2026-06-10"), |
| 274 | ("b3", "filled", "2026-01-01")): |
| 275 | object_records.create_collection_record( |
| 276 | "backorders", |
| 277 | {"id": row_id, "product_id": "p1", "kind": "backorder", |
| 278 | "quantity": "1", "status": status, "requested_on": day, |
| 279 | "owner_id": "shop"}, base_dir=data_dir) |
| 280 | |
| 281 | counted = run("system_backorder_attention", {}, objects_root, |
| 282 | method="COUNT") |
| 283 | assert counted["count"] == 2 |
| 284 | assert "oldest waiting" in counted["detail"] |
| 285 | |
| 286 | |
| 287 | # --- reorder points ----------------------------------------------------------- |
| 288 | |
| 289 | def reorder(objects_root, **payload): |
| 290 | payload.setdefault("today", TODAY) |
| 291 | return run("system_reorder_check", payload, objects_root) |
| 292 | |
| 293 | |
| 294 | def test_the_pass_suggests_and_never_orders(tmp_path, monkeypatch): |
| 295 | """A machine that raises purchase orders on its own is how a business |
| 296 | orders forty pallets of the wrong thing.""" |
| 297 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 298 | product(data_dir, "p1", "Enamel Mug", 1000, reorder_point=5, |
| 299 | reorder_quantity=24) |
| 300 | stock_in(data_dir, "p1", 3) |
| 301 | |
| 302 | result = reorder(objects_root) |
| 303 | assert result["suggested"] == 1 |
| 304 | assert result["ordered"] == 0 |
| 305 | assert "never raises a purchase order" in result["note"] |
| 306 | |
| 307 | suggestion = rows(data_dir, "reorder_suggestions")[0] |
| 308 | assert suggestion["product_id"] == "p1" |
| 309 | assert suggestion["on_hand"] == "3" |
| 310 | assert suggestion["reorder_point"] == "5" |
| 311 | assert suggestion["suggested_quantity"] == "24" |
| 312 | assert suggestion["status"] == "open" |
| 313 | assert suggestion["suggested_on"] == TODAY |
| 314 | |
| 315 | # Nothing anywhere that looks like a purchase. |
| 316 | assert not rows(data_dir, "orders") |
| 317 | assert not rows(data_dir, "stock_moves") or all( |
| 318 | row["reason"] == "purchase" and row["id"] != suggestion["id"] |
| 319 | for row in rows(data_dir, "stock_moves")) |
| 320 | |
| 321 | |
| 322 | def test_the_threshold_is_at_or_below_and_not_merely_below(tmp_path, monkeypatch): |
| 323 | """'Reorder at 5' means five is already the moment. A shop that had to |
| 324 | fall to four has been given a number that means something else.""" |
| 325 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 326 | product(data_dir, "at", "At the line", 1000, reorder_point=5) |
| 327 | product(data_dir, "over", "Above it", 1000, reorder_point=5) |
| 328 | stock_in(data_dir, "at", 5) |
| 329 | stock_in(data_dir, "over", 6) |
| 330 | |
| 331 | reorder(objects_root) |
| 332 | assert [row["product_id"] for row in rows(data_dir, "reorder_suggestions")] == ["at"] |
| 333 | |
| 334 | |
| 335 | def test_a_reorder_point_of_zero_is_off_not_reorder_at_zero(tmp_path, monkeypatch): |
| 336 | """Most catalogues have a handful of lines anybody actually reorders, |
| 337 | and a queue lit up by every service and made-to-order item is a queue |
| 338 | nobody reads.""" |
| 339 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 340 | product(data_dir, "p1", "Never reordered", 1000) # no point set |
| 341 | stock_in(data_dir, "p1", 0) |
| 342 | |
| 343 | result = reorder(objects_root) |
| 344 | assert result["checked"] == 0 |
| 345 | assert rows(data_dir, "reorder_suggestions") == [] |
| 346 | |
| 347 | |
| 348 | def test_an_inactive_product_is_not_reordered(tmp_path, monkeypatch): |
| 349 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 350 | product(data_dir, "p1", "Discontinued", 1000, reorder_point=5, |
| 351 | is_active="false") |
| 352 | stock_in(data_dir, "p1", 0) |
| 353 | reorder(objects_root) |
| 354 | assert rows(data_dir, "reorder_suggestions") == [] |
| 355 | |
| 356 | |
| 357 | def test_a_second_pass_refreshes_the_open_row_instead_of_adding_another(tmp_path, monkeypatch): |
| 358 | """A nightly pass that appended would turn one true fact into ninety |
| 359 | within a quarter, which is how a good signal becomes noise.""" |
| 360 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 361 | product(data_dir, "p1", "Enamel Mug", 1000, reorder_point=5, |
| 362 | reorder_quantity=24) |
| 363 | stock_in(data_dir, "p1", 3) |
| 364 | reorder(objects_root) |
| 365 | |
| 366 | stock_in(data_dir, "p1", -1) |
| 367 | second = reorder(objects_root, today="2026-06-16") |
| 368 | assert second["suggested"] == 0 and second["refreshed"] == 1 |
| 369 | |
| 370 | suggestions = rows(data_dir, "reorder_suggestions") |
| 371 | assert len(suggestions) == 1 |
| 372 | assert suggestions[0]["on_hand"] == "2" |
| 373 | assert suggestions[0]["suggested_on"] == "2026-06-16" |
| 374 | |
| 375 | |
| 376 | def test_a_dismissed_suggestion_is_left_alone_and_stops_counting(tmp_path, monkeypatch): |
| 377 | """A count nobody can dismiss stays lit forever and teaches people to |
| 378 | ignore the band it sits in.""" |
| 379 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 380 | product(data_dir, "p1", "Enamel Mug", 1000, reorder_point=5) |
| 381 | stock_in(data_dir, "p1", 3) |
| 382 | reorder(objects_root) |
| 383 | suggestion = rows(data_dir, "reorder_suggestions")[0] |
| 384 | |
| 385 | object_records.update_collection_record( |
| 386 | "reorder_suggestions", suggestion["id"], {"status": "dismissed"}, |
| 387 | base_dir=data_dir, actor="operator") |
| 388 | assert run("system_reorder_attention", {}, objects_root, |
| 389 | method="COUNT")["count"] == 0 |
| 390 | |
| 391 | # The next pass raises a fresh one rather than reopening the answered |
| 392 | # row: the human said no to what they were shown, not to the question |
| 393 | # forever. |
| 394 | again = reorder(objects_root, today="2026-06-16") |
| 395 | assert again["suggested"] == 1 |
| 396 | assert {row["status"] for row in rows(data_dir, "reorder_suggestions")} == { |
| 397 | "dismissed", "open"} |
| 398 | |
| 399 | |
| 400 | def test_the_reorder_count_keys_on_the_open_rows_and_names_the_worst(tmp_path, monkeypatch): |
| 401 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 402 | product(data_dir, "deep", "Very low", 1000, reorder_point=10) |
| 403 | product(data_dir, "shallow", "Just under", 1000, reorder_point=5) |
| 404 | stock_in(data_dir, "deep", 2) |
| 405 | stock_in(data_dir, "shallow", 4) |
| 406 | reorder(objects_root) |
| 407 | |
| 408 | counted = run("system_reorder_attention", {}, objects_root, method="COUNT") |
| 409 | assert counted["count"] == 2 |
| 410 | assert counted["detail"] == "one is 8 below its point" |
| 411 | |
| 412 | |
| 413 | def test_the_pass_costs_nothing_on_a_box_with_no_catalogue(tmp_path, monkeypatch): |
| 414 | """A pass should not log an error every night about an app nobody |
| 415 | installed.""" |
| 416 | data_dir = tmp_path / "data" |
| 417 | (data_dir / "schemas").mkdir(parents=True) |
| 418 | objects_root = tmp_path / "objects" |
| 419 | shutil.copytree(CATALOG_OBJECTS, objects_root, dirs_exist_ok=True) |
| 420 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 421 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root)) |
| 422 | |
| 423 | assert reorder(objects_root)["suggested"] == 0 |
| 424 | assert run("system_reorder_attention", {}, objects_root, |
| 425 | method="COUNT") == {"count": 0} |
| 426 | assert run("system_backorder_attention", {}, objects_root, |
| 427 | method="COUNT") == {"count": 0} |
| 428 | |
| 429 | |
| 430 | # --- the halves that are NOT done, held as specifications --------------------- |
| 431 | # |
| 432 | # Each needs one condition in a package this slice does not own, and |
| 433 | # hacking around a sibling package's handler would be exactly the kind of |
| 434 | # "make it work by the data" a stock rule cannot afford. Writing the |
| 435 | # acceptance test first and leaving it red is the honest move -- the same |
| 436 | # thing tests/test_dropship.py did for its two conditions -- because a |
| 437 | # green test over a shelf that silently ships is worse than a red one. |
| 438 | # |
| 439 | # FOLLOW-UP 1 (app-orders): order_lines needs a `backordered` boolean. |
| 440 | # action_checkout already stamps it through the same _has_field check it |
| 441 | # uses for shipping_cents and line_note, so the day the column exists |
| 442 | # this passes with no change here and no change there. |
| 443 | # |
| 444 | # FOLLOW-UP 2 (app-shipping): action_create_shipment defaults to |
| 445 | # EVERYTHING unshipped, which today includes a line the shop has openly |
| 446 | # agreed it does not have. It needs to skip lines that are backordered |
| 447 | # -- readable either from the order_lines column above or from the |
| 448 | # backorders rows this slice writes -- and system_order_fulfillment must |
| 449 | # agree, or the shop ships goods that are not in the building and the |
| 450 | # stock ledger goes negative on a sale nobody could pick. |
| 451 | |
| 452 | |
| 453 | def test_a_backordered_line_is_marked_on_the_order_itself(tmp_path, monkeypatch): |
| 454 | """The picker and the packer read the ORDER. A fact that lives only in |
| 455 | another app's collection is a fact the warehouse never sees.""" |
| 456 | data_dir, objects_root = setup_env(tmp_path, monkeypatch) |
| 457 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="allow") |
| 458 | stock_in(data_dir, "p1", 1) |
| 459 | cart(objects_root, "add", product_id="p1", quantity="3") |
| 460 | checkout(objects_root, customer_email="ada@example.com") |
| 461 | |
| 462 | line = rows(data_dir, "order_lines")[0] |
| 463 | assert line.get("backordered") == "true" |
| 464 | |
| 465 | |
| 466 | def test_a_backordered_line_does_not_go_in_the_box(tmp_path, monkeypatch): |
| 467 | """The whole promise of `allow` is that the sale happens and the goods |
| 468 | follow. Shipping the part that does not exist breaks both halves at |
| 469 | once: the customer gets a short parcel and the shelf shows minus two.""" |
| 470 | data_dir, objects_root = setup_env(tmp_path, monkeypatch, with_shipping=True) |
| 471 | product(data_dir, "p1", "Enamel Mug", 1000, backorder_policy="allow") |
| 472 | stock_in(data_dir, "p1", 1) |
| 473 | cart(objects_root, "add", product_id="p1", quantity="3") |
| 474 | placed = checkout(objects_root, customer_email="ada@example.com") |
| 475 | object_records.update_collection_record( |
| 476 | "orders", placed["order_id"], {"status": "confirmed"}, |
| 477 | base_dir=data_dir, actor="test") |
| 478 | |
| 479 | shipment = run("action_create_shipment", |
| 480 | {"order_id": placed["order_id"], "today": TODAY}, |
| 481 | objects_root) |
| 482 | packed = [row for row in rows(data_dir, "shipment_lines") |
| 483 | if row["shipment_id"] == shipment.get("shipment_id")] |
| 484 | # One on the shelf, two owed: the box holds one. |
| 485 | assert [row["quantity"] for row in packed] == ["1"] |