-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler_test.go
More file actions
370 lines (354 loc) · 20.1 KB
/
Copy pathhandler_test.go
File metadata and controls
370 lines (354 loc) · 20.1 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package tests
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/stretchr/testify/assert"
"github.com/codehand/echo-restful-crud-api-example/handler"
"github.com/codehand/echo-restful-crud-api-example/middlewares"
"github.com/codehand/echo-restful-crud-api-example/types"
)
// TestGetAllProducts is func test get all product - all case
func TestGetAllProducts(t *testing.T) {
e := echo.New()
e.Validator = middlewares.InitCustomValidator()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
}))
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
tests := []struct {
name string
data []*types.Product
isError types.PayloadStatus
}{
{
name: "Case 1: get all products",
data: nil,
isError: types.OkStatus,
},
}
for _, test := range tests {
assert.NotEmpty(t, test.name, "Name testing invalid")
req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
c.SetPath("/api/v1/products")
err := handler.GetProducts(c)
if test.isError.HasError() {
assert.Error(t, err, test.name)
assert.NotEqual(t, http.StatusOK, res.Code, test.name)
var es *types.PayloadStatus
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &es), "Parse body error")
assert.NotEmpty(t, es.Code, "Code error empty")
assert.NotEmpty(t, es.Message, "Message empty")
} else {
assert.NoError(t, err, test.name)
assert.Equal(t, http.StatusOK, res.Code, test.name)
var data []*types.Product
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &data), "Parse body error")
assert.NotEqual(t, 0, len(data), test.name)
for _, item := range data {
assert.NotEmpty(t, item.Name, test.name)
assert.NotEmpty(t, item.ImageClosed, test.name)
assert.NotEmpty(t, item.ImageOpen, test.name)
assert.NotEmpty(t, item.Description, test.name)
assert.NotEmpty(t, item.Story, test.name)
assert.NotEmpty(t, item.AllergyInfo, test.name)
assert.NotEmpty(t, item.DietaryCertifications, test.name)
assert.NotEqual(t, 0, item.ProductID, test.name)
}
}
}
}
// TestGetOneProduct is func test get one product - all case
func TestGetOneProduct(t *testing.T) {
e := echo.New()
e.Validator = middlewares.InitCustomValidator()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
}))
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
tests := []struct {
name string
input int
output *types.Product
isError types.PayloadStatus
}{
{
name: "Case 1: get one products - OK",
input: 1,
output: &types.Product{
Name: "Vanilla Toffee Bar Crunch",
Description: "Vanilla Ice Cream with Fudge-Covered Toffee Pieces",
SourcingValues: []string{"Non-GMO", "Cage-Free Eggs", "Fairtrade", "Responsibly Sourced Packaging", "Caring Dairy"},
AllergyInfo: "may contain wheat, peanuts and other tree nuts",
DietaryCertifications: "Kosher",
ProductID: "1",
},
isError: types.OkStatus,
},
{
name: "Case 2: get one products - FAIL",
input: 0,
output: nil,
isError: types.ParseStatus("NOT_FOUND", "not found"),
},
{
name: "Case 3: get one products - FAIL",
input: -1,
output: nil,
isError: types.ParseStatus("NOT_FOUND", "not found"),
},
{
name: "Case 4: get one products - FAIL",
input: 999999999,
output: nil,
isError: types.ParseStatus("NOT_FOUND", "not found"),
},
}
for _, test := range tests {
assert.NotEmpty(t, test.name, "Name testing invalid")
req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
c.SetPath("/api/v1/products/:id")
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", test.input))
err := handler.GetProduct(c)
if test.isError.HasError() {
assert.NoError(t, err, test.name)
assert.NotEqual(t, http.StatusOK, res.Code, test.name)
var es *types.PayloadStatus
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &es), "Parse body error")
assert.NotEmpty(t, es.Code, "Code error empty")
assert.NotEmpty(t, es.Message, "Message empty")
assert.Equal(t, test.isError.Code, es.Code, "Code not match")
} else {
assert.NoError(t, err, test.name)
assert.Equal(t, http.StatusOK, res.Code, test.name)
var data *types.Product
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &data), "Parse body error")
assert.NotEmpty(t, data.Name, test.name)
assert.NotEmpty(t, data.ImageClosed, test.name)
assert.NotEmpty(t, data.ImageOpen, test.name)
assert.NotEmpty(t, data.Description, test.name)
assert.NotEmpty(t, data.Story, test.name)
assert.NotEmpty(t, data.AllergyInfo, test.name)
assert.NotEmpty(t, data.DietaryCertifications, test.name)
assert.NotEqual(t, 0, data.ProductID, test.name)
assert.Equal(t, test.output.Name, data.Name, test.name)
assert.Equal(t, test.output.Description, data.Description, test.name)
assert.Equal(t, test.output.ProductID, data.ProductID, test.name)
assert.Equal(t, test.output.AllergyInfo, data.AllergyInfo, test.name)
assert.Equal(t, test.output.DietaryCertifications, data.DietaryCertifications, test.name)
for _, item := range test.output.SourcingValues {
assert.Contains(t, data.SourcingValues, item, test.name)
}
}
}
}
// TestCreateOneProduct is func test create one product - all case
func TestCreateOneProduct(t *testing.T) {
e := echo.New()
e.Validator = middlewares.InitCustomValidator()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
}))
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
tests := []struct {
name string
input []byte
output *types.Product
isError types.PayloadStatus
}{
{
name: "Case 1: create one products - OK",
input: []byte(`{"name":"Vanilla Toffee Bar Crunch","image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"Kosher"}`),
output: &types.Product{
Name: "Vanilla Toffee Bar Crunch",
Description: "Vanilla Ice Cream with Fudge-Covered Toffee Pieces",
SourcingValues: []string{"Non-GMO", "Cage-Free Eggs", "Fairtrade", "Responsibly Sourced Packaging", "Caring Dairy"},
AllergyInfo: "may contain wheat, peanuts and other tree nuts",
DietaryCertifications: "Kosher",
},
isError: types.OkStatus,
},
{
name: "Case 2: create one products - missing field",
input: []byte(`{"name":"","image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"Kosher"}`),
output: nil,
isError: types.ParseStatus("REQ_INVALID", "Vui lòng nhập giá trị Name"),
},
{
name: "Case 3: create one products - missing field",
input: []byte(`{"name":"name","image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":""}`),
output: nil,
isError: types.ParseStatus("REQ_INVALID", "Vui lòng nhập giá trị DietaryCertifications"),
},
{
name: "Case 4: create one products - incorrect body",
input: []byte(`{"name":11,"image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"test"}`),
output: nil,
isError: types.ParseStatus("REQ_ERR", "Có lỗi xảy ra, vui lòng kiểm tra lại thông tin"),
},
}
for _, test := range tests {
assert.NotEmpty(t, test.name, "Name testing invalid")
req := httptest.NewRequest(echo.POST, "/", strings.NewReader(string(test.input)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
c.SetPath("/api/v1/products")
err := handler.CreateProduct(c)
if test.isError.HasError() {
assert.NoError(t, err, test.name)
assert.NotEqual(t, http.StatusCreated, res.Code, test.name)
var es *types.PayloadStatus
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &es), "Parse body error")
assert.NotEmpty(t, es.Code, "Code error empty")
assert.NotEmpty(t, es.Message, "Message empty")
assert.Equal(t, test.isError.Code, es.Code, "Code not match")
assert.Equal(t, test.isError.Message, es.Message, "Msg not match")
} else {
assert.NoError(t, err, test.name)
assert.Equal(t, http.StatusCreated, res.Code, test.name)
var data *types.Product
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &data), "Parse body error")
assert.NotEmpty(t, data.Name, test.name)
assert.NotEmpty(t, data.ImageClosed, test.name)
assert.NotEmpty(t, data.ImageOpen, test.name)
assert.NotEmpty(t, data.Description, test.name)
assert.NotEmpty(t, data.Story, test.name)
assert.NotEmpty(t, data.AllergyInfo, test.name)
assert.NotEmpty(t, data.DietaryCertifications, test.name)
assert.NotEqual(t, 0, data.ProductID, test.name)
assert.Equal(t, test.output.Name, data.Name, test.name)
assert.Equal(t, test.output.Description, data.Description, test.name)
assert.Equal(t, test.output.AllergyInfo, data.AllergyInfo, test.name)
assert.Equal(t, test.output.DietaryCertifications, data.DietaryCertifications, test.name)
for _, item := range test.output.SourcingValues {
assert.Contains(t, data.SourcingValues, item, test.name)
}
}
}
}
// TestUpdateOneProduct is func test update one product - all case
func TestUpdateOneProduct(t *testing.T) {
e := echo.New()
e.Validator = middlewares.InitCustomValidator()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
}))
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
tests := []struct {
name string
id int
input []byte
output *types.Product
isError types.PayloadStatus
}{
{
name: "Case 1: update one products - OK",
id: 4,
input: []byte(`{"name":"Vanilla Toffee Bar Crunch","image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"Kosher"}`),
output: &types.Product{
Name: "Vanilla Toffee Bar Crunch",
Description: "Vanilla Ice Cream with Fudge-Covered Toffee Pieces",
SourcingValues: []string{"Non-GMO", "Cage-Free Eggs", "Fairtrade", "Responsibly Sourced Packaging", "Caring Dairy"},
AllergyInfo: "may contain wheat, peanuts and other tree nuts",
DietaryCertifications: "Kosher",
},
isError: types.OkStatus,
},
{
name: "Case 2: update one products - OK",
id: 4,
input: []byte(`{"name":"name","image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"Kosher"}`),
output: &types.Product{
Name: "name",
Description: "Vanilla Ice Cream with Fudge-Covered Toffee Pieces",
SourcingValues: []string{"Non-GMO", "Cage-Free Eggs", "Fairtrade", "Responsibly Sourced Packaging", "Caring Dairy"},
AllergyInfo: "may contain wheat, peanuts and other tree nuts",
DietaryCertifications: "Kosher",
},
isError: types.OkStatus,
},
{
name: "Case 3: update one products - OK",
id: 4,
input: []byte(`{"image_closed":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing.png","image_open":"/files/live/sites/systemsite/files/flavors/products/us/pint/open-closed-pints/vanilla-toffee-landing-open.png","description":"Vanilla Ice Cream with Fudge-Covered Toffee Pieces","story":"Vanilla What Bar Crunch? We gave this flavor a new name to go with the new toffee bars we’re using as part of our commitment to source Fairtrade Certified and non-GMO ingredients. We love it and know you will too!","sourcing_values":["Non-GMO","Cage-Free Eggs","Fairtrade","Responsibly Sourced Packaging","Caring Dairy"],"ingredients":["cream","skim milk","liquid sugar","water","sugar","coconut oil","egg yolks","butter","vanilla extract","almonds","cocoa (processed with alkali)","milk","soy lecithin","cocoa","natural flavor","salt","vegetable oil","guar gum","carrageenan"],"allergy_info":"may contain wheat, peanuts and other tree nuts","dietary_certifications":"Kosher"}`),
output: &types.Product{
Name: "name",
Description: "Vanilla Ice Cream with Fudge-Covered Toffee Pieces",
SourcingValues: []string{"Non-GMO", "Cage-Free Eggs", "Fairtrade", "Responsibly Sourced Packaging", "Caring Dairy"},
AllergyInfo: "may contain wheat, peanuts and other tree nuts",
DietaryCertifications: "Kosher",
},
isError: types.OkStatus,
},
}
for _, test := range tests {
assert.NotEmpty(t, test.name, "Name testing invalid")
req := httptest.NewRequest(echo.PUT, "/", strings.NewReader(string(test.input)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
c.SetPath("/api/v1/products/:id")
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", test.id))
err := handler.UpdateProduct(c)
if test.isError.HasError() {
assert.NoError(t, err, test.name)
assert.NotEqual(t, http.StatusOK, res.Code, test.name)
var es *types.PayloadStatus
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &es), "Parse body error")
assert.NotEmpty(t, es.Code, "Code error empty")
assert.NotEmpty(t, es.Message, "Message empty")
assert.Equal(t, test.isError.Code, es.Code, "Code not match")
assert.Equal(t, test.isError.Message, es.Message, "Msg not match")
} else {
assert.NoError(t, err, test.name)
assert.Equal(t, http.StatusOK, res.Code, test.name)
var data *types.Product
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "Parse body error")
assert.NoError(t, json.Unmarshal(body, &data), "Parse body error")
assert.NotEmpty(t, data.Name, test.name)
assert.NotEmpty(t, data.ImageClosed, test.name)
assert.NotEmpty(t, data.ImageOpen, test.name)
assert.NotEmpty(t, data.Description, test.name)
assert.NotEmpty(t, data.Story, test.name)
assert.NotEmpty(t, data.AllergyInfo, test.name)
assert.NotEmpty(t, data.DietaryCertifications, test.name)
assert.NotEqual(t, 0, data.ProductID, test.name)
assert.Equal(t, test.output.Name, data.Name, test.name)
assert.Equal(t, test.output.Description, data.Description, test.name)
assert.Equal(t, test.output.AllergyInfo, data.AllergyInfo, test.name)
assert.Equal(t, test.output.DietaryCertifications, data.DietaryCertifications, test.name)
}
}
}