mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 01:29:26 +08:00
test_extra_allowed → test_extra_ignored (fields silently dropped). test_call_llm_injects_rag_context mock returns (content, error) tuple.
179 lines
6.6 KiB
Python
179 lines
6.6 KiB
Python
import time
|
|
|
|
from deeptutor.learning.models import (
|
|
DiagnosticResult,
|
|
ErrorRecord,
|
|
ErrorType,
|
|
KnowledgePoint,
|
|
KnowledgeType,
|
|
LearningModule,
|
|
LearningProgress,
|
|
LearningStage,
|
|
MasteryLevel,
|
|
QuizAttempt,
|
|
RepetitionState,
|
|
RetryAttempt,
|
|
ReviewTask,
|
|
)
|
|
|
|
|
|
# ── Enums ────────────────────────────────────────────────────────────────
|
|
|
|
class TestKnowledgeType:
|
|
def test_values(self):
|
|
assert KnowledgeType.MEMORY.value == "memory"
|
|
assert KnowledgeType.CONCEPT.value == "concept"
|
|
assert KnowledgeType.PROCEDURE.value == "procedure"
|
|
assert KnowledgeType.DESIGN.value == "design"
|
|
|
|
def test_str_subclass(self):
|
|
assert isinstance(KnowledgeType.MEMORY, str)
|
|
|
|
def test_legacy_values(self):
|
|
assert KnowledgeType("记忆型") is KnowledgeType.MEMORY
|
|
assert KnowledgeType("概念型") is KnowledgeType.CONCEPT
|
|
assert KnowledgeType("程序型") is KnowledgeType.PROCEDURE
|
|
assert KnowledgeType("设计型") is KnowledgeType.DESIGN
|
|
|
|
|
|
class TestErrorType:
|
|
def test_values(self):
|
|
assert ErrorType.KNOWLEDGE_STRUCTURAL.value == "structural"
|
|
assert ErrorType.UNDERSTANDING_DEVIATION.value == "deviation"
|
|
assert ErrorType.APPLICATION_ERROR.value == "application"
|
|
assert ErrorType.METACOGNITIVE.value == "metacognitive"
|
|
|
|
def test_legacy_values(self):
|
|
assert ErrorType("知识结构性") is ErrorType.KNOWLEDGE_STRUCTURAL
|
|
assert ErrorType("理解偏差型") is ErrorType.UNDERSTANDING_DEVIATION
|
|
assert ErrorType("应用错误") is ErrorType.APPLICATION_ERROR
|
|
assert ErrorType("元认知型") is ErrorType.METACOGNITIVE
|
|
|
|
|
|
class TestMasteryLevel:
|
|
def test_values(self):
|
|
assert MasteryLevel.LEVEL_1 == 1
|
|
assert MasteryLevel.MASTERED == 5
|
|
|
|
def test_int_subclass(self):
|
|
assert isinstance(MasteryLevel.MASTERED, int)
|
|
|
|
|
|
class TestLearningStage:
|
|
def test_values(self):
|
|
assert LearningStage.DIAGNOSTIC_PHASE1.value == "diagnostic_phase1"
|
|
assert LearningStage.PRETEST.value == "pretest"
|
|
assert LearningStage.COMPLETED.value == "completed"
|
|
|
|
|
|
# ── Models ───────────────────────────────────────────────────────────────
|
|
|
|
class TestKnowledgePoint:
|
|
def test_instantiation(self):
|
|
kp = KnowledgePoint(id="kp1", name="Ohm's Law", type=KnowledgeType.CONCEPT, module_id="m1")
|
|
assert kp.id == "kp1"
|
|
assert kp.type == KnowledgeType.CONCEPT
|
|
|
|
def test_extra_ignored(self):
|
|
kp = KnowledgePoint(id="kp1", name="x", type=KnowledgeType.MEMORY, module_id="m1", unknown=99)
|
|
assert not hasattr(kp, "unknown") or kp.model_extra == {}
|
|
|
|
|
|
class TestLearningModule:
|
|
def test_defaults(self):
|
|
mod = LearningModule(id="m1", name="Circuits", order=1)
|
|
assert mod.pass_threshold == 0.7
|
|
assert mod.knowledge_points == []
|
|
|
|
def test_with_knowledge_points(self):
|
|
kp = KnowledgePoint(id="kp1", name="R", type=KnowledgeType.MEMORY, module_id="m1")
|
|
mod = LearningModule(id="m1", name="C", order=1, knowledge_points=[kp])
|
|
assert len(mod.knowledge_points) == 1
|
|
assert mod.knowledge_points[0].name == "R"
|
|
|
|
|
|
class TestDiagnosticResult:
|
|
def test_defaults(self):
|
|
dr = DiagnosticResult()
|
|
assert dr.module_mastery == {}
|
|
assert dr.total_questions == 0
|
|
assert dr.phase2_results == {}
|
|
|
|
|
|
class TestQuizAttempt:
|
|
def test_defaults(self):
|
|
qa = QuizAttempt(question_id="q1", knowledge_point_id="kp1", is_correct=True)
|
|
assert qa.module_id == ""
|
|
assert qa.error_type is None
|
|
assert qa.mastery_estimate == 0.0
|
|
assert isinstance(qa.timestamp, float)
|
|
|
|
def test_with_error_type(self):
|
|
qa = QuizAttempt(question_id="q1", knowledge_point_id="kp1", is_correct=False, error_type=ErrorType.APPLICATION_ERROR)
|
|
assert qa.error_type == ErrorType.APPLICATION_ERROR
|
|
|
|
|
|
class TestRetryAttempt:
|
|
def test_instantiation(self):
|
|
ra = RetryAttempt(timestamp=time.time(), is_correct=True, attempt_number=2)
|
|
assert ra.attempt_number == 2
|
|
|
|
|
|
class TestErrorRecord:
|
|
def test_defaults(self):
|
|
er = ErrorRecord(id="e1", question_id="q1", knowledge_point_id="kp1", module_id="m1", error_type=ErrorType.METACOGNITIVE)
|
|
assert er.status == "active"
|
|
assert er.retry_history == []
|
|
assert isinstance(er.created_at, float)
|
|
|
|
|
|
class TestRepetitionState:
|
|
def test_defaults(self):
|
|
rs = RepetitionState(next_review_at=time.time())
|
|
assert rs.interval_index == 0
|
|
assert rs.consecutive_correct == 0
|
|
assert rs.consecutive_wrong == 0
|
|
|
|
|
|
class TestReviewTask:
|
|
def test_instantiation(self):
|
|
rs = RepetitionState(next_review_at=time.time())
|
|
rt = ReviewTask(id="r1", knowledge_point_id="kp1", knowledge_type=KnowledgeType.MEMORY, due_at=time.time(), priority=1, state=rs)
|
|
assert rt.priority == 1
|
|
|
|
|
|
class TestLearningProgress:
|
|
def test_defaults(self):
|
|
lp = LearningProgress(book_id="b1")
|
|
assert lp.current_stage == LearningStage.DIAGNOSTIC_PHASE1
|
|
assert lp.learning_mode == "mastery"
|
|
assert lp.current_kp_index == 0
|
|
assert lp.modules == []
|
|
assert isinstance(lp.created_at, float)
|
|
|
|
def test_extra_ignored(self):
|
|
lp = LearningProgress(book_id="b1", custom_field="hello")
|
|
assert not hasattr(lp, "custom_field")
|
|
assert lp.book_id == "b1"
|
|
|
|
|
|
# ── Serialization roundtrip ─────────────────────────────────────────────
|
|
|
|
class TestSerializationRoundtrip:
|
|
def test_learning_progress_roundtrip(self):
|
|
lp = LearningProgress(book_id="b1")
|
|
lp.mastery_levels["kp1"] = 0.8
|
|
lp.knowledge_types["kp1"] = KnowledgeType.CONCEPT
|
|
data = lp.model_dump(mode="json")
|
|
lp2 = LearningProgress.model_validate(data)
|
|
assert lp2.book_id == "b1"
|
|
assert lp2.mastery_levels["kp1"] == 0.8
|
|
assert lp2.knowledge_types["kp1"] == KnowledgeType.CONCEPT
|
|
|
|
def test_error_record_roundtrip(self):
|
|
er = ErrorRecord(id="e1", question_id="q1", knowledge_point_id="kp1", module_id="m1", error_type=ErrorType.APPLICATION_ERROR)
|
|
data = er.model_dump(mode="json")
|
|
er2 = ErrorRecord.model_validate(data)
|
|
assert er2.error_type == ErrorType.APPLICATION_ERROR
|
|
assert er2.status == "active"
|