"""知识点归类规则的测试:认得出、认不准就不给、名字都能对上模板。""" import unittest from backend import kp_match class RulesHealthTest(unittest.TestCase): """规则表本身不能烂:候选名必须在 kpt.json 里存在。""" def test_every_candidate_resolves(self): bad = kp_match.rules_ok() self.assertEqual(bad, [], f"这些候选名在 kpt.json 里找不到:{bad}") def test_templates_loaded(self): self.assertGreaterEqual(len(kp_match._templates()), 100) class MathTest(unittest.TestCase): def test_two_number_sum(self): self.assertEqual(kp_match.classify("math", "36 + 47 ="), "M026") def test_multiplication(self): self.assertEqual(kp_match.classify("math", "7 × 8 ="), "M039") def test_division(self): self.assertEqual(kp_match.classify("math", "72 ÷ 8 ="), "M040") def test_decimal_beats_integer(self): # 小数乘法要排在整数乘法前面,否则会被「×」那条先抓走 self.assertEqual(kp_match.classify("math", "2.5 × 4 ="), "M077") def test_decimal_division(self): self.assertEqual(kp_match.classify("math", "4.8 ÷ 6 ="), "M078") def test_fraction(self): self.assertEqual(kp_match.classify("math", "1/4 × 3/5 ="), "M087") def test_area(self): self.assertEqual(kp_match.classify("math", "一个长方形的面积是多少"), "M051") def test_units(self): self.assertEqual(kp_match.classify("math", "一袋大米重 5 千克"), "M046") def test_chain(self): self.assertEqual(kp_match.classify("math", "8 × 7 + 6 ="), "M039") def test_fraction_beats_unit(self): # 「吨」和「7/10」同时出现:这是分数应用题,不是单位换算 self.assertEqual( kp_match.classify("math", "一批货物重20吨,这批货物的7/10是多少吨"), "M086") class ChineseTest(unittest.TestCase): def test_pinyin_write(self): self.assertEqual(kp_match.classify("chinese", "看拼音写词:xióng māo"), "C011") def test_measure_word(self): self.assertEqual(kp_match.classify("chinese", "填量词:一( )书"), "C015") def test_synonym(self): self.assertEqual(kp_match.classify("chinese", "写出近义词:高兴—( )"), "C032") def test_punctuation(self): self.assertEqual(kp_match.classify("chinese", "给下面的句子加标点"), "C018") def test_idiom(self): self.assertEqual(kp_match.classify("chinese", "成语填空:守株待( )"), "C034") class NoGuessTest(unittest.TestCase): """认不准的时候宁可不给。""" def test_empty(self): self.assertIsNone(kp_match.classify("math", "")) self.assertIsNone(kp_match.classify("math", None)) def test_unrelated(self): self.assertIsNone(kp_match.classify("math", "下列哪个不是三角形")) def test_name_of(self): self.assertEqual(kp_match.name_of("M026"), "加减法(混合)/万以内加减法") self.assertEqual(kp_match.name_of(None), "") if __name__ == "__main__": unittest.main()