Files

119 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""回归:待办卡缩略图开关 + 缩略图内存缓存(v1.0.48)
验证:
① 默认(ui.todo_thumb 缺省)= 不显示缩略图,不碰文件探测
② ui.todo_thumb=true → 有照片的商品卡带图,且第二次建卡不重复解码
③ 关掉时卡片仍是 3 个控件(品名/规格·客户/数量),布局不变形
④ 开关切换后重建不报错、卡片数正确
不需要秤/摄像头/网络。
"""
import os
import sys
import json
sys.path.insert(0, os.getcwd())
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.getcwd())
import tkinter as tk
import ui.app as A
ok = fail = 0
def check(name, cond, extra=''):
global ok, fail
if cond:
ok += 1
print(' [OK] %s %s' % (name, extra))
else:
fail += 1
print(' [FAIL] %s %s' % (name, extra))
cfg = json.load(open('config.json', encoding='utf-8'))
cfg.setdefault('server', {})['mock'] = True
cfg.setdefault('scale', {})['simulate'] = True
cfg.setdefault('camera', {})['simulate'] = True
cfg.setdefault('ui', {})['mode'] = 'window'
cfg['ui']['todo_group'] = 'none'
cfg['ui'].pop('todo_thumb', None)
A.load_config = lambda: cfg
root = tk.Tk()
root.geometry('1600x900')
app = A.App(root)
app._build()
app.ready = True
root.update()
rows = [{'id': 'r%d' % i,
'item_name': ('旱黄瓜' if i == 0 else '商品 %d' % i),
'spec': '500g', 'unit': '斤', 'required_qty': 3.0,
'customer_name': '客户%d' % (i % 5), 'sku_code': 'S%d' % i}
for i in range(12)]
print('=== 1. 缺省 = 不显示缩略图 ===')
check('_thumb_on() 默认 False', app._thumb_on() is False)
app._fill_todo([dict(r) for r in rows])
for _ in range(30):
root.update()
n_thumb = sum(1 for c in app.todo_cards.values()
if getattr(c, '_thumb_label', None) is not None)
n_child = sum(len(c.winfo_children()) for c in app.todo_cards.values())
print(' 卡片 %d 张|带图 %d 张|控件合计 %d' % (len(app.todo_cards), n_thumb, n_child))
check('没有一张卡带图', n_thumb == 0)
check('每卡 3 个控件', n_child == 3 * len(app.todo_cards),
'实际 %d / 期望 %d' % (n_child, 3 * len(app.todo_cards)))
def rebuild():
app._shown_ids = None
for c in list(app.todo_cards.values()):
try:
c.destroy()
except Exception:
pass
app.todo_cards = {}
app.todo_rows = {}
for _ in range(4):
root.update()
app._fill_todo([dict(r) for r in rows])
for _ in range(30):
root.update()
print('=== 2. 打开开关 → 有照片的卡带图 ===')
app.cfg['ui']['todo_thumb'] = True
check('_thumb_on() 变 True', app._thumb_on() is True)
rebuild()
n_thumb = sum(1 for c in app.todo_cards.values()
if getattr(c, '_thumb_label', None) is not None)
print(' 带图卡 =', n_thumb)
check('旱黄瓜那张有图', n_thumb >= 1)
check('缓存里有条目', len(getattr(app, '_thumb_mem', {}) or {}) >= 1,
'缓存 %d 条' % len(getattr(app, '_thumb_mem', {}) or {}))
print('=== 3. 第二次重建不重复解码(命中内存缓存)===')
cache_id = id(app._thumb_mem)
before = dict(app._thumb_mem)
rebuild()
check('缓存对象没被重建', id(app._thumb_mem) == cache_id)
check('缓存条目数不变', len(app._thumb_mem) == len(before),
'%d → %d' % (len(before), len(app._thumb_mem)))
print('=== 4. 关回去:图消失、卡片不报错 ===')
app.cfg['ui']['todo_thumb'] = False
rebuild()
n_thumb = sum(1 for c in app.todo_cards.values()
if getattr(c, '_thumb_label', None) is not None)
check('图全部消失', n_thumb == 0, '剩下 %d' % n_thumb)
check('卡片数正确', len(app.todo_cards) == 12, len(app.todo_cards))
root.destroy()
print()
print('通过 %d 项,失败 %d 项' % (ok, fail))
sys.exit(1 if fail else 0)