|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "question-service/models" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +// tests partially generated using Github Copilot |
| 16 | + |
| 17 | +func createCreateRequestWithData(_ *testing.T, body []byte) *http.Request { |
| 18 | + req := httptest.NewRequest(http.MethodPost, "http://localhost:12345/questions", bytes.NewBuffer(body)) |
| 19 | + |
| 20 | + return req |
| 21 | +} |
| 22 | + |
| 23 | +func TestCreateQuestion(t *testing.T) { |
| 24 | + t.Run("Create new question", func(t *testing.T) { |
| 25 | + var err error |
| 26 | + |
| 27 | + newQuestion := models.Question{ |
| 28 | + Title: "New Question", |
| 29 | + Description: "New Description", |
| 30 | + Complexity: models.Medium, |
| 31 | + Categories: []string{"Category1"}, |
| 32 | + |
| 33 | + DocRefID: "a-doc-ref-id", |
| 34 | + } |
| 35 | + |
| 36 | + setupDb(t) |
| 37 | + beforeCount := getCount(t) |
| 38 | + |
| 39 | + w := httptest.NewRecorder() |
| 40 | + data, err := json.Marshal(newQuestion) |
| 41 | + assert.NoError(t, err) |
| 42 | + req := createCreateRequestWithData(t, data) |
| 43 | + service.CreateQuestion(w, req) |
| 44 | + afterCount := getCount(t) |
| 45 | + // Check response |
| 46 | + assert.Equal(t, http.StatusOK, w.Code) |
| 47 | + var response models.Question |
| 48 | + err = json.NewDecoder(w.Body).Decode(&response) |
| 49 | + assert.NoError(t, err) |
| 50 | + assert.Equal(t, newQuestion.Title, response.Title) |
| 51 | + assert.Equal(t, newQuestion.Description, response.Description) |
| 52 | + assert.Equal(t, newQuestion.Complexity, response.Complexity) |
| 53 | + assert.Equal(t, newQuestion.Categories, response.Categories) |
| 54 | + assert.Equal(t, beforeCount+1, afterCount) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("Create question with missing title", func(t *testing.T) { |
| 58 | + newQuestion := models.Question{ |
| 59 | + Description: "New Description", |
| 60 | + Complexity: models.Medium, |
| 61 | + Categories: []string{"Category1"}, |
| 62 | + } |
| 63 | + |
| 64 | + setupDb(t) |
| 65 | + beforeCount := getCount(t) |
| 66 | + |
| 67 | + w := httptest.NewRecorder() |
| 68 | + data, _ := json.Marshal(newQuestion) |
| 69 | + req := createCreateRequestWithData(t, data) |
| 70 | + service.CreateQuestion(w, req) |
| 71 | + |
| 72 | + // Check response |
| 73 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 74 | + assert.Contains(t, w.Body.String(), "Title is required") |
| 75 | + assert.Equal(t, beforeCount, getCount(t)) |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("Create question with duplicate title", func(t *testing.T) { |
| 79 | + newQuestion := models.Question{ |
| 80 | + Title: "Duplicate Title", |
| 81 | + Description: "New Description", |
| 82 | + Complexity: models.Medium, |
| 83 | + Categories: []string{"Category1"}, |
| 84 | + } |
| 85 | + |
| 86 | + setupDb(t) |
| 87 | + |
| 88 | + // Create the first question |
| 89 | + w := httptest.NewRecorder() |
| 90 | + data, _ := json.Marshal(newQuestion) |
| 91 | + req := createCreateRequestWithData(t, data) |
| 92 | + service.CreateQuestion(w, req) |
| 93 | + assert.Equal(t, http.StatusOK, w.Code) |
| 94 | + |
| 95 | + // Try to create the second question with the same title |
| 96 | + w = httptest.NewRecorder() |
| 97 | + req = createCreateRequestWithData(t, data) |
| 98 | + service.CreateQuestion(w, req) |
| 99 | + |
| 100 | + // Check response |
| 101 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 102 | + assert.Contains(t, w.Body.String(), "Question title already exists") |
| 103 | + }) |
| 104 | + |
| 105 | + t.Run("Create question with empty description", func(t *testing.T) { |
| 106 | + newQuestion := models.Question{ |
| 107 | + Title: "New Question", |
| 108 | + Description: "", |
| 109 | + Complexity: models.Medium, |
| 110 | + Categories: []string{"Category1"}, |
| 111 | + } |
| 112 | + |
| 113 | + setupDb(t) |
| 114 | + |
| 115 | + w := httptest.NewRecorder() |
| 116 | + data, _ := json.Marshal(newQuestion) |
| 117 | + req := createCreateRequestWithData(t, data) |
| 118 | + service.CreateQuestion(w, req) |
| 119 | + |
| 120 | + // Check response |
| 121 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 122 | + assert.Contains(t, w.Body.String(), "Description is required") |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("Create question with nil title", func(t *testing.T) { |
| 126 | + newQuestion := models.Question{ |
| 127 | + // Title: "New Question", |
| 128 | + Description: "New Description", |
| 129 | + Complexity: models.Medium, |
| 130 | + Categories: []string{"Category1"}, |
| 131 | + } |
| 132 | + |
| 133 | + setupDb(t) |
| 134 | + |
| 135 | + w := httptest.NewRecorder() |
| 136 | + data, _ := json.Marshal(newQuestion) |
| 137 | + req := createCreateRequestWithData(t, data) |
| 138 | + service.CreateQuestion(w, req) |
| 139 | + |
| 140 | + // Check response |
| 141 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 142 | + assert.Contains(t, w.Body.String(), "Title is required") |
| 143 | + }) |
| 144 | +} |
0 commit comments