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_dropship.py · 26.6 KB · 603 lines
| 1 | """Drop-shipping: the composition test the whole fulfillment build was |
| 2 | measured against. |
| 3 | |
| 4 | plan/fulfillment-logistics-spec.md set it in as many words -- "a sale |
| 5 | order flagged `fulfillment: dropship` produces a linked PURCHASE order to |
| 6 | the vendor carrying the customer's ship-to; receiving is skipped (goods |
| 7 | never touch our shelf); margin is already a read because both orders |
| 8 | carry money. If the shipment model can't express this cleanly, the model |
| 9 | is wrong." |
| 10 | |
| 11 | The model expresses it, and it does so with no new collection anywhere, |
| 12 | because the decision made in orders v1 -- sale and purchase orders share |
| 13 | one schema -- turns out to have already modelled a drop-ship as two rows |
| 14 | pointing at each other. That is what the first half of this file asserts: |
| 15 | one PO, confirmed, carrying the customer's ship-to onto the vendor's |
| 16 | document, linked both ways, refusing a second PO and refusing an order |
| 17 | whose goods have already come off our shelf. |
| 18 | |
| 19 | **The stock rule is the test that matters**, and it has two halves that |
| 20 | this file deliberately reports differently, because they are in different |
| 21 | states and pretending otherwise would be the dishonest kind of green. |
| 22 | |
| 23 | The half that is done: the drop-ship flow itself moves no stock. No |
| 24 | handler fires, no move is composed, and the shelf is provably unchanged |
| 25 | from end to end. |
| 26 | |
| 27 | The half that is NOT: app-shipping's system_order_fulfillment composes a |
| 28 | `sale` move for every shipment line the moment a shipment reaches |
| 29 | `shipped`, and it does not look at fulfillment_source -- so the vendor's |
| 30 | dispatch, recorded as a shipment against the sale order the way the spec |
| 31 | describes, would decrement a shelf that never held the goods. The order |
| 32 | carries the fact the handler needs; the handler does not read it. That is |
| 33 | one condition in a package the disputes/drop-ship slice does not own, so |
| 34 | it is a STRICT XFAIL here rather than a hack in a package that is not |
| 35 | ours -- the same posture tests/test_cogs_on_sale.py takes toward the |
| 36 | missing COGS journal. The day somebody adds the condition, the acceptance |
| 37 | test is already written and this file goes red if the fix does not match |
| 38 | what was specified. |
| 39 | """ |
| 40 | |
| 41 | import pathlib |
| 42 | import shutil |
| 43 | |
| 44 | import pytest |
| 45 | from conftest import stage_collection |
| 46 | |
| 47 | import object_execution |
| 48 | import object_ids |
| 49 | import object_records |
| 50 | import object_stock |
| 51 | import python_object_runtime |
| 52 | |
| 53 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 54 | PACKAGES = REPO_ROOT / "packages" |
| 55 | ORDERS_OBJECTS = PACKAGES / "app-orders" / "objects" |
| 56 | SHIPPING_OBJECTS = PACKAGES / "app-shipping" / "objects" |
| 57 | RECEIVING_OBJECTS = PACKAGES / "app-receiving" / "objects" |
| 58 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 59 | |
| 60 | |
| 61 | def setup_env(tmp_path, monkeypatch, *, settings=()): |
| 62 | """A data dir with every collection this slice touches, and ONE object |
| 63 | root holding app-orders' objects beside app-shipping's and |
| 64 | app-receiving's. |
| 65 | |
| 66 | The merged root is what an installed server actually looks like, and |
| 67 | it is the only way the two gap tests at the bottom can fire the REAL |
| 68 | handlers rather than a description of them. |
| 69 | """ |
| 70 | data_dir = tmp_path / "data" |
| 71 | for pkg, name in (("app-orders", "orders"), ("app-orders", "order_lines"), |
| 72 | ("app-catalog", "products"), ("app-catalog", "locations"), |
| 73 | ("app-catalog", "stock_moves"), |
| 74 | ("app-contacts", "contacts"), |
| 75 | ("app-shipping", "shipments"), |
| 76 | ("app-shipping", "shipment_lines"), |
| 77 | ("app-receiving", "receipts"), |
| 78 | ("app-receiving", "receipt_lines")): |
| 79 | stage_collection(data_dir, pkg, name) |
| 80 | rows = "".join(f"s{i}\t{k}\t{v}\t\n" for i, (k, v) in enumerate(settings)) |
| 81 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 82 | |
| 83 | objects_root = tmp_path / "objects" |
| 84 | for source in (ORDERS_OBJECTS, SHIPPING_OBJECTS, RECEIVING_OBJECTS): |
| 85 | shutil.copytree(source, objects_root, dirs_exist_ok=True) |
| 86 | |
| 87 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 88 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_root)) |
| 89 | return data_dir, objects_root |
| 90 | |
| 91 | |
| 92 | def run(objects_root, object_id, method, payload): |
| 93 | return object_execution.execute_object( |
| 94 | RUNTIME, |
| 95 | object_execution.ObjectExecutionRequest( |
| 96 | object_id, method=method, payload=payload), |
| 97 | roots=[objects_root]).result |
| 98 | |
| 99 | |
| 100 | # --- fixtures shaped like a real shop ----------------------------------------- |
| 101 | |
| 102 | def product(data_dir, product_id="p1", name="Enamel Mug", cents=1200, |
| 103 | cost_cents=None): |
| 104 | record = {"id": product_id, "name": name, "sku": product_id.upper(), |
| 105 | "product_type": "physical", "price_cents": str(cents), |
| 106 | "currency": "USD", "is_active": "true", "owner_id": "shop"} |
| 107 | if cost_cents is not None: |
| 108 | record["cost_cents"] = str(cost_cents) |
| 109 | return object_records.create_collection_record("products", record, |
| 110 | base_dir=data_dir) |
| 111 | |
| 112 | |
| 113 | def location(data_dir, location_id, name, kind="warehouse"): |
| 114 | return object_records.create_collection_record( |
| 115 | "locations", |
| 116 | {"id": location_id, "name": name, "location_type": kind, |
| 117 | "owner_id": "shop"}, |
| 118 | base_dir=data_dir) |
| 119 | |
| 120 | |
| 121 | def vendor(data_dir, vendor_id="ven-1"): |
| 122 | """A supplier filed as a contact. full_name is COMPUTED on contacts, |
| 123 | so it is not written here -- the name the purchase order carries is |
| 124 | whatever app-contacts derives, which is the point of asking the |
| 125 | collection rather than stamping a string.""" |
| 126 | return object_records.create_collection_record( |
| 127 | "contacts", |
| 128 | {"id": vendor_id, "first_name": "Kiln & Clay", "last_name": "Ltd", |
| 129 | "email": "sales@kiln.test", "owner_id": "shop"}, |
| 130 | base_dir=data_dir) |
| 131 | |
| 132 | |
| 133 | def order(data_dir, order_id="ord-1", *, status="confirmed", number="SO-0001", |
| 134 | owner="shop", **fields): |
| 135 | record = {"id": order_id, "doc_type": "sale", "number": number, |
| 136 | "customer_name": "Ada Lovelace", |
| 137 | "customer_email": "ada@example.test", "currency": "USD", |
| 138 | "status": "draft", "order_date": "2026-07-01", "owner_id": owner} |
| 139 | record.update({k: str(v) for k, v in fields.items()}) |
| 140 | object_records.create_collection_record("orders", record, |
| 141 | base_dir=data_dir) |
| 142 | # Through the ladder, the way a human would: draft is where every order |
| 143 | # starts. |
| 144 | for step in ("confirmed", status): |
| 145 | current = object_records.get_collection_record( |
| 146 | "orders", order_id, base_dir=data_dir)["status"] |
| 147 | if step not in ("draft", current): |
| 148 | object_records.update_collection_record( |
| 149 | "orders", order_id, {"status": step}, |
| 150 | base_dir=data_dir, actor="test") |
| 151 | return object_records.get_collection_record("orders", order_id, |
| 152 | base_dir=data_dir) |
| 153 | |
| 154 | |
| 155 | def order_line(data_dir, line_id, order_id="ord-1", *, product_id="p1", |
| 156 | description="Enamel Mug", quantity="3", cents=1200): |
| 157 | total = int(float(quantity) * cents) |
| 158 | return object_records.create_collection_record( |
| 159 | "order_lines", |
| 160 | {"id": line_id, "order_id": order_id, "product_id": product_id, |
| 161 | "description": description, "quantity": str(quantity), |
| 162 | "unit_price_cents": str(cents), "line_total_cents": str(total), |
| 163 | "tax_rate_bps": "0", "line_tax_cents": "0", "owner_id": "shop"}, |
| 164 | base_dir=data_dir) |
| 165 | |
| 166 | |
| 167 | def stock_in(data_dir, product_id="p1", quantity=20, *, to="loc-shelf"): |
| 168 | return object_records.create_collection_record( |
| 169 | "stock_moves", |
| 170 | {"id": object_ids.new_uuid4(), "product_id": product_id, |
| 171 | "to_location_id": to, "quantity": str(quantity), "reason": "purchase", |
| 172 | "occurred_at": "2026-06-01", "owner_id": "shop"}, |
| 173 | base_dir=data_dir) |
| 174 | |
| 175 | |
| 176 | def moves(data_dir, reason=None): |
| 177 | rows = object_records.read_collection_records("stock_moves", |
| 178 | base_dir=data_dir) |
| 179 | if reason is None: |
| 180 | return rows |
| 181 | return [row for row in rows if row["reason"] == reason] |
| 182 | |
| 183 | |
| 184 | def on_shelf(data_dir, product_id="p1", location_id="loc-shelf"): |
| 185 | return object_stock.quantity_at_location(product_id, location_id, |
| 186 | base_dir=data_dir) |
| 187 | |
| 188 | |
| 189 | def orders_of(data_dir, doc_type=None): |
| 190 | rows = object_records.read_collection_records("orders", base_dir=data_dir) |
| 191 | if doc_type is None: |
| 192 | return rows |
| 193 | return [row for row in rows if row["doc_type"] == doc_type] |
| 194 | |
| 195 | |
| 196 | def lines_of(data_dir, order_id): |
| 197 | return [row for row in object_records.read_collection_records( |
| 198 | "order_lines", base_dir=data_dir) if row["order_id"] == order_id] |
| 199 | |
| 200 | |
| 201 | def dropship(objects_root, **payload): |
| 202 | request = {"order_id": "ord-1", "vendor_id": "ven-1"} |
| 203 | request.update(payload) |
| 204 | return run(objects_root, "action_dropship_order", "POST", request) |
| 205 | |
| 206 | |
| 207 | def margin(objects_root, order_id="ord-1", user_id="shop"): |
| 208 | return run(objects_root, "site_dropship_margin", "GET", |
| 209 | {"order_id": order_id, "_identity": {"user_id": user_id}}) |
| 210 | |
| 211 | |
| 212 | def shop_that_can_dropship(tmp_path, monkeypatch, *, cost_cents=None, |
| 213 | address="12 Analytical Way\nLondon"): |
| 214 | """A shop with stock on the shelf and one confirmed sale order for |
| 215 | three mugs, with somewhere for a parcel to go.""" |
| 216 | data_dir, objects_root = setup_env( |
| 217 | tmp_path, monkeypatch, |
| 218 | settings=[("shop.stock_location", "loc-shelf"), |
| 219 | ("shop.customer_location", "loc-customer")]) |
| 220 | location(data_dir, "loc-shelf", "Shelf") |
| 221 | location(data_dir, "loc-customer", "Customers", kind="customer") |
| 222 | product(data_dir, cost_cents=cost_cents) |
| 223 | stock_in(data_dir) |
| 224 | vendor(data_dir) |
| 225 | order(data_dir, ship_to_address=address) |
| 226 | order_line(data_dir, "line-1") |
| 227 | return data_dir, objects_root |
| 228 | |
| 229 | |
| 230 | # --- one sale order spawns exactly one purchase order -------------------------- |
| 231 | |
| 232 | def test_a_dropship_order_spawns_one_po_carrying_the_customers_address( |
| 233 | tmp_path, monkeypatch): |
| 234 | """The whole shape in one assertion block: the vendor is the |
| 235 | counterparty, the customer is the destination, and this is the only |
| 236 | place in the repo where those are different people.""" |
| 237 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 238 | result = dropship(objects_root, vendor_price_cents="700", |
| 239 | today="2026-07-20") |
| 240 | assert result["ok"] is True |
| 241 | assert result["lines"] == 1 |
| 242 | |
| 243 | purchases = orders_of(data_dir, "purchase") |
| 244 | assert len(purchases) == 1 |
| 245 | po = purchases[0] |
| 246 | assert po["id"] == result["purchase_order_id"] |
| 247 | assert po["number"] == "PO-SO-0001" |
| 248 | # Confirmed, not draft: the operator choosing a vendor IS the |
| 249 | # commitment to buy, and a drop-ship PO in draft is a customer waiting |
| 250 | # on a parcel nobody ordered. |
| 251 | assert po["status"] == "confirmed" |
| 252 | assert po["fulfillment_source"] == "dropship" |
| 253 | assert po["vendor_id"] == "ven-1" |
| 254 | # The counterparty is the vendor, named the way every other PO in this |
| 255 | # repo names its supplier. |
| 256 | assert po["customer_name"] == "Kiln & Clay Ltd" |
| 257 | # ...and the goods go somewhere else entirely. |
| 258 | assert po["ship_to_name"] == "Ada Lovelace" |
| 259 | assert po["ship_to_address"] == "12 Analytical Way\nLondon" |
| 260 | assert "orders/ord-1" in po["notes"] |
| 261 | |
| 262 | po_lines = lines_of(data_dir, po["id"]) |
| 263 | assert len(po_lines) == 1 |
| 264 | assert po_lines[0]["description"] == "Enamel Mug" |
| 265 | assert po_lines[0]["quantity"] == "3" |
| 266 | assert po_lines[0]["unit_price_cents"] == "700" |
| 267 | assert po_lines[0]["line_total_cents"] == "2100" |
| 268 | |
| 269 | # Linked both ways, so either end answers on its own and no handler |
| 270 | # ever has to join to find out what it is looking at. |
| 271 | sale = object_records.get_collection_record("orders", "ord-1", |
| 272 | base_dir=data_dir) |
| 273 | assert sale["fulfillment_source"] == "dropship" |
| 274 | assert sale["linked_order_id"] == po["id"] |
| 275 | assert sale["vendor_id"] == "ven-1" |
| 276 | assert po["linked_order_id"] == "ord-1" |
| 277 | |
| 278 | |
| 279 | def test_drop_shipping_the_same_order_twice_is_refused_with_the_po_that_exists( |
| 280 | tmp_path, monkeypatch): |
| 281 | """A duplicate PO is a duplicate commitment to buy, with a supplier on |
| 282 | the other end who will happily send the goods twice.""" |
| 283 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 284 | first = dropship(objects_root, vendor_price_cents="700") |
| 285 | assert first["ok"] |
| 286 | |
| 287 | refused = dropship(objects_root, vendor_price_cents="700") |
| 288 | assert refused["status"] == 409 |
| 289 | assert "already drop-shipped" in refused["already_linked"] |
| 290 | assert "PO-SO-0001" in refused["already_linked"] |
| 291 | assert len(orders_of(data_dir, "purchase")) == 1 |
| 292 | |
| 293 | |
| 294 | def test_a_purchase_order_cannot_itself_be_drop_shipped(tmp_path, monkeypatch): |
| 295 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 296 | order(data_dir, "po-9", doc_type="purchase", number="PO-9", |
| 297 | customer_name="Kiln & Clay Ltd") |
| 298 | order_line(data_dir, "po-9-line", order_id="po-9") |
| 299 | |
| 300 | refused = dropship(objects_root, order_id="po-9") |
| 301 | assert refused["status"] == 409 |
| 302 | assert "That is a purchase order" in refused["wrong_document"] |
| 303 | |
| 304 | |
| 305 | def test_every_blocker_is_reported_at_once(tmp_path, monkeypatch): |
| 306 | """Whoever is doing this is usually choosing between two vendors with |
| 307 | a customer waiting; revealing one problem at a time is how a screen |
| 308 | gets abandoned.""" |
| 309 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 310 | object_records.update_collection_record( |
| 311 | "orders", "ord-1", {"status": "cancelled"}, |
| 312 | base_dir=data_dir, actor="test") |
| 313 | |
| 314 | refused = dropship(objects_root, order_id="ord-1", vendor_id="") |
| 315 | assert refused["status"] == 409 |
| 316 | assert "cancelled" in refused["order_status"] |
| 317 | assert "has to name who is shipping it" in refused["missing_vendor"] |
| 318 | assert orders_of(data_dir, "purchase") == [] |
| 319 | |
| 320 | |
| 321 | # --- you cannot drop-ship what you already picked ------------------------------ |
| 322 | |
| 323 | def picked_from_stock(data_dir, order_id="ord-1", shipment_id="ship-1"): |
| 324 | """A parcel packed and sent from our own shelf, with the `sale` move |
| 325 | system_order_fulfillment composes for it. Written directly, the way a |
| 326 | trusted server-side write is: this file is about what happens NEXT.""" |
| 327 | object_records.create_collection_record( |
| 328 | "shipments", |
| 329 | {"id": shipment_id, "order_id": order_id, "direction": "outbound", |
| 330 | "status": "open", "service": "ground", "ship_to_name": "Ada Lovelace", |
| 331 | "shipped_on": "2026-07-05", "owner_id": "shop"}, |
| 332 | base_dir=data_dir) |
| 333 | object_records.create_collection_record( |
| 334 | "shipment_lines", |
| 335 | {"id": f"{shipment_id}-line-0", "shipment_id": shipment_id, |
| 336 | "order_line_id": "line-1", "product_id": "p1", |
| 337 | "description": "Enamel Mug", "quantity": "3", "owner_id": "shop"}, |
| 338 | base_dir=data_dir) |
| 339 | object_records.create_collection_record( |
| 340 | "stock_moves", |
| 341 | {"id": object_ids.new_uuid4(), "product_id": "p1", |
| 342 | "from_location_id": "loc-shelf", "to_location_id": "loc-customer", |
| 343 | "quantity": "3", "reason": "sale", |
| 344 | "reference": f"shipments/{shipment_id}:line/{shipment_id}-line-0 SO-0001", |
| 345 | "occurred_at": "2026-07-05", "owner_id": "shop"}, |
| 346 | base_dir=data_dir) |
| 347 | |
| 348 | |
| 349 | def test_drop_shipping_an_order_with_stock_movement_is_refused( |
| 350 | tmp_path, monkeypatch): |
| 351 | """The goods left our shelf, so the sale is ours to fulfill. Raising a |
| 352 | PO now would buy a second set of the same units, and the refusal names |
| 353 | the moves so somebody can see whether they picked the wrong order or |
| 354 | the wrong day.""" |
| 355 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 356 | picked_from_stock(data_dir) |
| 357 | |
| 358 | refused = dropship(objects_root, vendor_price_cents="700") |
| 359 | assert refused["status"] == 409 |
| 360 | assert len(refused["stock_moved"]) == 1 |
| 361 | moved = refused["stock_moved"][0] |
| 362 | assert moved["product_id"] == "p1" |
| 363 | assert moved["quantity"] == "3" |
| 364 | assert moved["reason"] == "sale" |
| 365 | assert "shipments/ship-1:" in moved["reference"] |
| 366 | assert refused["shipments"] == ["ship-1"] |
| 367 | |
| 368 | # Nothing half-written, and the sale order still says what it is. |
| 369 | assert orders_of(data_dir, "purchase") == [] |
| 370 | assert object_records.get_collection_record( |
| 371 | "orders", "ord-1", base_dir=data_dir)["fulfillment_source"] == "stock" |
| 372 | |
| 373 | |
| 374 | # --- THE composition test: a drop-ship order moves no stock -------------------- |
| 375 | |
| 376 | def test_a_dropship_order_moves_no_stock_end_to_end(tmp_path, monkeypatch): |
| 377 | """The property the whole slice is judged on, asserted over the shelf |
| 378 | itself rather than over a handler's return value. |
| 379 | |
| 380 | Twenty mugs on the shelf before, twenty after: not one move composed |
| 381 | anywhere, no receipt raised, and the purchase order still sitting at |
| 382 | confirmed because nothing on this server creates a receipt on its own. |
| 383 | That last clause is why receiving is skipped BY CONSTRUCTION rather |
| 384 | than by a gate -- system_receipt_posting only ever reacts to a |
| 385 | `receipts` row a human raised. |
| 386 | """ |
| 387 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 388 | before = on_shelf(data_dir) |
| 389 | assert before == 20 |
| 390 | moves_before = len(moves(data_dir)) |
| 391 | |
| 392 | result = dropship(objects_root, vendor_price_cents="700") |
| 393 | assert result["ok"] is True |
| 394 | assert "never touch our shelf" in result["note"] |
| 395 | |
| 396 | assert on_shelf(data_dir) == before |
| 397 | assert len(moves(data_dir)) == moves_before |
| 398 | assert moves(data_dir, "sale") == [] |
| 399 | # The one purchase move is the shelf being stocked in the fixture; the |
| 400 | # drop-ship added none of its own. |
| 401 | assert len(moves(data_dir, "purchase")) == 1 |
| 402 | |
| 403 | # No receipt exists, so system_receipt_posting has nothing to react to |
| 404 | # and the PO stays where the action put it. |
| 405 | assert object_records.read_collection_records( |
| 406 | "receipts", base_dir=data_dir) == [] |
| 407 | po = orders_of(data_dir, "purchase")[0] |
| 408 | assert po["status"] == "confirmed" |
| 409 | |
| 410 | # And firing the receiving handler anyway -- the replay case -- finds |
| 411 | # no receipt and moves nothing. |
| 412 | replayed = run(objects_root, "system_receipt_posting", "EVENT", |
| 413 | {"event": "receipts.record.updated", |
| 414 | "collection": "receipts", "record_id": "no-such-receipt", |
| 415 | "action": "update"}) |
| 416 | assert replayed["ok"] is True |
| 417 | assert on_shelf(data_dir) == before |
| 418 | |
| 419 | |
| 420 | # --- margin is a read over the two orders -------------------------------------- |
| 421 | |
| 422 | def test_margin_reads_from_the_two_linked_orders(tmp_path, monkeypatch): |
| 423 | """The spec's claim was that margin needs no stored field because both |
| 424 | orders carry money. This is that claim, checked: revenue off the sale |
| 425 | order, cost off the purchase order, and object_billing.margin doing |
| 426 | the arithmetic rather than a second implementation of it.""" |
| 427 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 428 | dropship(objects_root, vendor_price_cents="700") |
| 429 | |
| 430 | read = margin(objects_root) |
| 431 | assert read["margin"] == {"revenue_minor": 3600, "cost_minor": 2100, |
| 432 | "gross_minor": 1500, "gross_pct": 41.67} |
| 433 | assert read["currency"] == "USD" |
| 434 | # Folded from the lines, because system_order_totals is a post-commit |
| 435 | # reaction that has not run in this test -- and a margin quoted off a |
| 436 | # total that simply has not landed yet would be a confident wrong |
| 437 | # answer at exactly the wrong moment. |
| 438 | assert read["revenue_source"] == "folded" |
| 439 | assert read["cost_source"] == "folded" |
| 440 | assert "36.00 USD" in read["body"] |
| 441 | assert "21.00 USD" in read["body"] |
| 442 | assert "15.00 USD" in read["body"] |
| 443 | |
| 444 | |
| 445 | def test_margin_opens_from_either_end_of_the_pair(tmp_path, monkeypatch): |
| 446 | """A link written both ways is a link you never have to search for.""" |
| 447 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 448 | created = dropship(objects_root, vendor_price_cents="700") |
| 449 | |
| 450 | from_sale = margin(objects_root, "ord-1") |
| 451 | from_po = margin(objects_root, created["purchase_order_id"]) |
| 452 | assert from_sale["margin"] == from_po["margin"] |
| 453 | assert from_po["sale_order_id"] == "ord-1" |
| 454 | |
| 455 | |
| 456 | def test_margin_prefers_the_stamped_total_when_the_handler_has_run( |
| 457 | tmp_path, monkeypatch): |
| 458 | """The document's own number wins when it exists: that is what a human |
| 459 | sees on the order itself, and a page that quietly disagreed with the |
| 460 | order it is about would be worse than no page.""" |
| 461 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 462 | created = dropship(objects_root, vendor_price_cents="700") |
| 463 | for order_id, total in (("ord-1", "4000"), |
| 464 | (created["purchase_order_id"], "2500")): |
| 465 | object_records.update_collection_record( |
| 466 | "orders", order_id, |
| 467 | {"subtotal_cents": total, "total_cents": total}, |
| 468 | base_dir=data_dir, actor="test") |
| 469 | |
| 470 | read = margin(objects_root) |
| 471 | assert read["revenue_source"] == "stamped" |
| 472 | assert read["margin"]["revenue_minor"] == 4000 |
| 473 | assert read["margin"]["cost_minor"] == 2500 |
| 474 | assert read["margin"]["gross_minor"] == 1500 |
| 475 | |
| 476 | |
| 477 | def test_a_missing_vendor_price_is_warned_about_rather_than_smoothed_over( |
| 478 | tmp_path, monkeypatch): |
| 479 | """A cost of zero reads as 100% margin and a cost copied from the sale |
| 480 | price reads as none. Both are lies; saying the number is missing is |
| 481 | not.""" |
| 482 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 483 | result = dropship(objects_root) |
| 484 | assert result["ok"] is True |
| 485 | assert result["unpriced_lines"] == ["line-1"] |
| 486 | assert "no vendor price" in result["warning"] |
| 487 | |
| 488 | read = margin(objects_root) |
| 489 | assert read["margin"]["cost_minor"] == 0 |
| 490 | assert read["margin"]["gross_pct"] == 100.0 |
| 491 | assert "reads as pure profit and is not" in read["body"] |
| 492 | |
| 493 | |
| 494 | def test_the_products_recorded_cost_is_used_before_giving_up(tmp_path, |
| 495 | monkeypatch): |
| 496 | """A price we already wrote down is a fact; zero is not.""" |
| 497 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch, |
| 498 | cost_cents=650) |
| 499 | result = dropship(objects_root) |
| 500 | assert result["ok"] is True |
| 501 | assert "warning" not in result |
| 502 | po_lines = lines_of(data_dir, result["purchase_order_id"]) |
| 503 | assert po_lines[0]["unit_price_cents"] == "650" |
| 504 | |
| 505 | |
| 506 | def test_margin_on_an_ordinary_order_says_what_is_missing_instead_of_zero( |
| 507 | tmp_path, monkeypatch): |
| 508 | """An order fulfilled from our own shelf has no vendor cost to set |
| 509 | against it -- that needs inventory valuation, which this server does |
| 510 | not do yet. Saying so beats printing a margin of 100%.""" |
| 511 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 512 | read = margin(objects_root) |
| 513 | assert read["status"] == 409 |
| 514 | assert "not part of a drop-ship pair" in read["body"] |
| 515 | |
| 516 | |
| 517 | def test_margin_is_not_a_number_for_visitors(tmp_path, monkeypatch): |
| 518 | """The page is addressed by an internal order id; public execute would |
| 519 | make it an enumeration oracle for the whole margin book.""" |
| 520 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 521 | dropship(objects_root, vendor_price_cents="700") |
| 522 | body = margin(objects_root, user_id="")["body"] |
| 523 | assert "Please sign in" in body |
| 524 | assert "36.00" not in body |
| 525 | |
| 526 | |
| 527 | # --- the halves that are NOT done, held as specifications ---------------------- |
| 528 | # |
| 529 | # Both were written first as strict xfails, because each needed one |
| 530 | # condition in a package the drop-ship slice did not own, and hacking |
| 531 | # around a sibling package's handler would have been exactly the kind of |
| 532 | # "make it work by the data" a stock rule cannot afford. Writing the |
| 533 | # acceptance test first and leaving it red is the honest move: a green |
| 534 | # test over a shelf that silently decrements is worse than a red one. |
| 535 | # |
| 536 | # Both conditions have since been added -- app-shipping's fulfilment |
| 537 | # handler and app-receiving's check-in action each now read the order's |
| 538 | # fulfillment_source -- so these are ordinary passing tests. The history |
| 539 | # is left here because it is the argument for why they exist at all. |
| 540 | |
| 541 | def shipped_by_the_vendor(data_dir, shipment_id="ship-ds"): |
| 542 | """The vendor's dispatch, recorded the way plan/fulfillment-logistics- |
| 543 | spec.md describes it: a shipment against the sale order. |
| 544 | |
| 545 | NOTE: the spec asks for `source: vendor` on that shipment, and |
| 546 | app-shipping's shipments schema has no `source` field -- a second, |
| 547 | smaller follow-up in the same package, named here so it is not |
| 548 | rediscovered. |
| 549 | """ |
| 550 | object_records.create_collection_record( |
| 551 | "shipments", |
| 552 | {"id": shipment_id, "order_id": "ord-1", "direction": "outbound", |
| 553 | "status": "open", "service": "ground", "carrier": "Vendor", |
| 554 | "ship_to_name": "Ada Lovelace", "owner_id": "shop"}, |
| 555 | base_dir=data_dir) |
| 556 | object_records.create_collection_record( |
| 557 | "shipment_lines", |
| 558 | {"id": f"{shipment_id}-line-0", "shipment_id": shipment_id, |
| 559 | "order_line_id": "line-1", "product_id": "p1", |
| 560 | "description": "Enamel Mug", "quantity": "3", "owner_id": "shop"}, |
| 561 | base_dir=data_dir) |
| 562 | for step in ("packed", "shipped"): |
| 563 | object_records.update_collection_record( |
| 564 | "shipments", shipment_id, {"status": step}, |
| 565 | base_dir=data_dir, actor="test") |
| 566 | return shipment_id |
| 567 | |
| 568 | |
| 569 | def test_the_vendors_dispatch_must_not_move_our_stock(tmp_path, monkeypatch): |
| 570 | """The specification for the missing condition, written as the |
| 571 | acceptance test whoever adds it already has.""" |
| 572 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 573 | dropship(objects_root, vendor_price_cents="700") |
| 574 | before = on_shelf(data_dir) |
| 575 | |
| 576 | shipment_id = shipped_by_the_vendor(data_dir) |
| 577 | result = run(objects_root, "system_order_fulfillment", "EVENT", |
| 578 | {"event": "shipments.record.updated", |
| 579 | "collection": "shipments", "record_id": shipment_id, |
| 580 | "action": "update"}) |
| 581 | |
| 582 | assert result["moved"] == 0 |
| 583 | assert moves(data_dir, "sale") == [] |
| 584 | assert on_shelf(data_dir) == before |
| 585 | |
| 586 | |
| 587 | def test_receiving_against_a_dropship_po_is_refused(tmp_path, monkeypatch): |
| 588 | """The second specification. Weaker than the first -- it takes a |
| 589 | deliberate human mistake to trigger, where the shipping gap fires on |
| 590 | its own -- but the same one-line fix in the same shape.""" |
| 591 | data_dir, objects_root = shop_that_can_dropship(tmp_path, monkeypatch) |
| 592 | created = dropship(objects_root, vendor_price_cents="700") |
| 593 | po_line = lines_of(data_dir, created["purchase_order_id"])[0] |
| 594 | before = on_shelf(data_dir) |
| 595 | |
| 596 | result = run(objects_root, "action_receive_goods", "POST", |
| 597 | {"order_id": created["purchase_order_id"], |
| 598 | "lines": [{"order_line_id": po_line["id"], |
| 599 | "quantity_received": "3"}]}) |
| 600 | |
| 601 | assert result.get("status") == 409 |
| 602 | assert "drop-ship" in result.get("error", "") |
| 603 | assert on_shelf(data_dir) == before |