-
Notifications
You must be signed in to change notification settings - Fork 0
/
category_test.go
223 lines (172 loc) · 6.41 KB
/
category_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
215
216
217
218
219
220
221
222
223
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
)
func TestEmptyCategoriesTable(t *testing.T) {
clearTables()
req, _ := http.NewRequest("GET", "/categories", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
if body := response.Body.String(); body != "[]" {
t.Errorf("Expected an empty array. Got %s", body)
}
}
func TestGetNonExistentCategory(t *testing.T) {
clearTables()
req, _ := http.NewRequest("GET", "/category/b119178b-2fd2-4a5c-9301-190c341df180", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
var m map[string]string
json.Unmarshal(response.Body.Bytes(), &m)
if m["error"] != "Category not found" {
t.Errorf("Expected the 'error' key of the response to be set to 'Category not found'. Got %s", m["error"])
}
}
func TestCreateCategory(t *testing.T) {
clearTables()
jsonString := []byte(`{"name":"Test category","description": "Test description"}`)
req, _ := http.NewRequest("POST", "/category", bytes.NewBuffer(jsonString))
req.Header.Set("Content-Type", "application/json")
response := executeRequest(req)
checkResponseCode(t, http.StatusCreated, response.Code)
var m map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &m)
if m["name"] != "Test category" {
t.Errorf("Expected name of category to be 'Test category'. Got %s", m["name"])
}
if m["description"] != "Test description" {
t.Errorf("Expected name of category to be 'Test description'. Got %s", m["description"])
}
}
func TestFetchCategory(t *testing.T) {
clearTables()
categoryId := addCategory()
req, _ := http.NewRequest("GET", fmt.Sprintf("/category/%v", categoryId), nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
}
func TestFetchAllCategories(t *testing.T) {
clearTables()
addCategories(3)
req, _ := http.NewRequest("GET", "/categories", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var categories []category
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&categories); err != nil {
t.Errorf("Could not read categories data from request response. Error: %v", err)
}
if len(categories) != 3 {
t.Errorf("Expected to receive 3 categories. Got %v", len(categories))
}
}
func TestUpdateCategory(t *testing.T) {
clearTables()
categoryId := addCategory()
req, _ := http.NewRequest("GET", fmt.Sprintf("/category/%v", categoryId), nil)
response := executeRequest(req)
var originalCategory map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &originalCategory)
jsonString := []byte(`{"name": "Updated name", "description": "Updated description"}`)
req, _ = http.NewRequest("PUT", fmt.Sprintf("/category/%v", categoryId), bytes.NewBuffer(jsonString))
req.Header.Set("Content-Type", "application/json")
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var updatedCategory map[string]interface{}
json.Unmarshal(response.Body.Bytes(), &updatedCategory)
if updatedCategory["category_id"] != originalCategory["category_id"] {
t.Errorf("Expected ID to be %v. Got %v", originalCategory["category_id"], updatedCategory["category_id"])
}
if updatedCategory["name"] == originalCategory["name"] {
t.Errorf(
"Expected name to change from %v to %v. Got %v",
originalCategory["name"],
"Updated name",
updatedCategory["name"],
)
}
if updatedCategory["description"] == originalCategory["description"] {
t.Errorf(
"Expected description to change from %v to %v. Got %v",
originalCategory["description"],
"Updated description",
updatedCategory["description"],
)
}
}
func TestDeleteCategory(t *testing.T) {
clearTables()
categoryId := addCategory()
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/category/%v", categoryId), nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", fmt.Sprintf("/category/%v", categoryId), nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
}
// task related tests:
func TestDeleteCategoryWithTasks(t *testing.T) {
clearTables()
categoryId := addCategory()
addTasksToCategory(categoryId, 3)
// delete the category
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/category/%v", categoryId), nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
req, _ = http.NewRequest("GET", fmt.Sprintf("/category/%v", categoryId), nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
// check that tasks were deleted
req, _ = http.NewRequest("GET", fmt.Sprintf("/category/%v/tasks", categoryId), nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
var tasks []task
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&tasks); err != nil {
t.Errorf("Could not read tasks data from request response. Error: %v", err)
}
if len(tasks) != 0 {
t.Errorf("Expected to receive 0 tasks under category_id %v. Got %v", categoryId, len(tasks))
}
}
// handle it on the front-end and
// func TestUpdateCategoryTaskOrdering(t *testing.T) {
// clearTables()
// categoryId := addCategory()
// addTasksToCategory(categoryId, 5)
// // get all tasks check their order
// req, _ := http.NewRequest("GET", fmt.Sprintf("/category/%v/tasks", categoryId), nil)
// response := executeRequest(req)
// checkResponseCode(t, http.StatusOK, response.Code)
// var tasks []task
// decoder := json.NewDecoder(response.Body)
// if err := decoder.Decode(&tasks); err != nil {
// t.Errorf("Could not read tasks data from request response. Error: %v", err)
// }
// var originalTasksListOrder []string
// for _, t := range tasks {
// originalTasksListOrder = append(originalTasksListOrder, t.Task)
// }
// fmt.Println(originalTasksListOrder)
// fmt.Println(tasks)
// // reverse the sequence of the tasks
// for i := 0; i < len(tasks); i++ {
// j := len(tasks) - i
// tasks[i].Seq = j
// }
// fmt.Println(tasks)
// // update order
// jsonString, err := json.Marshal(tasks)
// if err != nil {
// t.Errorf("Could not encode tasks list back into json. Error: %v", err)
// }
// req, _ = http.NewRequest("PUT", "/category/1/tasks", bytes.NewBuffer(jsonString))
// executeRequest(req)
// checkResponseCode(t, http.StatusOK, response.Code)
// // get all tasks and check their order
// t.Error("Not Implemented.")
// }