B-4 逐条勾选:入库前把一块里的题摊开,只收勾上的(prepare 接口 + 弹窗,239 测试)

This commit is contained in:
2026-11-22 15:30:47 +08:00
parent 11a7f18b31
commit ef201ffacc
13 changed files with 305 additions and 15 deletions
+55
View File
@@ -151,6 +151,61 @@ class ScanFlowTest(unittest.TestCase):
self.assertEqual(len(got), 3)
self.assertTrue(any("40×65" in s for s in got))
def test_prepare_lists_every_sub(self):
"""入库前预览:一块三道的口算卡要摊成 3 条,且带上 block/sub 坐标。"""
d = self._detect()
subs = [{"stem": f"{e} =", "answer": a, "judge_type": "calc"}
for e, a in [("34+25", "59"), ("78-25", "53"), ("27+69", "96")]]
with mock.patch.object(scanflow, "recognize_block",
return_value={"stem": "34+25= 78-25= 27+69=", "judge_type": "card",
"options": None, "answer": None, "page_answer": None,
"source": "page", "subs": subs}):
pv = svc.prepare_scan_picks(
self.conn, self.gid, self.cid, "math", d["draft_id"], [d["blocks"][0]["i"]])
self.assertEqual(len(pv["items"]), 3)
self.assertEqual([it["sub"] for it in pv["items"]], [0, 1, 2])
self.assertTrue(all(it["block"] == d["blocks"][0]["i"] for it in pv["items"]))
# 预览只读:预览完库里还是空的
n = self.conn.execute("SELECT COUNT(*) FROM questions").fetchone()[0]
self.assertEqual(n, 0)
def test_import_only_checked_subs(self):
"""逐条勾选:5 道口算里只勾第 0、2 道,就只入库这 2 道。"""
d = self._detect()
subs = [{"stem": f"{e} =", "answer": a, "judge_type": "calc"}
for e, a in [("34+25", "59"), ("78-25", "53"), ("27+69", "96"),
("77+69", "146"), ("90-49", "41")]]
bi = d["blocks"][0]["i"]
with mock.patch.object(scanflow, "recognize_block",
return_value={"stem": "口算卡", "judge_type": "card",
"options": None, "answer": None, "page_answer": None,
"source": "page", "subs": subs}):
paper = svc.import_scan_picks(
self.conn, self.gid, self.cid, "math", d["draft_id"],
[{"i": bi, "subs": [0, 2]}])
self.assertEqual(len(paper["questions"]), 2)
got = [r["stem"] for r in self.conn.execute(
"SELECT stem FROM mistakes WHERE child_id=? ORDER BY id", (self.cid,))]
self.assertEqual(len(got), 2)
self.assertTrue(any("34+25" in s for s in got))
self.assertTrue(any("27+69" in s for s in got))
self.assertFalse(any("77+69" in s for s in got), "没勾的(读歪那条)不该入库")
def test_import_empty_sub_list_falls_back_to_nothing(self):
"""subs 全取消:这块就不该入库,不能退化成「整块都要」。"""
d = self._detect()
subs = [{"stem": "1+1 =", "answer": "2", "judge_type": "calc"},
{"stem": "2+2 =", "answer": "4", "judge_type": "calc"}]
bi = d["blocks"][0]["i"]
with mock.patch.object(scanflow, "recognize_block",
return_value={"stem": "x", "judge_type": "card", "options": None,
"answer": None, "page_answer": None,
"source": "page", "subs": subs}):
with self.assertRaises(ApiError) as ctx:
svc.import_scan_picks(self.conn, self.gid, self.cid, "math",
d["draft_id"], [{"i": bi, "subs": []}])
self.assertEqual(ctx.exception.code, "NO_PICKS")
if __name__ == "__main__":
unittest.main()