W6:变式自校验(verify)+严档(原题做对先换一道,变式做对才算)(267 测试)

This commit is contained in:
dsh
2026-11-24 20:19:20 +08:00
parent 5fcbe086fd
commit dfc9656eb2
14 changed files with 371 additions and 12 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ try:
st, r = call("GET", "/api/health")
check("health 200", st == 200, r)
check("routes=61", r["data"]["routes"] == 61, r["data"].get("routes"))
check("routes=62", r["data"]["routes"] == 62, r["data"].get("routes"))
st, r = call("POST", "/api/auth/sms-code", {"phone": "13800138000"})
check("发码 200", st == 200 and r["data"]["sent"] is True, r)
+1 -1
View File
@@ -63,7 +63,7 @@ try:
st, r = call("GET", "/api/health")
check("health 200", st == 200, r)
check("routes=61", r["data"]["routes"] == 61, r["data"].get("routes"))
check("routes=62", r["data"]["routes"] == 62, r["data"].get("routes"))
st, r = call("POST", "/api/auth/wx-login", {"code": "demo"})
tok = r["data"]["token"]
+114
View File
@@ -0,0 +1,114 @@
"""W6:变式自校验 + 严档毕业。"""
import unittest
import backend.db as dbm
import backend.practice as practice
import backend.services as svc
from backend.services import ApiError
from ._tmpdir import workdir
class VerifyTest(unittest.TestCase):
def test_good_calc(self):
ok, why = practice.verify({"stem": "36 + 24 =", "answer": "60", "judge_type": "calc"})
self.assertTrue(ok, why)
def test_wrong_answer_rejected(self):
ok, why = practice.verify({"stem": "36 + 24 =", "answer": "61", "judge_type": "calc"})
self.assertFalse(ok)
self.assertIn("对不上", why)
def test_uncomputable_rejected(self):
ok, _ = practice.verify({"stem": "一个数加 3 等于几?", "answer": "5", "judge_type": "calc"})
self.assertFalse(ok)
def test_empty_answer_rejected(self):
ok, why = practice.verify({"stem": "36 + 24 =", "answer": "", "judge_type": "calc"})
self.assertFalse(ok)
self.assertIn("没有答案", why)
def test_choice_answer_must_be_in_options(self):
ok, _ = practice.verify({"stem": "哪个对?", "answer": "E", "judge_type": "choice",
"options": {"A": "1", "B": "2"}})
self.assertFalse(ok)
ok2, _ = practice.verify({"stem": "哪个对?", "answer": "B", "judge_type": "choice",
"options": {"A": "1", "B": "2"}})
self.assertTrue(ok2)
def test_similar_only_returns_verified(self):
items = practice.similar("36 + 47 =", "83", "calc", "math", 2, n=3)
for it in items:
self.assertTrue(practice.verify(it)[0], it)
class StrictBase(unittest.TestCase):
def setUp(self):
self.conn = dbm.connect(workdir("w6") / (self._testMethodName + ".db"))
dbm.init_db(self.conn)
self.gid = svc.login(self.conn, "demo")["guardian_id"]
svc.give_consent(self.conn, self.gid, "v1")
self.cid = svc.create_child(self.conn, self.gid, "皮皮", 2)["id"]
def tearDown(self):
self.conn.close()
def _queued(self, stem="36 + 47 ="):
import backend.judging as judging
ans = judging.fmt_qty(judging.safe_eval(stem.rstrip("== ")))
r = svc.add_manual_mistake(self.conn, self.gid, self.cid, "math", stem, ans)
mid = r["created"][0]["mistake_id"]
for _ in range(3):
self.conn.execute("UPDATE mistakes SET next_due=? WHERE id=?", (svc.today(), mid))
svc.attempt(self.conn, self.gid, self.cid, mid, "999")
# 隔一天再做对:否则会先命中「当天错了又对 → 会但不稳」,走不到严档分支
svc.dev_advance(self.conn, 1)
return mid
def _ans(self, mid):
return self.conn.execute("SELECT answer FROM mistakes WHERE id=?", (mid,)).fetchone()["answer"]
def _strict(self, on=True):
return svc.set_settings(self.conn, self.gid, self.cid, 1 if on else 0)["strict_variant"]
class StrictFlowTest(StrictBase):
def test_strict_requires_variant_before_passed_once(self):
mid = self._queued()
self.assertEqual(self._strict(), 1)
r = svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid))
self.assertEqual(r["status"], "variant_pending")
self.assertTrue(r["need_variant"])
# 变式做对 → 才算第一次
r2 = svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid), is_variant=True)
self.assertEqual(r2["status"], "passed_once")
def test_variant_wrong_resets(self):
mid = self._queued("52 + 39 =")
self._strict()
svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid))
r = svc.attempt(self.conn, self.gid, self.cid, mid, "999", is_variant=True)
self.assertEqual(r["status"], "queued")
self.assertFalse(r["correct"])
def test_default_off_keeps_old_behaviour(self):
mid = self._queued("18 + 27 =")
r = svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid))
self.assertEqual(r["status"], "passed_once")
def test_cannot_fake_variant(self):
mid = self._queued("66 + 18 =")
with self.assertRaises(ApiError) as ctx:
svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid), is_variant=True)
self.assertEqual(ctx.exception.code, "NOT_VARIANT_PENDING")
def test_pending_requires_variant_flag(self):
mid = self._queued("77 + 16 =")
self._strict()
svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid))
with self.assertRaises(ApiError) as ctx:
svc.attempt(self.conn, self.gid, self.cid, mid, self._ans(mid))
self.assertEqual(ctx.exception.code, "NEED_VARIANT")
if __name__ == "__main__":
unittest.main()