package examprep import ( "context" "errors" "testing" "Rezina-Backend/internal/domain" "github.com/jackc/pgx/v5" ) type quizStore struct { examPrepStore set domain.QuestionSet created bool input domain.CreateExamPrepPracticeInput parents []domain.ExamPrepPracticeParent } func (s *quizStore) GetQuestionSetByID(context.Context, int64) (domain.QuestionSet, error) { if s.set.ID == 0 { return domain.QuestionSet{}, pgx.ErrNoRows } return s.set, nil } func (s *quizStore) GetExamPrepUnitByID(context.Context, int64) (domain.ExamPrepUnit, error) { return domain.ExamPrepUnit{ID: 7}, nil } func (s *quizStore) CreateExamPrepPractice(_ context.Context, in domain.CreateExamPrepPracticeInput, parents []domain.ExamPrepPracticeParent) (domain.ExamPrepPractice, error) { s.created = true s.input = in s.parents = parents return domain.ExamPrepPractice{ID: 10, QuestionSetID: in.QuestionSetID, Parents: domain.ExamPrepPracticeParentsPtr(parents)}, nil } func TestChapterQuizCreation(t *testing.T) { tests := []struct { name string setType string override bool want error }{ {name: "quiz defaults to draft", setType: "QUIZ"}, {name: "practice is rejected", setType: "PRACTICE", want: ErrExamPrepQuizRequiresQuizSet}, {name: "exam is rejected", setType: "EXAM", want: ErrExamPrepQuizRequiresQuizSet}, {name: "URL parent cannot be overridden", setType: "QUIZ", override: true, want: ErrInvalidExamPrepPracticeParent}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { store := &quizStore{set: domain.QuestionSet{ID: 3, SetType: tc.setType}} in := domain.CreateExamPrepPracticeInput{QuestionSetID: 3} if tc.override { in.ParentKind = domain.ExamPrepParentKindLesson in.ParentID = 99 } _, err := NewService(store).CreateChapterQuiz(context.Background(), 7, in) if !errors.Is(err, tc.want) { t.Fatalf("error=%v want=%v", err, tc.want) } if tc.want != nil { if store.created { t.Fatal("invalid quiz was persisted") } return } if store.input.PublishStatus != "DRAFT" { t.Fatal("quiz should default to DRAFT") } if len(store.parents) != 1 || store.parents[0].Kind != domain.ExamPrepParentKindUnit || store.parents[0].ID != 7 { t.Fatalf("parents=%v", store.parents) } }) } } func TestQuizCannotBeLinkedOutsideOneChapter(t *testing.T) { store := &quizStore{set: domain.QuestionSet{ID: 3, SetType: "QUIZ"}} service := NewService(store) for _, parents := range [][]domain.ExamPrepPracticeParent{ nil, {{Kind: domain.ExamPrepParentKindCatalogCourse, ID: 7}}, {{Kind: domain.ExamPrepParentKindLesson, ID: 7}}, {{Kind: domain.ExamPrepParentKindUnit, ID: 7}, {Kind: domain.ExamPrepParentKindUnit, ID: 8}}, } { _, err := service.createExamPrepPractice(context.Background(), parents, domain.CreateExamPrepPracticeInput{QuestionSetID: 3}) if !errors.Is(err, ErrInvalidExamPrepPracticeParent) { t.Fatalf("parents=%v error=%v", parents, err) } } if store.created { t.Fatal("invalid quiz was persisted") } }