Dish_Recognition/tools/test_mgmt_dishes.py

50 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""快速验证菜品公开接口(GET dishes / PATCH learning-status)。"""
import argparse
import json
import sys
from pathlib import Path
_ROOT = Path(__file__).resolve().parents[1]
if str(_ROOT) not in sys.path:
sys.path.insert(0, str(_ROOT))
def main():
parser = argparse.ArgumentParser(description='测试菜品信息接口')
parser.add_argument('--name', default='', help='菜名(GET 模糊 / PATCH 精确)')
parser.add_argument('--category', default='', help='分类筛选')
parser.add_argument('--status', default='', help='若传入则执行 PATCH,如 已学习')
args = parser.parse_args()
from service.mgmt_client import MgmtApiError, fetch_dishes, patch_dish_learning_status
try:
data = fetch_dishes(
name=args.name or None,
category=args.category or None,
)
items = data.get('items') if isinstance(data, dict) else data
total = data.get('total', len(items or [])) if isinstance(data, dict) else len(items or [])
print(f'[OK] dishes total={total}')
for row in (items or [])[:5]:
print(' ', json.dumps(row, ensure_ascii=False))
if items and len(items) > 5:
print(f' ... 其余 {len(items) - 5} 条')
if args.status:
name = (args.name or '').strip()
if not name:
print('[ERR] PATCH 需要 --name 精确菜名')
sys.exit(1)
resp = patch_dish_learning_status(name, args.status)
print('[OK] learning-status updated:')
print(json.dumps(resp, ensure_ascii=False, indent=2))
except MgmtApiError as exc:
print(f'[ERR] {exc}')
sys.exit(1)
if __name__ == '__main__':
main()