Platform v2 step 1.13 (APPS_PLATFORM D-23).
- product_categories(product_id, category_id, sort_order); products.category_id becomes
products.primary_category_id (revision 0013 moves the data: one listing per product at its
old position). services/catalog.py keeps the rule "the primary is one of its categories"
and fixes up products when a category is deleted.
- Products API: category_ids + primary_category_id in and out, plus categories
[{category_id, sort_order}]; GET /api/products/?category_id= lists a category's products in
that category's order; PUT /reorder with category_id orders within a category. category_id
stays as dev scaffolding (section 3.3): output = primary, input = "move the primary".
- Primary category: ticket grouping and prep-zone category order, the order-item category
snapshot (1.12), report filters for waste-only rows and /meta/products.
- Every listing: price modifier / deal / min-quantity category targets, deal free choices,
the QR menu snapshot (product in each category it's listed in; images pushed once).
- Catalogue export v2: each category lists all its products with "primary" and
"category_sort_order"; import restores listings and order. v1 files are refused (D-21).
- New errors catalog.unknown_category, catalog.primary_not_listed.
67 lines
3.1 KiB
Python
67 lines
3.1 KiB
Python
"""Order items keep the product and category names they were sold under (plan step 1.12, D-18)."""
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def sold(db, make_user):
|
|
"""'Freddo' in 'Coffees', sold twice on one order, then renamed and moved to 'Drinks'."""
|
|
from models.order import Order, OrderItem
|
|
from models.product import Category, Product
|
|
waiter = make_user(role="waiter")
|
|
coffees, drinks = Category(name="Coffees"), Category(name="Drinks")
|
|
db.add_all([coffees, drinks])
|
|
db.flush()
|
|
prod = Product(name="Freddo", base_price=4.0, primary_category_id=coffees.id)
|
|
db.add(prod)
|
|
db.flush()
|
|
order = Order(opened_by=waiter.id, order_type="takeaway")
|
|
db.add(order)
|
|
db.flush()
|
|
items = [OrderItem(order_id=order.id, product_id=prod.id, added_by=waiter.id, quantity=1, unit_price=4.0)
|
|
for _ in range(2)]
|
|
db.add_all(items)
|
|
db.commit()
|
|
prod.name, prod.primary_category_id = "Freddo Espresso", drinks.id
|
|
coffees.name = "Hot & cold coffees"
|
|
db.commit()
|
|
return {"product": prod, "coffees": coffees, "drinks": drinks, "items": items, "order": order}
|
|
|
|
|
|
def test_items_snapshot_names_on_insert(db, sold):
|
|
it = sold["items"][0]
|
|
db.refresh(it)
|
|
assert (it.product_name, it.category_id, it.category_name) == ("Freddo", sold["coffees"].id, "Coffees")
|
|
|
|
|
|
def test_explicit_values_win(db, sold, make_user):
|
|
from models.order import OrderItem
|
|
it = OrderItem(order_id=sold["order"].id, product_id=sold["product"].id, added_by=make_user(role="waiter").id,
|
|
quantity=1, unit_price=4.0, product_name="Imported name", category_name=None)
|
|
db.add(it)
|
|
db.commit()
|
|
# an explicit product_name means the caller snapshots everything (e.g. an importer)
|
|
assert (it.product_name, it.category_id, it.category_name) == ("Imported name", None, None)
|
|
|
|
|
|
def test_reports_use_names_as_sold(client, sold, make_user, login): # "done when"
|
|
h = login(make_user(role="owner"))
|
|
products = client.get("/api/reports/products/performance", headers=h).json()["products"]
|
|
assert [(p["product_name"], p["category_id"], p["qty_sold"]) for p in products] == [("Freddo", sold["coffees"].id, 2)]
|
|
# filtering by category uses the category it was sold in
|
|
assert client.get("/api/reports/products/performance", headers=h,
|
|
params={"category_id": sold["drinks"].id}).json()["products"] == []
|
|
|
|
cats = client.get("/api/reports/categories/performance", headers=h).json()["categories"]
|
|
assert [(c["category_id"], c["category_name"], c["units_sold"]) for c in cats] == [(sold["coffees"].id, "Coffees", 2)]
|
|
|
|
hist = client.get("/api/reports/orders/history", headers=h).json()
|
|
names = {i["product"]["name"] for o in hist for i in o["items"]}
|
|
assert names == {"Freddo"}
|
|
|
|
|
|
def test_api_exposes_snapshot_and_live_name(client, sold, make_user, login):
|
|
h = login(make_user(role="owner"))
|
|
item = client.get(f"/api/orders/{sold['order'].id}", headers=h).json()["items"][0]
|
|
assert item["product_name"] == "Freddo" and item["category_name"] == "Coffees"
|
|
assert item["product"]["name"] == "Freddo Espresso" # `product` is the live catalogue
|