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_in_store_only.py · 7.5 KB · 162 lines
| 1 | """A product can be sold in the shop only -- never basketed or reserved online. |
| 2 | |
| 3 | "otherwise the inventory should have a flag to not allow pick reservations, |
| 4 | because sometimes you'd have a special only upon entry and they have a line |
| 5 | inside the store or outside, or even had people a number ticket so they |
| 6 | don't have line jumpers" -- Dan, 2026-11-30. |
| 7 | |
| 8 | products.in_store_only refuses the online basket, checkout and any |
| 9 | reserving order line (the locked check), with one sentence |
| 10 | (object_reservations.online_refusal); products.show_online decides whether |
| 11 | the shop page still lists it; the counter (a back-office order) still sells |
| 12 | it. products.online_max_per_order caps what one online order may take. |
| 13 | Everything goes through the HTTP API (scripts/try_package.py's Sandbox). |
| 14 | """ |
| 15 | |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | import json |
| 19 | from pathlib import Path |
| 20 | |
| 21 | import pytest |
| 22 | |
| 23 | import object_records |
| 24 | from scripts.try_package import Sandbox |
| 25 | |
| 26 | ROOT = Path(__file__).resolve().parents[1] |
| 27 | |
| 28 | |
| 29 | @pytest.fixture() |
| 30 | def shop(): |
| 31 | with Sandbox(["app-shop", "app-pickup", "app-payments", "app-settings"]) as box: |
| 32 | _put(box, "locations", id="shelf", name="Shelf", location_type="warehouse") |
| 33 | _product(box, "doughnut", on_hand=12, in_store_only="true", name="Doughnut special") |
| 34 | _product(box, "mug", on_hand=5) |
| 35 | yield box |
| 36 | |
| 37 | |
| 38 | def _put(box, collection, **row): |
| 39 | row.setdefault("owner_id", "shop") |
| 40 | return object_records.create_collection_record(collection, row, base_dir=box.data_dir) |
| 41 | |
| 42 | |
| 43 | def _product(box, product_id, *, on_hand, **fields): |
| 44 | fields.setdefault("name", product_id.title()) |
| 45 | _put(box, "products", id=product_id, price_cents="300", product_type="physical", |
| 46 | is_active="true", **fields) |
| 47 | _put(box, "stock_moves", product_id=product_id, to_location_id="shelf", |
| 48 | quantity=str(on_hand), reason="purchase", unit_cost_cents="100") |
| 49 | |
| 50 | |
| 51 | def _add(box, product_id, quantity="1", token=""): |
| 52 | body = {"action": "add", "product_id": product_id, "quantity": quantity} |
| 53 | if token: |
| 54 | body["session_token"] = token |
| 55 | return box.call("anon", "POST", "/objects/action_cart", body) |
| 56 | |
| 57 | |
| 58 | def test_an_in_store_only_product_cannot_go_in_an_online_basket(shop): |
| 59 | status, body = _add(shop, "doughnut") |
| 60 | assert status == 409, body |
| 61 | assert body["code"] == "in_store_only" |
| 62 | assert "sold in the shop only" in body["error"] |
| 63 | assert "cannot be ordered or reserved online" in body["error"] |
| 64 | assert not [i for i in object_records.read_collection_records( |
| 65 | "cart_items", base_dir=shop.data_dir) if i["product_id"] == "doughnut"] |
| 66 | |
| 67 | |
| 68 | def test_a_basket_filled_before_the_flag_cannot_check_it_out(shop): |
| 69 | """The flag set after somebody put it in a basket: checkout refuses it, |
| 70 | with the same sentence, and nothing is ordered.""" |
| 71 | object_records.update_collection_record( |
| 72 | "products", "doughnut", {"in_store_only": "false"}, base_dir=shop.data_dir) |
| 73 | status, cart = _add(shop, "doughnut") |
| 74 | assert status == 200 |
| 75 | object_records.update_collection_record( |
| 76 | "products", "doughnut", {"in_store_only": "true"}, base_dir=shop.data_dir) |
| 77 | status, body = shop.call("anon", "POST", "/objects/action_checkout", |
| 78 | {"session_token": cart["session_token"], |
| 79 | "customer_email": "b@example.test"}) |
| 80 | assert status == 409, body |
| 81 | assert "sold in the shop only" in body["inactive"][0]["reason"] |
| 82 | assert object_records.read_collection_records("orders", base_dir=shop.data_dir) == [] |
| 83 | |
| 84 | |
| 85 | def test_no_reserving_order_line_can_hold_it_whoever_writes_it(shop): |
| 86 | """The locked check: a line for it on an online order is refused for |
| 87 | any writer, an admin over the API included.""" |
| 88 | status, cart = _add(shop, "mug") |
| 89 | status, order = shop.call("anon", "POST", "/objects/action_checkout", |
| 90 | {"session_token": cart["session_token"], |
| 91 | "customer_email": "b@example.test"}) |
| 92 | assert status == 200, order |
| 93 | status, body = shop.call("admin", "POST", "/collections/order_lines/records", |
| 94 | {"order_id": order["order_id"], "product_id": "doughnut", |
| 95 | "description": "Doughnut", "quantity": "1", |
| 96 | "unit_price_cents": "300", "owner_id": "shop"}) |
| 97 | assert status == 409, body |
| 98 | assert body["check"] == "stock_hold" and "sold in the shop only" in body["error"] |
| 99 | |
| 100 | |
| 101 | def test_the_counter_still_sells_it(shop): |
| 102 | """A back-office order does not reserve, so the flag does not touch it.""" |
| 103 | status, order = shop.call("admin", "POST", "/collections/orders/records", |
| 104 | {"doc_type": "sale", "number": "TILL-1", |
| 105 | "customer_name": "Walk-in", "owner_id": "shop"}) |
| 106 | assert status == 201, order |
| 107 | status, line = shop.call("admin", "POST", "/collections/order_lines/records", |
| 108 | {"order_id": order["record"]["id"], "product_id": "doughnut", |
| 109 | "description": "Doughnut", "quantity": "2", |
| 110 | "unit_price_cents": "300", "owner_id": "shop"}) |
| 111 | assert status == 201, line |
| 112 | |
| 113 | |
| 114 | def test_the_shop_page_shows_it_as_in_store_only_unless_told_not_to(shop): |
| 115 | status, page = shop.call("anon", "GET", "/objects/site_shop") |
| 116 | assert status == 200 |
| 117 | assert "Doughnut special" in page and "In store only" in page |
| 118 | status, detail = shop.call("anon", "GET", "/objects/site_shop?product_id=doughnut") |
| 119 | assert status == 200 and "In store only" in detail |
| 120 | assert 'value="doughnut"' not in detail # no Add form for it |
| 121 | |
| 122 | object_records.update_collection_record( |
| 123 | "products", "doughnut", {"show_online": "false"}, base_dir=shop.data_dir) |
| 124 | status, page = shop.call("anon", "GET", "/objects/site_shop") |
| 125 | assert "Doughnut special" not in page |
| 126 | status, _ = shop.call("anon", "GET", "/objects/site_shop?product_id=doughnut") |
| 127 | assert status == 404 |
| 128 | |
| 129 | |
| 130 | def test_show_online_off_does_not_hide_a_product_that_sells_online(shop): |
| 131 | object_records.update_collection_record( |
| 132 | "products", "mug", {"show_online": "false"}, base_dir=shop.data_dir) |
| 133 | status, page = shop.call("anon", "GET", "/objects/site_shop") |
| 134 | assert "Mug" in page |
| 135 | |
| 136 | |
| 137 | def test_an_online_order_takes_at_most_the_per_order_limit(shop): |
| 138 | object_records.update_collection_record( |
| 139 | "products", "mug", {"online_max_per_order": "2"}, base_dir=shop.data_dir) |
| 140 | status, cart = _add(shop, "mug", "2") |
| 141 | assert status == 200 |
| 142 | status, body = _add(shop, "mug", "1", token=cart["session_token"]) |
| 143 | assert status == 409 and body["code"] == "online_limit" |
| 144 | assert "limited to 2 per online order" in body["error"] |
| 145 | # A second line of the same product counts toward the same limit. |
| 146 | status, body = shop.call("anon", "POST", "/objects/action_cart", |
| 147 | {"action": "add", "product_id": "mug", "quantity": "1", |
| 148 | "line_note": "gift wrap", |
| 149 | "session_token": cart["session_token"]}) |
| 150 | assert status == 409 |
| 151 | status, order = shop.call("anon", "POST", "/objects/action_checkout", |
| 152 | {"session_token": cart["session_token"], |
| 153 | "customer_email": "b@example.test"}) |
| 154 | assert status == 200, order |
| 155 | |
| 156 | |
| 157 | def test_the_flags_are_declared_on_products(): |
| 158 | schema = json.loads((ROOT / "packages/app-catalog/schemas/products.json").read_text()) |
| 159 | fields = {f["name"]: f for f in schema["fields"]} |
| 160 | assert fields["in_store_only"]["default"] == "false" |
| 161 | assert fields["show_online"]["default"] == "true" |
| 162 | assert fields["online_max_per_order"]["type"] == "integer" |