Add associated_question_id on question set items so authors can group practice questions into ordered sections. Expose prerequisite_question_ids on read APIs for client-side gating, validate associations on save, and document admin panel integration. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package domain
|
|
|
|
import "testing"
|
|
|
|
func TestValidateQuestionSetAssociations(t *testing.T) {
|
|
anchor := int64(1)
|
|
associated := int64(2)
|
|
invalidAssoc := int64(99)
|
|
|
|
memberships := []QuestionSetMembership{
|
|
{QuestionID: 1, DisplayOrder: 1, AssociatedQuestionID: nil},
|
|
{QuestionID: 2, DisplayOrder: 2, AssociatedQuestionID: &anchor},
|
|
{QuestionID: 3, DisplayOrder: 3, AssociatedQuestionID: &anchor},
|
|
{QuestionID: 4, DisplayOrder: 4, AssociatedQuestionID: nil},
|
|
}
|
|
if err := ValidateQuestionSetAssociations(memberships); err != nil {
|
|
t.Fatalf("expected valid associations, got %v", err)
|
|
}
|
|
|
|
if err := ValidateQuestionSetAssociations([]QuestionSetMembership{
|
|
{QuestionID: 2, DisplayOrder: 2, AssociatedQuestionID: &associated},
|
|
}); err == nil {
|
|
t.Fatal("expected error when associating with non-anchor question")
|
|
}
|
|
|
|
if err := ValidateQuestionSetAssociations([]QuestionSetMembership{
|
|
{QuestionID: 2, DisplayOrder: 2, AssociatedQuestionID: &invalidAssoc},
|
|
}); err == nil {
|
|
t.Fatal("expected error when associated question is not in set")
|
|
}
|
|
}
|
|
|
|
func TestPrerequisiteQuestionIDs(t *testing.T) {
|
|
anchor := int64(1)
|
|
memberships := []QuestionSetMembership{
|
|
{QuestionID: 1, DisplayOrder: 1, AssociatedQuestionID: nil},
|
|
{QuestionID: 2, DisplayOrder: 2, AssociatedQuestionID: &anchor},
|
|
{QuestionID: 3, DisplayOrder: 3, AssociatedQuestionID: &anchor},
|
|
{QuestionID: 4, DisplayOrder: 4, AssociatedQuestionID: nil},
|
|
}
|
|
|
|
got := PrerequisiteQuestionIDs(memberships, 3)
|
|
if len(got) != 2 || got[0] != 1 || got[1] != 2 {
|
|
t.Fatalf("unexpected prerequisites for question 3: %#v", got)
|
|
}
|
|
|
|
got = PrerequisiteQuestionIDs(memberships, 4)
|
|
if len(got) != 3 || got[0] != 1 || got[1] != 2 || got[2] != 3 {
|
|
t.Fatalf("unexpected prerequisites for question 4: %#v", got)
|
|
}
|
|
}
|