"""Allergens per product (plan step 6.3): the 14 EU allergens, "contains" or "may contain", set in the manager, carried to the counter, the phones, the menu tablet, the catalogue bundle and the customer apps' menu (cloud contract C-2).""" import re from pathlib import Path import pytest from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures) from tests.test_menu_tablet import menu, tablet # noqa: F401 (fixtures) ROOT = Path(__file__).resolve().parents[2] def test_set_change_and_clear(client, owner_h, menu): pid = menu["souvlaki"].id r = client.put(f"/api/products/{pid}", headers=owner_h, json={"allergens": {"milk": "may_contain", "gluten": "contains"}}) assert r.status_code == 200, r.text assert r.json()["allergens"] == {"gluten": "contains", "milk": "may_contain"} # the Annex's order # other changes leave them alone assert client.put(f"/api/products/{pid}", headers=owner_h, json={"base_price": 4}).json()["allergens"]["gluten"] == "contains" assert client.put(f"/api/products/{pid}", headers=owner_h, json={"allergens": {}}).json()["allergens"] == {} created = client.post("/api/products/", headers=owner_h, json={"name": "Salad", "base_price": 6, "allergens": {"celery": "contains"}}) assert created.status_code == 201 and created.json()["allergens"] == {"celery": "contains"} plain = client.post("/api/products/", headers=owner_h, json={"name": "Water", "base_price": 1}).json() assert plain["allergens"] == {} @pytest.mark.parametrize("bad", [{"pollen": "contains"}, {"milk": "a_little"}]) def test_unknown_allergens_or_levels_are_refused(client, owner_h, menu, bad): r = client.put(f"/api/products/{menu['beer'].id}", headers=owner_h, json={"allergens": bad}) assert r.status_code == 422 and err(r) == "catalog.allergen" def test_they_reach_the_counter_the_phones_and_the_tablet(client, owner_h, menu, tablet, make_user, login): pid = menu["steak"].id client.put(f"/api/products/{pid}", headers=owner_h, json={"allergens": {"mustard": "contains"}}) waiter_h = login(make_user(role="waiter"), client="waiter") phones = {p["id"]: p for p in client.get("/api/products/", headers=waiter_h).json()} assert phones[pid]["allergens"] == {"mustard": "contains"} data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json() assert data["products"][str(pid)]["allergens"] == {"mustard": "contains"} from routers.pos import _product_out assert _product_out(menu["steak"], 12.0)["allergens"] == {"mustard": "contains"} def test_catalogue_bundle_and_cloud_menu(client, db, owner_h, menu): pid = menu["beer"].id client.put(f"/api/products/{pid}", headers=owner_h, json={"allergens": {"gluten": "contains", "sulphites": "may_contain"}}) bundle = client.get("/api/data-transfer/export/catalog", headers=owner_h).json() beer = next(p for c in bundle["data"]["categories"] for p in c["products"] if p["name"] == "Beer") assert beer["allergens"] == {"gluten": "contains", "sulphites": "may_contain"} client.put(f"/api/products/{pid}", headers=owner_h, json={"allergens": {}}) assert client.post("/api/data-transfer/import/catalog", headers=owner_h, json=bundle).status_code < 300 assert client.get("/api/products/?all=true", headers=owner_h).json() db.expire_all() from models.product import Product from core.allergens import of assert of(db.get(Product, pid)) == {"gluten": "contains", "sulphites": "may_contain"} from services.cloud_sync import menu_snapshot snap, _ = menu_snapshot(db) products = {p["id"]: p for c in snap["categories"] for p in c["products"]} assert products[pid]["allergens"] == {"gluten": "contains", "sulphites": "may_contain"} def test_the_frontend_list_matches_the_backend(): """packages/access/src/allergens.js keeps the same 14, in the same order.""" from core.allergens import ALLERGENS, LEVELS js = (ROOT / "packages" / "access" / "src" / "allergens.js").read_text(encoding="utf-8") listed = re.search(r"export const ALLERGENS = \[([^\]]*)\]", js).group(1) assert [x.strip().strip("'") for x in listed.split(",") if x.strip()] == list(ALLERGENS) levels = re.search(r"export const ALLERGEN_LEVELS = \[([^\]]*)\]", js).group(1) assert [x.strip().strip("'") for x in levels.split(",") if x.strip()] == list(LEVELS)