"""The menu board's content (plan step 5.4b): any products (all, whole categories that stay live, single products), grouped or not, in the board's own order, with a sales point's live prices, sold-out products crossed out or hidden; menu changes reach the screens showing a menu board.""" import pytest from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) from tests.test_media import jpeg, media_dir, up # noqa: F401 (fixture + helpers) from tests.test_signage import playlist, slide @pytest.fixture def menu(db): from models.product import Category, Product, ProductCategory grill, drinks = Category(name="Grill", sort_order=0), Category(name="Drinks", sort_order=1) db.add_all([grill, drinks]) db.flush() pita = Category(name="Pita", sort_order=0, parent_id=grill.id) db.add(pita) db.flush() def prod(name, cat, price, order, **kw): p = Product(name=name, base_price=price, primary_category_id=cat.id, sort_order=order, **kw) db.add(p) db.flush() db.add(ProductCategory(product_id=p.id, category_id=cat.id, sort_order=order)) return p out = { "souvlaki": prod("Souvlaki", grill, 3.5, 0, description="Pork, on a stick"), "steak": prod("Steak", grill, 12.0, 1), "gyros": prod("Gyros pita", pita, 4.0, 0), "beer": prod("Beer", drinks, 4.5, 0), "wine": prod("Wine", drinks, 5.0, 1, sold_at='["online_delivery"]'), "old": prod("Old dish", grill, 9.0, 2, lifecycle_status="archived"), } db.commit() out["cats"] = {"grill": grill.id, "drinks": drinks.id, "pita": pita.id} return out @pytest.fixture def tv(client, owner_h, pair): token, r = pair("signage", "TV") p = playlist(client, owner_h, "Menu") client.put(f"/api/signage/screens/{r.json()['id']}", headers=owner_h, json={"playlist_id": p["id"]}) return {"h": {"X-Xenia-Device": token}, "id": r.json()["id"], "playlist": p["id"]} def board(client, owner_h, tv, **content): s = slide(client, owner_h, tv["playlist"], "menu", content=content) got = client.get("/api/signage/screen", headers=tv["h"]).json()["playlist"]["slides"] return s, next(x for x in got if x["id"] == s["id"])["menu"] def names(menu): return [[g["category"]["name"] if g["category"] else None, [i["name"] for i in g["items"]]] for g in menu["groups"]] def test_whole_categories_stay_live(client, owner_h, tv, menu, db): s, m = board(client, owner_h, tv, category_ids=[menu["cats"]["grill"]]) assert names(m) == [["Grill", ["Souvlaki", "Steak"]], ["Pita", ["Gyros pita"]]] # sub-categories too, never archived from models.product import Product, ProductCategory lamb = Product(name="Lamb chops", base_price=14.0, primary_category_id=menu["cats"]["grill"], sort_order=3) db.add(lamb) db.flush() db.add(ProductCategory(product_id=lamb.id, category_id=menu["cats"]["grill"], sort_order=3)) db.commit() m = next(x for x in client.get("/api/signage/screen", headers=tv["h"]).json()["playlist"]["slides"] if x["id"] == s["id"])["menu"] assert names(m)[0] == ["Grill", ["Souvlaki", "Steak", "Lamb chops"]] and m["count"] == 4 def test_any_products_and_exclusions(client, owner_h, tv, menu): _, m = board(client, owner_h, tv, product_ids=[menu["beer"].id, menu["souvlaki"].id]) assert names(m) == [["Grill", ["Souvlaki"]], ["Drinks", ["Beer"]]] _, m = board(client, owner_h, tv, select="all", exclude_ids=[menu["steak"].id]) # everything active at the shop's prices: Wine is sold only for delivery, Steak excluded, never archived assert sorted(i["name"] for g in m["groups"] for i in g["items"]) == ["Beer", "Gyros pita", "Souvlaki"] _, m = board(client, owner_h, tv, select="all", sales_point="online_delivery") assert "Wine" in [i["name"] for g in m["groups"] for i in g["items"]] def test_the_boards_own_order(client, owner_h, tv, menu): c = menu["cats"] _, m = board(client, owner_h, tv, category_ids=[c["grill"], c["drinks"]], category_order=[c["drinks"], c["pita"]], product_order=[menu["steak"].id]) assert names(m) == [["Drinks", ["Beer"]], ["Pita", ["Gyros pita"]], ["Grill", ["Steak", "Souvlaki"]]] _, m = board(client, owner_h, tv, category_ids=[c["grill"], c["drinks"]], group_by_category=False, product_order=[menu["beer"].id, menu["gyros"].id]) assert names(m) == [[None, ["Beer", "Gyros pita", "Souvlaki", "Steak"]]] def test_prices_sold_out_badges_photos(client, owner_h, tv, menu, db): photo = up(client, owner_h, jpeg(800, 600)).json() client.put(f"/api/products/{menu['souvlaki'].id}", headers=owner_h, json={"media_id": photo["id"]}) menu["steak"].is_available = False db.commit() content = dict(category_ids=[menu["cats"]["grill"]], show_photos=True, show_descriptions=True, badges={str(menu["souvlaki"].id): "chef"}) _, m = board(client, owner_h, tv, **content) souv, steak = m["groups"][0]["items"] assert (souv["price"], souv["badge"], souv["description"], souv["image"]) == (3.5, "chef", "Pork, on a stick", photo["urls"]["card"]) assert steak["available"] is False and steak["price"] == 12.0 and steak["image"] is None _, m = board(client, owner_h, tv, **content, sold_out="hide") assert [i["name"] for i in m["groups"][0]["items"]] == ["Souvlaki"] _, m = board(client, owner_h, tv, category_ids=[menu["cats"]["grill"]], sales_point=None) assert {i["price"] for g in m["groups"] for i in g["items"]} == {None} # a price change shows on the next read, at the price the counter charges client.put(f"/api/products/{menu['souvlaki'].id}", headers=owner_h, json={"base_price": 3.8}) _, m = board(client, owner_h, tv, category_ids=[menu["cats"]["grill"]]) assert m["groups"][0]["items"][0]["price"] == 3.8 def test_menu_changes_reach_the_screens_with_a_menu_board(client, owner_h, menu, pair, db, monkeypatch): import main import routers.signage as sg calls = [] monkeypatch.setattr(main, "_signage_menu_changed", lambda: calls.append(1)) client.put(f"/api/products/{menu['beer'].id}", headers=owner_h, json={"base_price": 4.9}) assert calls == [1] # every request that changes the menu client.get("/api/products/", headers=owner_h) assert calls == [1] # who hears it: only the screens whose playlist has a menu board t1, r1 = pair("signage", "Menu TV") t2, r2 = pair("signage", "Promo TV") with_menu, without = playlist(client, owner_h, "With menu"), playlist(client, owner_h, "Pictures") slide(client, owner_h, with_menu["id"], "menu", content={"select": "all"}) slide(client, owner_h, without["id"], "text", content={"title": "Hi"}) client.put(f"/api/signage/screens/{r1.json()['id']}", headers=owner_h, json={"playlist_id": with_menu["id"]}) client.put(f"/api/signage/screens/{r2.json()['id']}", headers=owner_h, json={"playlist_id": without["id"]}) sent = [] monkeypatch.setattr(sg, "broadcast_sync", lambda kind, data, **kw: sent.append((kind, kw.get("device_ids")))) sg.menu_changed(db) assert sent == [("signage.changed", [r1.json()["id"]])] def test_menu_settings_are_checked(client, owner_h, tv): url = f"/api/signage/playlists/{tv['playlist']}/slides" assert err(client.post(url, headers=owner_h, json={"kind": "menu", "content": {"sales_point": "moon"}})) == "signage.content_invalid" assert err(client.post(url, headers=owner_h, json={"kind": "menu", "content": {"badges": {"x": "new"}}})) == "signage.content_invalid" assert err(client.post(url, headers=owner_h, json={"kind": "menu", "content": {"badges": {"3": "shiny"}}})) == "signage.content_invalid" assert err(client.post(url, headers=owner_h, json={"kind": "menu", "content": {"columns": 4}})) == "signage.content_invalid" def test_the_editor_previews(client, owner_h, tv, menu): s, _ = board(client, owner_h, tv, category_ids=[menu["cats"]["drinks"]]) got = client.get(f"/api/signage/playlists/{tv['playlist']}", headers=owner_h).json() assert names(next(x for x in got["slides"] if x["id"] == s["id"])["menu"]) == [["Drinks", ["Beer"]]] # Wine: delivery only draft = client.post("/api/signage/preview/menu", headers=owner_h, json={"content": {"product_ids": [menu["steak"].id], "sales_point": None}}).json() assert names(draft["menu"]) == [["Grill", ["Steak"]]] and draft["content"]["columns"] == 2 assert err(client.post("/api/signage/preview/menu", headers=owner_h, json={"content": {"columns": 9}})) == "signage.content_invalid" client.put("/api/signage/message", headers=owner_h, json={"text": "Hello"}) assert client.get("/api/signage/venue", headers=owner_h).json() == {"name": None, "message": "Hello", "weather": None}