mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 02:20:08 +08:00
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""Tests for the unified grade_answer function."""
|
|
|
|
from deeptutor.learning.grading import grade_answer
|
|
|
|
|
|
class TestChoiceGrading:
|
|
def test_choice_exact_match(self):
|
|
assert grade_answer("A", "A", "choice") is True
|
|
|
|
def test_choice_case_insensitive(self):
|
|
assert grade_answer("b", "B", "choice") is True
|
|
|
|
def test_choice_with_spaces(self):
|
|
assert grade_answer("A ", " A", "choice") is True
|
|
|
|
def test_choice_wrong(self):
|
|
assert grade_answer("C", "A", "choice") is False
|
|
|
|
|
|
class TestShortGrading:
|
|
def test_short_exact_match(self):
|
|
assert grade_answer("photosynthesis", "photosynthesis", "short") is True
|
|
|
|
def test_short_fuzzy_pass(self):
|
|
# "photosynthesi" vs "photosynthesis" — high similarity
|
|
assert grade_answer("photosynthesi", "photosynthesis", "short") is True
|
|
|
|
def test_short_fuzzy_fail(self):
|
|
assert grade_answer("completely different", "photosynthesis", "short") is False
|
|
|
|
def test_short_long_expected_no_fuzzy(self):
|
|
long_expected = "a" * 31 # >30 chars, no fuzzy
|
|
assert grade_answer(long_expected, long_expected, "short") is True
|
|
assert grade_answer("something else entirely", long_expected, "short") is False
|
|
|
|
|
|
class TestOpenGrading:
|
|
def test_open_keywords_pass(self):
|
|
expected = "cell membrane, nucleus, mitochondria"
|
|
user = "The cell has a cell membrane and nucleus, with mitochondria for energy"
|
|
assert grade_answer(user, expected, "open") is True
|
|
|
|
def test_open_keywords_fail(self):
|
|
expected = "cell membrane, nucleus, mitochondria"
|
|
user = "I don't know anything about cells"
|
|
assert grade_answer(user, expected, "open") is False
|
|
|
|
def test_open_chinese_separators(self):
|
|
expected = "光合作用;叶绿体;二氧化碳"
|
|
user = "光合作用发生在叶绿体中,需要二氧化碳"
|
|
assert grade_answer(user, expected, "open") is True
|
|
|
|
|
|
class TestEdgeCases:
|
|
def test_empty_expected_returns_false(self):
|
|
assert grade_answer("anything", "", "short") is False
|
|
assert grade_answer("anything", " ", "short") is False
|
|
|
|
def test_empty_user_answer(self):
|
|
assert grade_answer("", "expected", "short") is False
|
|
|
|
def test_substring_no_longer_matches(self):
|
|
"""Regression: 'expected in user' substring match must not cause false positive."""
|
|
user = "I do not know electromagnetic induction but maybe something else"
|
|
expected = "electromagnetic induction"
|
|
assert grade_answer(user, expected, "short") is False
|
|
|
|
def test_unknown_type_returns_false(self):
|
|
assert grade_answer("a", "a", "unknown") is False
|