# -*- coding: utf-8 -*- """选择题:题库 → 切题 → 判分 → 接口,一条线都要通。""" import unittest from backend import content, cut, judging class ChoiceBankTest(unittest.TestCase): def test_both_subjects_have_choice(self): for subject in ("math", "chinese"): qs = content.papers_for(2, subject) picks = [q for q in qs if q["judge_type"] == "choice"] self.assertTrue(picks, f"{subject} 题库里没有选择题") for q in picks: self.assertGreaterEqual(len(q["options"]), 2) self.assertIn(q["answer"], "ABCD"[: len(q["options"])]) def test_only_two_subjects(self): self.assertEqual(content.SUBJECT_ZH, {"math": "数学", "chinese": "语文"}) class ChoiceJudgeTest(unittest.TestCase): def test_letter_forms(self): for given in ("C", "c", "选C", "C. 56", "(C)"): ok, _ = judging.judge("choice", "C", given) self.assertTrue(ok, given) def test_wrong_pick(self): ok, detail = judging.judge("choice", "C", "A") self.assertFalse(ok) self.assertIn("应选 C", detail) def test_no_pick(self): ok, _ = judging.judge("choice", "C", "") self.assertFalse(ok) def test_old_text_answer(self): # 老数据:选字填空的答案存的是文字 self.assertTrue(judging.judge("choice", "再", "再")[0]) self.assertFalse(judging.judge("choice", "再", "在")[0]) class ChoiceCutTest(unittest.TestCase): def test_options_on_separate_lines(self): lines = [{"text": "1.6 + 6 =( )"}, {"text": "A.10"}, {"text": "B.11"}, {"text": "C.12"}, {"text": "D.13"}, {"text": "2.36 + 47 ="}] qs = cut.segment(lines, "math") self.assertEqual(qs[0]["judge_type"], "choice") self.assertEqual(qs[0]["options"], ["10", "11", "12", "13"]) self.assertEqual(qs[0]["answer"], "C") # 算出来 12,对到 C self.assertEqual(qs[1]["judge_type"], "calc") def test_options_in_one_line(self): lines = [{"text": "3.下面哪个算式的结果最大?( )"}, {"text": "A.6×6 B.8×4 C.9×4 D.5×9"}] qs = cut.segment(lines, "math") self.assertEqual(qs[0]["options"], ["6×6", "8×4", "9×4", "5×9"]) self.assertEqual(qs[0]["judge_type"], "manual") # 答案没标,交给孩子自评 self.assertEqual(qs[0]["stem"], "下面哪个算式的结果最大?( )") def test_no_options_still_calc(self): qs = cut.segment([{"text": "1.72-38 ="}], "math") self.assertEqual(qs[0]["judge_type"], "calc") self.assertEqual(qs[0]["answer"], "34") self.assertIsNone(qs[0]["options"]) if __name__ == "__main__": unittest.main()