Files
wtc.waiswuaschd.de/scripts/test_walking_path_and_presets.py

96 lines
4.3 KiB
Python

import sys
import os
# Set UTF-8 encoding for stdout
if sys.stdout.encoding.lower() != 'utf-8':
sys.stdout.reconfigure(encoding='utf-8')
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.database import (
get_shopping_categories,
reset_default_shopping_categories,
guess_category_for_ingredient,
add_shopping_category,
update_shopping_category,
delete_shopping_category,
add_shopping_item,
update_shopping_item_category,
get_ingredient_category_mapping,
clear_shopping_list
)
def run_tests():
print("--- 1. Testing Standard Walking Path Categories ---")
reset_default_shopping_categories(preset="standard")
cats = get_shopping_categories()
assert len(cats) == 9, f"Expected 9 categories, got {len(cats)}"
# Check natural walking order: Produce (1) -> Bakery (2) -> Pantry (3) -> Spices (4) -> Dairy (5) -> Meat (6) -> Frozen (7) -> Drinks (8) -> Drugstore (9)
expected_order = [
("Obst & Gemüse", "produce", 1),
("Brot & Backwaren", "bakery", 2),
("Vorrat & Nudeln", "pantry", 3),
("Gewürze & Öle", "spices", 4),
("Kühlregal & Käse", "dairy", 5),
("Fleisch & Fisch", "meat", 6),
("Tiefkühl", "frozen", 7),
("Getränke", "drinks", 8),
("Drogerie & Sonstiges", "drugstore", 9)
]
for idx, (exp_name, exp_type, exp_order) in enumerate(expected_order):
cat = cats[idx]
assert cat["name"] == exp_name, f"Cat {idx} name mismatch: {cat['name']} != {exp_name}"
assert cat["shelf_type"] == exp_type, f"Cat {idx} shelf_type mismatch: {cat['shelf_type']} != {exp_type}"
assert cat["sort_order"] == exp_order, f"Cat {idx} sort_order mismatch: {cat['sort_order']} != {exp_order}"
print("[OK] Standard walking path matches physical supermarket logic!")
print("--- 2. Testing Discounter Preset ---")
reset_default_shopping_categories(preset="discounter")
disc_cats = get_shopping_categories()
assert len(disc_cats) == 9
assert disc_cats[0]["shelf_type"] == "produce"
assert disc_cats[1]["shelf_type"] == "bakery"
assert disc_cats[2]["shelf_type"] == "dairy" # In discounters, cold wall is often early along the outer wall
assert disc_cats[3]["shelf_type"] == "meat"
print("[OK] Discounter preset applied successfully!")
print("--- 3. Testing Semantic Matching via shelf_type (Custom Category Renaming) ---")
# Reset back to standard
reset_default_shopping_categories(preset="standard")
cats = get_shopping_categories()
pantry_cat = next(c for c in cats if c["shelf_type"] == "pantry")
# User customizes the name: "Gang 3: Nudeln, Reis & Saucen"
update_shopping_category(pantry_cat["id"], "Gang 3: Nudeln, Reis & Saucen", pantry_cat["icon"], pantry_cat["shelf_type"], pantry_cat["sort_order"])
# Guess for spaghetti
guessed = guess_category_for_ingredient("Spaghetti")
assert guessed == "Gang 3: Nudeln, Reis & Saucen", f"Expected matching via shelf_type, got {guessed}"
print(f"[OK] Renamed category was resolved via shelf_type: 'Spaghetti' -> '{guessed}'")
print("--- 4. Testing 1-Tap Shelf Reassignment & Learned Memory ---")
clear_shopping_list()
# Ensure fresh test state by clearing mapping for test ingredient using execute_query
from app.database import execute_query
execute_query("DELETE FROM ingredient_category_mappings WHERE LOWER(ingredient_name) = LOWER(?)", ('frische petersilie',), commit=True)
item = add_shopping_item("Frische Petersilie", "1", "Bund")
assert item["category"] == "Obst & Gemüse"
# User taps 1-tap shelf selector to move it to "Kühlregal & Käse" (or custom shelf)
updated = update_shopping_item_category(item["id"], "Kühlregal & Käse")
assert updated["category"] == "Kühlregal & Käse"
# Check that system memorized it
learned = get_ingredient_category_mapping("Frische Petersilie")
assert learned == "Kühlregal & Käse", f"Expected learned mapping 'Kühlregal & Käse', got '{learned}'"
print("[OK] 1-Tap shelf reassignment immediately updated item and saved learned memory!")
# Reset back to clean standard
reset_default_shopping_categories(preset="standard")
clear_shopping_list()
print("ALL WALKING PATH & PRESET TESTS PASSED SUCCESSFULLY!")
if __name__ == "__main__":
run_tests()