Files
jinzuanwork/backend/backend-site/tests/数据库测试.py
T
ywsandClaude Opus 5.5 09b229deee portal: 金钻宇宙换成星系图(three.js 懒加载)+ 推送前审查 + 总交接文档
- 门户「金钻宇宙」:删掉启动仪式 / 宇宙首屏晶体 / 盘符机架 / 读盘舱及其 CSS、图标;
  换成星系图(portal/assets/universe/,three.js r170 自托管于 assets/vendor/,第一次打开板块才下载)
  + 窗下世界线卡片;#0001 深链飞到对应世界线;世界线数据只留 config.js 一份
- 像素字子集重做(旧字全保留 + 新字);其余板块与改动前逐像素一致
- 后端审查:add_call 写库失败不再打断渲染机回传;多进程同时首启只导入一次(+1 条测试,共 38);
  撤销「处理过了」清掉 handled_at
- 交接文档-2026-10.md:变更清单 / 功能增删 / 接口与数据格式变更 / 设计规范 / 业务逻辑 / 部署运维
- 交付清单哈希重算;README、旧交接说明指路

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 17:31:36 +00:00

299 lines
16 KiB
Python
Raw 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 -*-
"""数据库(store.py / ys.db)测试 —— 数据全是这里现编的假数据,不碰任何真实环境。
用法:python3 tests/数据库测试.py <server.py 所在目录>
(和 回执复现测试.py 一样:交付副本里三个 URL 格式串被脱敏弄坏了,第 9 节要起服务器,跑之前先换成能格式化的占位)
测的是:
1. 全新安装:空目录第一次启动 → 自动建库,什么都不用配
2. 老数据迁移:现编一套旧版 JSON(卡密 / 订单 meta / 爱发电账本 / 呼叫记录(含写坏的半行)/ 小块状态)→ 一次导全
3. 重启不重复导入;旧文件原样不动
4. 卡密整份写回 = 只改有变化的行;能查的列(state / order_id)跟着变
5. 订单清理:行留着记账,但对外当它不存在;单号不会被重新发出去
6. 事务:中途出错整体回滚,不留半截
7. 导出旧版 JSON → 再导入新库,数据一模一样(回退路径能用)
8. 热备份能打开、行数一致;完整性检查 ok
9. 服务器直接在「迁移过来的老数据」上跑:老订单能查、老卡密能认、admin.py db / calls / ls 能用
10. 并发:8 个线程同时记呼叫、两个进程同时写卡密,一条不丢;三个进程同时第一次启动只导一次
11. 规模:5000 张码 + 2000 单,按状态列订单的耗时
"""
import json, os, random, shutil, sqlite3, subprocess, sys, tempfile, threading, time
SRC = os.path.abspath(sys.argv[1])
sys.path.insert(0, SRC)
import store # noqa: E402
RESULTS = []
def check(name, cond, detail=""):
RESULTS.append((name, bool(cond), detail))
print(("PASS " if cond else "FAIL ") + name + ((" —— " + detail) if detail else ""), flush=True)
def jw(p, obj):
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, "w", encoding="utf-8") as f:
json.dump(obj, f, ensure_ascii=False, indent=1)
ALPH = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"
rnd = random.Random(20261005)
def fake_code():
return "YS-" + "-".join("".join(rnd.choice(ALPH) for _ in range(4)) for _ in range(3))
def fake_oid():
return "YS" + "".join(rnd.choice(ALPH) for _ in range(6))
def make_legacy(root, n_codes=40, n_orders=12):
"""现编一套「旧版」数据目录(形状照 server.py 写出来的样子)。"""
codes, orders = {}, {}
states = ["waiting", "working", "done", "failed", "unpaid", "paid"]
for i in range(n_orders):
oid = fake_oid()
orders[oid] = {"oid": oid, "state": states[i % len(states)], "token": "".join(rnd.choice(ALPH) for _ in range(14)),
"created": "2026-10-0%dT1%d:00:00+08:00" % (1 + i % 4, i % 10), "arm": "classic",
"templates": ["stand"], "res": 1000, "skins": [{"id": "s1", "path": "skin.png", "templates": ["stand"]}],
"codes": [], "files": [], "note": "假单 %d" % i, "expire": "2026-10-09T00:00:00+08:00"}
oids = list(orders)
for i in range(n_codes):
c = fake_code()
st = ["active", "inactive", "bound", "used"][i % 4]
rec = {"used": st == "used", "order": None, "ts": None, "batch": "测试批%d" % (i % 3), "value": 6,
"platform": ["", "闲鱼", "爱发电"][i % 3], "tpl": "", "state": st, "set": False, "direct": False,
"n_fix": 1, "n_max": 0, "lock_tpl": False, "n_free": False}
if st in ("bound", "used"):
o = oids[i % len(oids)]
rec["order"] = o
orders[o]["codes"].append(c)
codes[c] = rec
codes["YS-OLD0-OLD0-OLD0"] = {"used": True, "order": None, "batch": "最早一批"} # 老格式:只有 used 布尔
jw(os.path.join(root, "codes.json"), codes)
for oid, m in orders.items():
jw(os.path.join(root, "orders", oid, "meta.json"), m)
with open(os.path.join(root, "orders", oid, "skin.png"), "wb") as f:
f.write(b"\x89PNG\r\n\x1a\nfake-skin")
jw(os.path.join(root, "payplatform-orders.json"),
{"2026100500000000000000000%02d" % i: {"ts": "2026-10-05T12:00:00+08:00", "code": fake_code(), "n_img": 1,
"paid": "6.00", "notified": False} for i in range(5)})
with open(os.path.join(root, "calls.jsonl"), "w", encoding="utf-8") as f:
for i in range(6):
f.write(json.dumps({"ts": "2026-10-05T1%d:00:00+08:00" % i, "ip": "10.0.0.%d" % i, "order": oids[i],
"note": "等太久了 %d" % i, "notified": i % 2 == 0}, ensure_ascii=False) + "\n")
f.write('{"ts": "2026-10-05T19:00:00+08:00", "ip": "10.0.0.9", "no') # 断电写了半行
jw(os.path.join(root, "gifts.json"), {"batches": [{"id": "g1", "name": "国庆福利", "n_fix": 1, "enabled": True}]})
jw(os.path.join(root, "heartbeat.json"), {"ts": "2026-10-05T12:00:00+08:00", "host": "render"})
jw(os.path.join(root, "home_ips.json"), {"ips": ["198.51.100.7"]})
jw(os.path.join(root, "tpl_times.json"), {"stand": [40.0, 42.5, 39.1]})
return codes, orders
def fingerprint(root):
out = {}
for dp, _, fs in os.walk(root):
for f in fs:
p = os.path.join(dp, f)
out[os.path.relpath(p, root)] = open(p, "rb").read()
return out
# ================================================================== 1. 全新安装
R1 = tempfile.mkdtemp(prefix="ys-db-fresh-")
st = store.Store(R1)
ok, n, imp = st.check()
check("全新安装:空目录第一次启动自动建库", os.path.exists(os.path.join(R1, "ys.db")) and ok == "ok" and sum(n.values()) == 0,
"完整性=%s 行数=%s" % (ok, n))
check("全新安装:库文件权限不对外(640)", (os.stat(st.path).st_mode & 0o777) == 0o640, oct(os.stat(st.path).st_mode & 0o777))
# ================================================================== 2/3. 老数据迁移
R2 = tempfile.mkdtemp(prefix="ys-db-legacy-")
codes0, orders0 = make_legacy(R2)
before = fingerprint(R2)
st2 = store.Store(R2)
check("迁移:卡密全部导入、内容一致", st2.codes_all() == codes0, "%d 张" % len(st2.codes_all()))
check("迁移:订单全部导入、内容一致", {m["oid"]: m for m in st2.orders()} == orders0, "%d 单" % len(st2.orders()))
check("迁移:爱发电账本导入", len(st2.ledger_all()) == 5)
calls = st2.calls()
check("迁移:呼叫记录导入(写坏的半行跳过,不影响别的)", len(calls) == 6 and calls[0]["note"] == "等太久了 0", "%d 条" % len(calls))
check("迁移:小块状态(福利券 / 心跳 / 家里 IP / 模板耗时)导入",
st2.kv_get("gifts")["batches"][0]["name"] == "国庆福利" and st2.kv_get("home_ips") == {"ips": ["198.51.100.7"]}
and st2.kv_get("tpl_times")["stand"][1] == 42.5 and st2.kv_get("heartbeat")["host"] == "render")
check("迁移:老格式卡密(只有 used 布尔)能查到、状态算成 used",
st2.codes_where(state="used").get("YS-OLD0-OLD0-OLD0") is not None)
bound = [c for c, r in codes0.items() if r.get("state") == "bound"]
o1 = codes0[bound[0]]["order"]
check("迁移:按订单号查它绑的卡密(走索引)", bound[0] in st2.codes_where(order_id=o1))
after = {k: v for k, v in fingerprint(R2).items() if not k.startswith("ys.db")}
check("迁移:旧文件原样不动(一个字节都没改)", after == before)
st2b = store.Store(R2)
check("重启:不会重复导入", len(st2b.codes_all()) == len(codes0) and len(st2b.calls()) == 6 and len(st2b.orders()) == len(orders0))
# ================================================================== 4. 整份写回 = 只改有变化的行
cs = st2.codes_all()
c_up, c_del = list(cs)[0], list(cs)[1]
cs[c_up] = dict(cs[c_up], state="bound", order=o1)
del cs[c_del]
cs["YS-NEW1-NEW1-NEW1"] = {"state": "active", "batch": "新批"}
stamp = dict(st2.conn().execute("SELECT code, updated FROM codes"))
time.sleep(0.02)
st2.codes_save(cs)
stamp2 = dict(st2.conn().execute("SELECT code, updated FROM codes"))
changed = {k for k in stamp2 if stamp2[k] != stamp.get(k)}
check("写回:只动了改过 / 新加的两行,删掉的那行没了", changed == {c_up, "YS-NEW1-NEW1-NEW1"} and c_del not in stamp2,
"变动的行:%s" % sorted(changed))
check("写回:能查的列跟着变(state=bound / order_id)", c_up in st2.codes_where(state="bound", order_id=o1))
# ================================================================== 5. 订单清理
victim = list(orders0)[0]
st2.order_purge(victim)
check("清理:清掉的单对外当它不存在", st2.order_get(victim) is None and victim not in st2.order_ids())
check("清理:行还在(记账用),单号不会被再发出去", st2.order_known(victim)
and any(m["oid"] == victim for m in st2.orders(include_purged=True)))
st2.order_put(victim, orders0[victim])
check("清理:同号重新写入会复活(恢复误删用)", st2.order_get(victim) is not None)
st2.order_drop("YSNOPE00")
check("清理:删不存在的单不报错", True)
# ================================================================== 6. 事务回滚
n_before = len(st2.codes_all())
try:
with st2.tx() as c:
c.execute("INSERT INTO codes (code, state, data, updated) VALUES ('YS-HALF-HALF-HALF','active','{}',0)")
raise RuntimeError("中途出错(模拟断电 / 异常)")
except RuntimeError:
pass
check("事务:中途出错整体回滚,不留半截", len(st2.codes_all()) == n_before and st2.code_get("YS-HALF-HALF-HALF") is None)
# ================================================================== 7. 导出 → 再导入,一模一样
EXP = tempfile.mkdtemp(prefix="ys-db-export-")
st2.export_legacy(EXP)
st3 = store.Store(EXP)
check("回退:导出的 JSON 再导入,卡密一模一样", st3.codes_all() == st2.codes_all())
check("回退:订单 / 账本 / 小块状态一模一样",
st3.orders() == st2.orders() and st3.ledger_all() == st2.ledger_all()
and all(st3.kv_get(k) == st2.kv_get(k) for k in store.KV_FILES))
check("回退:呼叫记录条数和内容一致", [dict(r, id=0) for r in st3.calls()] == [dict(r, id=0) for r in st2.calls()])
# ================================================================== 8. 备份 / 体检
bp = st2.backup()
b = sqlite3.connect(bp)
bn = b.execute("SELECT COUNT(*) FROM codes").fetchone()[0]
b.close()
check("备份:热备份能打开、行数一致", bn == len(st2.codes_all()), bp)
check("体检:integrity_check = ok", st2.check()[0] == "ok")
# ================================================================== 9. 服务器跑在迁移过来的老数据上
R4 = tempfile.mkdtemp(prefix="ys-db-server-")
codes4, orders4 = make_legacy(R4, n_codes=12, n_orders=6)
o4 = [o for o, m in orders4.items() if m["state"] == "done"][0]
b4 = [c for c, r in codes4.items() if r.get("state") == "bound"][0]
probe = r"""
import sys, json, threading, urllib.request
sys.path.insert(0, %(src)r)
import server as S
srv = S.ThreadingHTTPServer(("127.0.0.1", 0), S.H); srv.daemon_threads = True
threading.Thread(target=srv.serve_forever, daemon=True).start()
base = "http://127.0.0.1:%%d" %% srv.server_address[1]
def get(p):
try:
with urllib.request.urlopen(base + p, timeout=20) as r: return r.status, json.loads(r.read())
except urllib.error.HTTPError as e: return e.code, {}
out = {}
out["order"] = get("/api/order/%(oid)s?t=%(tok)s")
out["badtok"] = get("/api/order/%(oid)s?t=WRONGTOKEN0000")
out["code"] = S.code_info(%(code)r)
out["status"] = get("/api/status")
out["home_ips"] = S.HOME_IPS
out["waiting"] = [m["oid"] for m in S.list_orders(("waiting",))]
print(json.dumps(out, ensure_ascii=False))
srv.shutdown()
""" % {"src": SRC, "oid": o4, "tok": orders4[o4]["token"], "code": b4}
env = dict(os.environ, YS_ROOT=R4)
p = subprocess.run([sys.executable, "-c", probe], capture_output=True, text=True, env=env, cwd=SRC, timeout=120)
try:
res = json.loads(p.stdout.strip().splitlines()[-1])
except Exception:
res = {}
print(p.stdout[-800:], p.stderr[-1500:])
check("服务器:老订单直接能查(口令对 → 200)", (res.get("order") or [0])[0] == 200, str(res.get("order"))[:100])
check("服务器:口令错 → 404(和以前一样不承认这单)", (res.get("badtok") or [0])[0] == 404)
check("服务器:老卡密能认,带出它绑的单", (res.get("code") or {}).get("oid") == codes4[b4]["order"])
check("服务器:状态接口照常", (res.get("status") or [0])[0] == 200)
check("服务器:重启后还记得家里的出口 IP(从 home_ips.json 迁过来)", res.get("home_ips") == ["198.51.100.7"])
check("服务器:按状态列队列", sorted(res.get("waiting") or []) == sorted(o for o, m in orders4.items() if m["state"] == "waiting"))
for args, want in ((["db", "check"], "完整性:ok"), (["calls"], "10.0.0.1"),
(["ls", "waiting"], ""), (["stat"], "")):
q = subprocess.run([sys.executable, os.path.join(SRC, "admin.py")] + args, capture_output=True, text=True,
env=env, cwd=SRC, timeout=60)
check("admin.py %s 能跑" % " ".join(args), q.returncode == 0 and want in q.stdout, (q.stdout + q.stderr).strip()[-120:])
# ================================================================== 10. 并发
R5 = tempfile.mkdtemp(prefix="ys-db-conc-")
st5 = store.Store(R5)
def spam(k):
for i in range(50):
st5.call_add({"ts": "2026-10-05T12:00:%02d+08:00" % (i % 60), "ip": "t%d" % k, "order": "", "note": str(i)})
ths = [threading.Thread(target=spam, args=(k,)) for k in range(8)]
[t.start() for t in ths]
[t.join() for t in ths]
check("并发:8 个线程同时记呼叫,400 条一条不丢", len(st5.calls()) == 400, "%d 条" % len(st5.calls()))
writer = r"""
import sys; sys.path.insert(0, %r)
import store
s = store.Store(%r)
for i in range(300):
s.code_put("YS-P%%s-%%04d-AAAA" %% (sys.argv[1], i), {"state": "active", "batch": "proc" + sys.argv[1]})
""" % (SRC, R5)
ps = [subprocess.Popen([sys.executable, "-c", writer, x]) for x in ("A", "B")]
[p.wait(timeout=120) for p in ps]
check("并发:两个进程同时各写 300 张码,600 张一张不少", len(st5.codes_all()) == 600, "%d 张" % len(st5.codes_all()))
R7 = tempfile.mkdtemp(prefix="ys-db-race-")
make_legacy(R7, n_codes=20, n_orders=6)
opener = "import sys; sys.path.insert(0, %r); import store; store.Store(%r)" % (SRC, R7)
ps = [subprocess.Popen([sys.executable, "-c", opener]) for _ in range(3)]
[p.wait(timeout=120) for p in ps]
st7 = store.Store(R7)
check("并发:三个进程同时第一次启动,旧数据只导一次(呼叫记录不翻倍)",
len(st7.calls()) == 6 and len(st7.codes_all()) == 21, "呼叫 %d 条 · 卡密 %d 张" % (len(st7.calls()), len(st7.codes_all())))
shutil.rmtree(R7, ignore_errors=True)
# ================================================================== 11. 规模
R6 = tempfile.mkdtemp(prefix="ys-db-scale-")
make_legacy(R6, n_codes=5000, n_orders=2000)
t0 = time.time()
st6 = store.Store(R6)
t_imp = time.time() - t0
t0 = time.time()
for _ in range(20):
w = st6.orders(("waiting",))
t_q = (time.time() - t0) / 20
t0 = time.time()
for _ in range(20):
old = []
od = os.path.join(R6, "orders")
for oid in sorted(os.listdir(od)):
m = json.load(open(os.path.join(od, oid, "meta.json"), encoding="utf-8"))
if m.get("state") == "waiting":
old.append(m)
t_old = (time.time() - t0) / 20
check("规模:5000 张码 + 2000 单首次导入 < 10 秒", t_imp < 10, "%.2f 秒" % t_imp)
check("规模:按状态列队列比旧版(一单一单读文件)快", t_q < t_old and len(w) == len(old),
"数据库 %.1f ms / 旧版 %.1f ms(%d 单在排队)" % (t_q * 1000, t_old * 1000, len(w)))
# ================================================================== 汇总
for d in (R1, R2, EXP, R4, R5, R6):
shutil.rmtree(d, ignore_errors=True)
bad = [n for n, ok, _ in RESULTS if not ok]
print("\n%d 条,失败 %d 条" % (len(RESULTS), len(bad)))
for n in bad:
print(" ✗ " + n)
sys.exit(1 if bad else 0)