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_available_to_promise.py · 5.6 KB · 138 lines
| 1 | """The storefront says what can still be PROMISED, not what is on the shelf. |
| 2 | |
| 3 | Once online orders hold their goods from checkout (tests/test_online_ |
| 4 | reservations.py), "In stock" read from on hand alone invites a shopper to a |
| 5 | checkout that will refuse them: three mugs on the shelf, three unpaid |
| 6 | orders holding them. The product page, the variant picker and the basket |
| 7 | now say "Only N left" / "Sold out" from AVAILABLE (on hand less what live |
| 8 | online orders hold), and say nothing else about those orders -- a number |
| 9 | only when it is small (shop.low_stock_threshold, default 3), never who |
| 10 | holds what. Through the HTTP API (scripts/try_package.py's Sandbox). |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | from decimal import Decimal |
| 16 | |
| 17 | import pytest |
| 18 | |
| 19 | import object_records |
| 20 | import object_reservations |
| 21 | from scripts.try_package import Sandbox |
| 22 | |
| 23 | |
| 24 | @pytest.fixture() |
| 25 | def shop(): |
| 26 | with Sandbox(["app-shop", "app-payments", "app-settings"]) as box: |
| 27 | _put(box, "locations", id="shelf", name="Shelf", location_type="warehouse") |
| 28 | _product(box, "mug", on_hand=3) |
| 29 | yield box |
| 30 | |
| 31 | |
| 32 | def _put(box, collection, **row): |
| 33 | row.setdefault("owner_id", "shop") |
| 34 | return object_records.create_collection_record(collection, row, base_dir=box.data_dir) |
| 35 | |
| 36 | |
| 37 | def _product(box, product_id, *, on_hand, **fields): |
| 38 | fields.setdefault("name", product_id.title()) |
| 39 | _put(box, "products", id=product_id, price_cents="1000", product_type="physical", |
| 40 | is_active="true", **fields) |
| 41 | if on_hand: |
| 42 | _put(box, "stock_moves", product_id=product_id, to_location_id="shelf", |
| 43 | quantity=str(on_hand), reason="purchase", unit_cost_cents="400") |
| 44 | |
| 45 | |
| 46 | def _buy(box, product_id="mug", quantity="1", email="holder@example.test"): |
| 47 | status, cart = box.call("anon", "POST", "/objects/action_cart", |
| 48 | {"action": "add", "product_id": product_id, |
| 49 | "quantity": quantity}) |
| 50 | status, body = box.call("anon", "POST", "/objects/action_checkout", |
| 51 | {"session_token": cart["session_token"], |
| 52 | "customer_email": email}) |
| 53 | assert status == 200, body |
| 54 | return body |
| 55 | |
| 56 | |
| 57 | def _stock_line(html): |
| 58 | """The one availability line the product page draws.""" |
| 59 | import re |
| 60 | found = re.search(r'<p class="stock">([^<]*)</p>', html) |
| 61 | return found.group(1) if found else "" |
| 62 | |
| 63 | |
| 64 | def _page(box, product_id="mug"): |
| 65 | status, html = box.call("anon", "GET", f"/objects/site_shop?product_id={product_id}") |
| 66 | assert status == 200 |
| 67 | return html |
| 68 | |
| 69 | |
| 70 | def test_the_product_page_counts_what_other_orders_hold(shop): |
| 71 | """Three on the shelf. On hand alone said "In stock" all the way to the |
| 72 | third unpaid order -- and checkout then refused the fourth shopper.""" |
| 73 | assert "Only 3 left" in _page(shop) |
| 74 | _buy(shop) |
| 75 | assert "Only 2 left" in _page(shop) |
| 76 | _buy(shop, quantity="2") |
| 77 | assert _stock_line(_page(shop)) == "Sold out" |
| 78 | |
| 79 | |
| 80 | def test_plenty_says_in_stock_and_no_number(shop): |
| 81 | _product(shop, "lamp", on_hand=40) |
| 82 | _buy(shop, "lamp", quantity="5") |
| 83 | assert _stock_line(_page(shop, "lamp")) == "In stock" |
| 84 | |
| 85 | |
| 86 | def test_the_page_says_nothing_about_whose_orders_hold_it(shop): |
| 87 | body = _buy(shop, email="private.person@example.test") |
| 88 | page = _page(shop) |
| 89 | assert "private.person" not in page |
| 90 | assert body["order_id"][:8].upper() not in page |
| 91 | assert body["order_id"] not in page |
| 92 | |
| 93 | |
| 94 | def test_the_threshold_is_a_setting(shop): |
| 95 | status, _ = shop.call("admin", "POST", "/collections/app_settings/records", |
| 96 | {"key": "shop.low_stock_threshold", "value": "0"}) |
| 97 | assert status == 201 |
| 98 | assert _stock_line(_page(shop)) == "In stock" |
| 99 | |
| 100 | |
| 101 | def test_the_basket_says_only_n_left_and_sold_out(shop): |
| 102 | status, cart = shop.call("anon", "POST", "/objects/action_cart", |
| 103 | {"action": "add", "product_id": "mug"}) |
| 104 | line = cart["lines"][0] |
| 105 | assert line["stock"] == {"state": "low", "left": 3} |
| 106 | _buy(shop, quantity="3") |
| 107 | status, cart = shop.call("anon", "POST", "/objects/action_cart", |
| 108 | {"action": "get", "session_token": cart["session_token"]}) |
| 109 | assert cart["lines"][0]["stock"] == {"state": "sold_out", "left": 0} |
| 110 | |
| 111 | status, html = shop.call("anon", "POST", "/objects/site_shop", |
| 112 | {"do": "set", "product_id": "mug", "quantity": "1", |
| 113 | "cart_item_id": line["cart_item_id"], |
| 114 | "session_token": cart["session_token"]}) |
| 115 | assert "Sold out" in html |
| 116 | |
| 117 | |
| 118 | def test_a_variant_held_by_other_orders_is_shown_sold_out(shop): |
| 119 | _put(shop, "products", id="tote", name="Tote", price_cents="0", |
| 120 | product_type="physical", is_active="true") |
| 121 | _product(shop, "tote-m", on_hand=1, parent_product_id="tote", |
| 122 | options='{"size": "M"}', name="Tote M") |
| 123 | _product(shop, "tote-l", on_hand=2, parent_product_id="tote", |
| 124 | options='{"size": "L"}', name="Tote L") |
| 125 | _buy(shop, "tote-m") |
| 126 | page = _page(shop, "tote") |
| 127 | assert 'value="tote-m" disabled' in page and "Sold out" in page |
| 128 | assert 'value="tote-l" required' in page and "Only 2 left" in page |
| 129 | |
| 130 | |
| 131 | def test_what_may_be_promised_is_pure_and_shows_a_number_only_when_small(): |
| 132 | promise = object_reservations.promise |
| 133 | assert promise(Decimal("0")) == {"state": "sold_out", "left": 0} |
| 134 | assert promise(Decimal("-2")) == {"state": "sold_out", "left": 0} |
| 135 | assert promise(Decimal("2")) == {"state": "low", "left": 2} |
| 136 | assert promise(Decimal("4")) == {"state": "in_stock", "left": None} |
| 137 | assert promise(Decimal("4"), low=0) == {"state": "in_stock", "left": None} |
| 138 | assert promise(Decimal("1.5"), low=3) == {"state": "low", "left": Decimal("1.5")} |