-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
214 lines (169 loc) · 5.68 KB
/
main_test.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
// TestCreateTask tests the POST /tasks API
func TestCreateTask(t *testing.T) {
router := setupRouter()
task := Task{
Title: "Test Task",
Description: "This is a test task",
DueDate: parseTime("2024-12-31T23:59:59Z"),
}
w := httptest.NewRecorder()
body, _ := json.Marshal(task)
req, _ := http.NewRequest("POST", "/tasks", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
var createdTask Task
json.Unmarshal(w.Body.Bytes(), &createdTask)
assert.Equal(t, "Test Task", createdTask.Title)
}
// TestGetAllTasks tests the GET /tasks API
func TestGetAllTasks(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/tasks", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var tasks []Task
json.Unmarshal(w.Body.Bytes(), &tasks)
assert.GreaterOrEqual(t, len(tasks), 0)
}
// TestGetTaskByID tests the GET /tasks/:id API
func TestGetTaskByID(t *testing.T) {
router := setupRouter()
// First, create a task to get its ID
task := Task{
Title: "Test Task for Get",
Description: "This is a task for testing Get",
DueDate: parseTime("2024-12-31T23:59:59Z"),
}
w := httptest.NewRecorder()
body, _ := json.Marshal(task)
req, _ := http.NewRequest("POST", "/tasks", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
var createdTask Task
json.Unmarshal(w.Body.Bytes(), &createdTask)
// Now, get the task by ID
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/tasks/"+strconv.Itoa(createdTask.ID), nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var fetchedTask Task
json.Unmarshal(w.Body.Bytes(), &fetchedTask)
assert.Equal(t, createdTask.ID, fetchedTask.ID)
}
// TestUpdateTask tests the PUT /tasks/:id API
func TestUpdateTask(t *testing.T) {
router := setupRouter()
// First, create a task to update
task := Task{
Title: "Test Task for Update",
Description: "This is a task for testing Update",
DueDate: parseTime("2024-12-31T23:59:59Z"),
}
w := httptest.NewRecorder()
body, _ := json.Marshal(task)
req, _ := http.NewRequest("POST", "/tasks", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
var createdTask Task
json.Unmarshal(w.Body.Bytes(), &createdTask)
// Now, update the task
updatedTask := Task{
Title: "Updated Task Title",
Description: "Updated description",
DueDate: parseTime("2024-12-31T23:59:59Z"),
Status: "in progress",
}
w = httptest.NewRecorder()
body, _ = json.Marshal(updatedTask)
req, _ = http.NewRequest("PUT", "/tasks/"+strconv.Itoa(createdTask.ID), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var taskAfterUpdate Task
json.Unmarshal(w.Body.Bytes(), &taskAfterUpdate)
assert.Equal(t, "Updated Task Title", taskAfterUpdate.Title)
assert.Equal(t, "in progress", taskAfterUpdate.Status)
}
// TestDeleteTask tests the DELETE /tasks/:id API
func TestDeleteTask(t *testing.T) {
router := setupRouter()
// First, create a task to delete
task := Task{
Title: "Test Task for Delete",
Description: "This is a task for testing Delete",
DueDate: parseTime("2024-12-31T23:59:59Z"),
}
w := httptest.NewRecorder()
body, _ := json.Marshal(task)
req, _ := http.NewRequest("POST", "/tasks", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
var createdTask Task
json.Unmarshal(w.Body.Bytes(), &createdTask)
// Now, delete the task by ID
w = httptest.NewRecorder()
req, _ = http.NewRequest("DELETE", "/tasks/"+strconv.Itoa(createdTask.ID), nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
// Try to get the task again to verify deletion
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/tasks/"+strconv.Itoa(createdTask.ID), nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
// TestMarkTaskComplete tests the PATCH /tasks/:id/complete API
func TestMarkTaskComplete(t *testing.T) {
router := setupRouter()
// First, create a task to mark complete
task := Task{
Title: "Test Task for Complete",
Description: "This is a task for testing Complete",
DueDate: parseTime("2024-12-31T23:59:59Z"),
}
w := httptest.NewRecorder()
body, _ := json.Marshal(task)
req, _ := http.NewRequest("POST", "/tasks", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
var createdTask Task
json.Unmarshal(w.Body.Bytes(), &createdTask)
// Now, mark the task as complete
w = httptest.NewRecorder()
req, _ = http.NewRequest("PATCH", "/tasks/"+strconv.Itoa(createdTask.ID)+"/complete", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var completedTask Task
json.Unmarshal(w.Body.Bytes(), &completedTask)
assert.Equal(t, "complete", completedTask.Status)
}
// Helper function to set up the router
func setupRouter() *gin.Engine {
gin.SetMode(gin.TestMode)
router := gin.Default()
router.POST("/tasks", createTask)
router.GET("/tasks", getAllTasks)
router.GET("/tasks/:id", getTaskByID)
router.PUT("/tasks/:id", updateTask)
router.DELETE("/tasks/:id", deleteTask)
router.PATCH("/tasks/:id/complete", markTaskComplete)
return router
}
// Helper function to parse time
func parseTime(timeStr string) time.Time {
t, _ := time.Parse(time.RFC3339, timeStr)
return t
}