package handlers import ( "context" "net/http/httptest" "strings" "testing" "Rezina-Backend/internal/domain" "Rezina-Backend/internal/ports" "Rezina-Backend/internal/services/questions" "github.com/gofiber/fiber/v2" "github.com/jackc/pgx/v5" ) type quizReplacementStore struct { ports.QuestionStore setType string } func (s quizReplacementStore) GetQuestionSetByID(context.Context, int64) (domain.QuestionSet, error) { if s.setType == "" { return domain.QuestionSet{}, pgx.ErrNoRows } return domain.QuestionSet{ID: 12, SetType: s.setType}, nil } func TestQuizFullUpdateRejectsInvalidReplacementBeforeWriting(t *testing.T) { for _, tc := range []struct { name, body, setType string want int }{ {"malformed request", `{`, "QUIZ", 400}, {"practice set", `{"practice":{"question_set_id":12}}`, "PRACTICE", 400}, {"exam set", `{"practice":{"question_set_id":12}}`, "EXAM", 400}, {"missing set", `{"practice":{"question_set_id":12}}`, "", 404}, } { t.Run(tc.name, func(t *testing.T) { h := &Handler{questionsSvc: questions.NewService(quizReplacementStore{setType: tc.setType})} app := fiber.New() app.Put("/quizzes/:id/full", h.UpdateChapterQuizFull) request := httptest.NewRequest("PUT", "/quizzes/9/full", strings.NewReader(tc.body)) request.Header.Set("Content-Type", "application/json") response, err := app.Test(request) if err != nil { t.Fatal(err) } defer response.Body.Close() if response.StatusCode != tc.want { t.Fatalf("status=%d want=%d", response.StatusCode, tc.want) } }) } }