1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package handlers
import (
"database/sql"
"demoon/config"
"demoon/internal/models"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetQuestionHandler(t *testing.T) {
t.Run("get existing question", func(t *testing.T) {
// Setup mock repository
mockRepo := &MockRepo{}
handlers := NewHandlers(config.Config{}, slog.Default(), mockRepo)
// Test request/response
req := httptest.NewRequest("GET", "/question/1", nil)
w := httptest.NewRecorder()
// Call handler directly
GetQuestion(w, req)
// Verify response
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
// Add more assertions about response body
})
}
type MockRepo struct {
questions map[uint32]*models.Question
}
func (m *MockRepo) DBGetQuestion(id string) (*models.Question, error) {
if q, ok := m.questions[1]; ok {
return q, nil
}
return nil, sql.ErrNoRows
}
func (m *MockRepo) DBGetDefaultsMap() (map[string]string, error) {
return map[string]string{}, nil
}
|