test: 增加全链路多设备同步与药瓶生命周期自动化测试套件

This commit is contained in:
shen
2026-10-27 22:58:46 -07:00
parent 89ab13ecf5
commit ba32e29682
4 changed files with 712 additions and 2 deletions
+19 -2
View File
@@ -128,6 +128,19 @@ def init_db():
conn.execute("UPDATE dose_records SET flixotide_bottle_id = 'flix_b1_initial' WHERE flixotide_puffs > 0 AND flixotide_bottle_id IS NULL")
conn.execute("UPDATE dose_records SET ventolin_bottle_id = 'vent_b1_initial' WHERE ventolin_puffs > 0 AND ventolin_bottle_id IS NULL")
conn.execute("UPDATE dose_records SET updated_at = ? WHERE updated_at IS NULL", (now_iso,))
# 6. 若 canisters 为空,初始化基础配置
canister_count = conn.execute('SELECT COUNT(*) as c FROM canisters').fetchone()['c']
if canister_count == 0:
now_iso = datetime.datetime.now(datetime.timezone.utc).isoformat()
conn.execute('''
INSERT INTO canisters (id, name, total_doses, remaining_doses, opened_at, is_enabled, puffs_per_session, inhalation_seconds, is_steroid, sort_order, note, updated_at)
VALUES ('ventolin_primary', '万托林 (硫酸沙丁胺醇)', 200, 200, '2020-01-01T00:00:00Z', 1, 2, 15, 0, 1, '支气管舒张剂', ?)
''', (now_iso,))
conn.execute('''
INSERT INTO canisters (id, name, total_doses, remaining_doses, opened_at, is_enabled, puffs_per_session, inhalation_seconds, is_steroid, sort_order, note, updated_at)
VALUES ('flixotide_primary', '辅舒酮 (丙酸氟替卡松)', 60, 60, '2020-01-01T00:00:00Z', 1, 2, 15, 1, 2, '糖皮质激素', ?)
''', (now_iso,))
conn.close()
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
@@ -379,9 +392,13 @@ class SyncHandler(SimpleHTTPRequestHandler):
rem = max(0, td - consumed)
conn.execute('UPDATE bottles SET remaining_doses = ? WHERE id = ?', (rem, bid))
# 若是当前在用活跃瓶,同步更新 canisters 方案余量
# 若是当前在用活跃瓶,同步确保并更新 canisters 方案余量
if b['is_active']:
conn.execute('UPDATE canisters SET remaining_doses = ?, opened_at = ? WHERE id = ?', (rem, b['opened_at'], cid))
conn.execute('''
INSERT INTO canisters (id, name, total_doses, remaining_doses, opened_at, is_enabled, puffs_per_session, inhalation_seconds, is_steroid, sort_order, note, updated_at)
VALUES (?, ?, ?, ?, ?, 1, 2, 15, ?, 1, '', ?)
ON CONFLICT(id) DO UPDATE SET remaining_doses = excluded.remaining_doses, opened_at = excluded.opened_at
''', (cid, b['name'], td, rem, b['opened_at'], is_steroid, now_str))
# 6. 汇总全量数据响应客户端
all_records = []