feat: edit quiz questions over MCP
- quizzes_create_question, update_question, delete_question and
reorder_questions on /api/v1/quizzes/{slug}/questions
- update_question replaces answers by id (kept, created, deleted), so
answer ids stored in past attempts stay valid; foreign ids are refused
- classes.CheckQuestion holds the importer's per-question rules, shared
with the manage routes; attempt scores are never touched (D-16)
This commit is contained in:
@@ -112,3 +112,74 @@ func TestBuildStats(t *testing.T) {
|
||||
t.Errorf("most_chosen_wrong by count = %+v, want 103", w)
|
||||
}
|
||||
}
|
||||
|
||||
func uid(n uint) *uint { return &n }
|
||||
|
||||
// TestQuestionInputRules: MergeQuestion applies a partial update and the
|
||||
// answers list to the current question, and with CheckQuestion refuses
|
||||
// what the importer would refuse.
|
||||
func TestQuestionInputRules(t *testing.T) {
|
||||
single := models.Question{ID: 1, Text: "Stolica?", Type: models.TypeSingle, Points: 2, Answers: []models.Answer{
|
||||
{ID: 10, QuestionID: 1, Text: "Warszawa", IsCorrect: true, Explanation: str("Od 1596.")},
|
||||
{ID: 11, QuestionID: 1, Text: "Kraków"},
|
||||
}}
|
||||
card := models.Question{ID: 2, Text: "Odporność?", Type: models.TypeSelfCheck, AnswerText: str("Zdolność"), Points: 1}
|
||||
check := func(current models.Question, in classes.QuestionInput) (classes.ImportQuestion, []string) {
|
||||
merged, problems := classes.MergeQuestion(current, in)
|
||||
return merged, append(problems, classes.CheckQuestion("question", merged)...)
|
||||
}
|
||||
|
||||
merged, problems := check(single, classes.QuestionInput{Text: str(" Stolica Polski? ")})
|
||||
if len(problems) != 0 || merged.Text != "Stolica Polski?" || *merged.Points != 2 || len(merged.Answers) != 2 || *merged.Answers[0].Explanation != "Od 1596." {
|
||||
t.Errorf("text only: %+v %v", merged, problems)
|
||||
}
|
||||
answers := []classes.ManagedAnswerInput{{ID: uid(99), Text: "Gdańsk", IsCorrect: true}, {ID: uid(11), Text: "Kraków"}}
|
||||
if _, problems := check(single, classes.QuestionInput{Answers: &answers}); !strings.Contains(strings.Join(problems, "; "), "answers[0].id does not belong to this question") {
|
||||
t.Errorf("foreign answer id: %v", problems)
|
||||
}
|
||||
twice := []classes.ManagedAnswerInput{{ID: uid(10), Text: "Warszawa", IsCorrect: true}, {ID: uid(10), Text: "Kraków"}}
|
||||
if _, problems := check(single, classes.QuestionInput{Answers: &twice}); !strings.Contains(strings.Join(problems, "; "), "listed twice") {
|
||||
t.Errorf("answer id twice: %v", problems)
|
||||
}
|
||||
two := []classes.ManagedAnswerInput{{ID: uid(10), Text: "Warszawa", IsCorrect: true}, {ID: uid(11), Text: "Kraków", IsCorrect: true}}
|
||||
if _, problems := check(single, classes.QuestionInput{Answers: &two}); !strings.Contains(strings.Join(problems, "; "), "exactly one correct") {
|
||||
t.Errorf("single with two correct: %v", problems)
|
||||
}
|
||||
if _, problems := check(single, classes.QuestionInput{Type: str(models.TypeSelfCheck)}); !strings.Contains(strings.Join(problems, "; "), "self_check question has no answers") {
|
||||
t.Errorf("type to self_check with answers left: %v", problems)
|
||||
}
|
||||
kept := []classes.ManagedAnswerInput{{ID: uid(10), Text: "Warszawa", IsCorrect: true}, {Text: "Gdańsk", Explanation: str("")}}
|
||||
merged, problems = check(single, classes.QuestionInput{Answers: &kept, Explanation: str("")})
|
||||
if len(problems) != 0 || len(merged.Answers) != 2 || *merged.Answers[0].Explanation != "Od 1596." || merged.Answers[1].Explanation != nil || merged.Explanation != nil {
|
||||
t.Errorf("kept and new answers: %+v %v", merged, problems)
|
||||
}
|
||||
toSingle := []classes.ManagedAnswerInput{{Text: "Tak", IsCorrect: true}, {Text: "Nie"}}
|
||||
merged, problems = check(card, classes.QuestionInput{Type: str(models.TypeSingle), Answers: &toSingle, AnswerText: str("")})
|
||||
if len(problems) != 0 || merged.Type != models.TypeSingle || merged.AnswerText != nil {
|
||||
t.Errorf("self_check to single: %+v %v", merged, problems)
|
||||
}
|
||||
if problems := classes.CheckNewQuestion(classes.QuestionInput{Text: str("Q")}); !strings.Contains(strings.Join(problems, "; "), "type is required") {
|
||||
t.Errorf("create without type: %v", problems)
|
||||
}
|
||||
withID := []classes.ManagedAnswerInput{{ID: uid(10), Text: "a", IsCorrect: true}, {Text: "b"}}
|
||||
if problems := classes.CheckNewQuestion(classes.QuestionInput{Text: str("Q"), Type: str("single"), Answers: &withID}); !strings.Contains(strings.Join(problems, "; "), "does not belong") {
|
||||
t.Errorf("create with an answer id: %v", problems)
|
||||
}
|
||||
if problems := classes.CheckNewQuestion(classes.QuestionInput{Text: str("Q"), Type: str("self_check")}); len(problems) != 0 {
|
||||
t.Errorf("minimal self_check create: %v", problems)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckQuestionOrder: an empty list and a repeated id are refused
|
||||
// before the database; a missing or foreign id is refused by
|
||||
// ReorderQuestions against the quiz's questions.
|
||||
func TestCheckQuestionOrder(t *testing.T) {
|
||||
for _, ids := range [][]uint{nil, {}, {1, 2, 1}} {
|
||||
if problems := classes.CheckQuestionOrder(ids); len(problems) != 1 {
|
||||
t.Errorf("%v: %v, want one problem", ids, problems)
|
||||
}
|
||||
}
|
||||
if problems := classes.CheckQuestionOrder([]uint{3, 1, 2}); len(problems) != 0 {
|
||||
t.Errorf("valid order: %v", problems)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user