-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee_test.go
297 lines (217 loc) · 7.81 KB
/
employee_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
/* Employee Table CRUD Test */
func TestAddEmployee(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var result map[string]interface{}
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.POST("/api/employee/:name/:department", AddEmployee)
w := httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/employee/Test Employee/Test Department", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &result)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
db.Where("Employee_Name = ?", "Test Employee").Association("Employee_Departments").Clear()
db.Unscoped().Where("Employee_Name=?", "Test Employee").Delete(&Employee{})
db.Unscoped().Where("Department_Name=?", "Test Department").Delete(&Department{})
}
func TestReadEmployee(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var results []Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/", ReadEmployee)
w := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/employee/", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &results)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
//fmt.Println(results)
}
func TestReadEmployeePaging(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var results []Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/", ReadEmployee)
w := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/employee/?page=2&limit=2", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &results)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestReadEmployeeInvalidPaging(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/", ReadEmployee)
w := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/employee/?page=-1&limit=-1", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
func TestUpdateEmployee(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var result Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.PUT("/api/employee/:id/:new", UpdateEmployee)
// Create Data for Test
newName := "New Name"
newEmployee := Employee{
Employee_Name: "Old Name",
}
db.Create(&newEmployee)
w := httptest.NewRecorder()
requrl := "/api/employee/" + strconv.FormatUint(uint64(newEmployee.ID), 10) + "/" + newName
request, _ := http.NewRequest("PUT", requrl, nil) // id=1인 employee의 name을 UJS로 UPDATE
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
db.Where("id = ?", newEmployee.ID).Find(&result)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "New Name", result.Employee_Name)
db.Unscoped().Where("id = ?", newEmployee.ID).Delete(&Employee{})
}
func TestDeleteEmployee(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var result Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.DELETE("/api/employee/:id", DeleteEmployee)
test := Employee{ // 지울 data 정보
Employee_Name: "deleteTest",
}
createResult := db.Create(&test) // 지울 data 넣기
assert.NoError(t, createResult.Error)
w := httptest.NewRecorder()
requrl := "/api/employee/" + strconv.FormatUint(uint64(test.ID), 10)
request, _ := http.NewRequest("DELETE", requrl, nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
db.Where("id=?", test.ID).Find(&result)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, uint(0), result.ID) // 삭제 후에는 id = 0 으로 조회됨
}
func TestDeleteEmployeeInvalidEid(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.DELETE("/api/employee/:id", DeleteEmployee)
w := httptest.NewRecorder()
request, _ := http.NewRequest("DELETE", "/api/employee/-1", nil) // Use invalid department id
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
func TestSearchEmployeeByName(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var results []Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/name/:name", SearchEmployeeByName)
w := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/employee/name/Test Employee", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &results)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
//fmt.Println(results)
}
func TestSearchEmployeeByDay(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var results []Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/day/:days", SearchEmployeeByDay)
days := 4
w := httptest.NewRecorder()
requrl := "/api/employee/day/" + strconv.Itoa(days)
request, _ := http.NewRequest("GET", requrl, nil) // Search Department which name is 'A'
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &results)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
//fmt.Println(results)
}
func TestSearchEmployeeByDayInvalidPaging(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/day/:days", SearchEmployeeByDay)
days := 4
w := httptest.NewRecorder()
requrl := "/api/employee/day/" + strconv.Itoa(days) + "?page=-1&limit=-1"
request, _ := http.NewRequest("GET", requrl, nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
func TestSearchEmployeeByDayPaging(t *testing.T) {
err = InitDB()
assert.NoError(t, err)
var results []Employee
token, err := GenerateToken("gotest", "myCA")
assert.NoError(t, err)
router := gin.Default()
router.Use(AuthorizeAccount())
router.GET("/api/employee/day/:days", SearchEmployeeByDay)
days := 4
w := httptest.NewRecorder()
requrl := "/api/employee/day/" + strconv.Itoa(days) + "?page=1&limit=1" //하나는 data가 있을 것이라고 가정
request, _ := http.NewRequest("GET", requrl, nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, request)
err = json.Unmarshal(w.Body.Bytes(), &results)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, 1, len(results))
}