@@ -9,127 +9,239 @@ import (
9
9
10
10
"github.com/dieg0code/serverles-api-scraper/api/data/request"
11
11
"github.com/dieg0code/shared/json/response"
12
+ "github.com/dieg0code/shared/mocks"
12
13
"github.com/gin-gonic/gin"
13
14
"github.com/stretchr/testify/assert"
14
- "github.com/stretchr/testify/mock"
15
15
)
16
16
17
- type MockProductService struct {
18
- mock.Mock
19
- }
17
+ func TestProductController_GetAll (t * testing.T ) {
18
+ t .Run ("GetAll_Success" , func (t * testing.T ) {
19
+ gin .SetMode (gin .TestMode )
20
+ mockService := new (mocks.MockProductService )
21
+ productController := NewProductControllerImpl (mockService )
22
+
23
+ router := gin .Default ()
24
+ router .GET ("/products" , productController .GetAll )
25
+
26
+ mockService .On ("GetAll" ).Return ([]response.ProductResponse {
27
+ {
28
+ ProductID : "test-id" ,
29
+ Name : "Test Product" ,
30
+ Category : "Test Category" ,
31
+ OriginalPrice : 100 ,
32
+ DiscountedPrice : 90 ,
33
+ },
34
+ {
35
+ ProductID : "test-id-2" ,
36
+ Name : "Test Product 2" ,
37
+ Category : "Test Category 2" ,
38
+ OriginalPrice : 200 ,
39
+ DiscountedPrice : 180 ,
40
+ },
41
+ }, nil )
42
+
43
+ req , err := http .NewRequest (http .MethodGet , "/products" , nil )
44
+ assert .NoError (t , err , "Expected no error creating request" )
45
+
46
+ rec := httptest .NewRecorder ()
47
+ router .ServeHTTP (rec , req )
48
+
49
+ assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200" )
50
+
51
+ var response response.BaseResponse
52
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
53
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
54
+ assert .Equal (t , 200 , response .Code , "Response code should be 200" )
55
+ assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
56
+ })
20
57
21
- func (m * MockProductService ) GetAll () ([]response.ProductResponse , error ) {
22
- args := m .Called ()
23
- return args .Get (0 ).([]response.ProductResponse ), args .Error (1 )
24
- }
25
- func (m * MockProductService ) GetByID (productID string ) (response.ProductResponse , error ) {
26
- args := m .Called (productID )
27
- return args .Get (0 ).(response.ProductResponse ), args .Error (1 )
28
- }
29
- func (m * MockProductService ) UpdateData (updateData request.UpdateDataRequest ) (bool , error ) {
30
- args := m .Called (updateData )
31
- return args .Bool (0 ), args .Error (1 )
58
+ t .Run ("GetAll_Error" , func (t * testing.T ) {
59
+ gin .SetMode (gin .TestMode )
60
+ mockService := new (mocks.MockProductService )
61
+ productController := NewProductControllerImpl (mockService )
62
+
63
+ router := gin .Default ()
64
+ router .GET ("/products" , productController .GetAll )
65
+
66
+ mockService .On ("GetAll" ).Return ([]response.ProductResponse {}, assert .AnError )
67
+
68
+ req , err := http .NewRequest (http .MethodGet , "/products" , nil )
69
+ assert .NoError (t , err , "Expected no error creating request" )
70
+
71
+ rec := httptest .NewRecorder ()
72
+ router .ServeHTTP (rec , req )
73
+
74
+ assert .Equal (t , http .StatusInternalServerError , rec .Code , "Expected status code 500" )
75
+
76
+ var response response.BaseResponse
77
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
78
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
79
+ assert .Equal (t , 500 , response .Code , "Response code should be 500" )
80
+ assert .Equal (t , "Internal Server Error" , response .Status , "Response status should be Internal Server Error" )
81
+
82
+ mockService .AssertExpectations (t )
83
+ })
32
84
}
33
85
34
- func TestProductController_GetAll (t * testing.T ) {
35
- gin .SetMode (gin .TestMode )
36
- mockService := new (MockProductService )
37
- productController := NewProductControllerImpl (mockService )
86
+ func TestProductController_GetByID (t * testing.T ) {
87
+ t .Run ("GetByID_Success" , func (t * testing.T ) {
88
+ gin .SetMode (gin .TestMode )
89
+ mockService := new (mocks.MockProductService )
90
+ productController := NewProductControllerImpl (mockService )
38
91
39
- router := gin .Default ()
40
- router .GET ("/products" , productController .GetAll )
92
+ router := gin .Default ()
93
+ router .GET ("/products/:productId " , productController .GetByID )
41
94
42
- mockService .On ("GetAll" ).Return ([]response.ProductResponse {
43
- {
95
+ mockService .On ("GetByID" , "test-id" ).Return (response.ProductResponse {
44
96
ProductID : "test-id" ,
45
97
Name : "Test Product" ,
46
98
Category : "Test Category" ,
47
99
OriginalPrice : 100 ,
48
100
DiscountedPrice : 90 ,
49
- },
50
- {
51
- ProductID : "test-id-2" ,
52
- Name : "Test Product 2" ,
53
- Category : "Test Category 2" ,
54
- OriginalPrice : 200 ,
55
- DiscountedPrice : 180 ,
56
- },
57
- }, nil )
58
-
59
- req , err := http .NewRequest (http .MethodGet , "/products" , nil )
60
- assert .NoError (t , err , "Expected no error creating request" )
61
-
62
- rec := httptest .NewRecorder ()
63
- router .ServeHTTP (rec , req )
64
-
65
- assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200" )
66
-
67
- var response response.BaseResponse
68
- err = json .Unmarshal (rec .Body .Bytes (), & response )
69
- assert .NoError (t , err , "Expected no error unmarshalling response" )
70
- assert .Equal (t , 200 , response .Code , "Response code should be 200" )
71
- assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
72
- }
101
+ }, nil )
73
102
74
- func TestProductController_GetByID (t * testing.T ) {
75
- gin .SetMode (gin .TestMode )
76
- mockService := new (MockProductService )
77
- productController := NewProductControllerImpl (mockService )
78
-
79
- router := gin .Default ()
80
- router .GET ("/products/:productId" , productController .GetByID )
81
-
82
- mockService .On ("GetByID" , "test-id" ).Return (response.ProductResponse {
83
- ProductID : "test-id" ,
84
- Name : "Test Product" ,
85
- Category : "Test Category" ,
86
- OriginalPrice : 100 ,
87
- DiscountedPrice : 90 ,
88
- }, nil )
89
-
90
- req , err := http .NewRequest (http .MethodGet , "/products/test-id" , nil )
91
- assert .NoError (t , err , "Expected no error creating request" )
92
-
93
- rec := httptest .NewRecorder ()
94
- router .ServeHTTP (rec , req )
95
-
96
- assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200" )
97
-
98
- var response response.BaseResponse
99
- err = json .Unmarshal (rec .Body .Bytes (), & response )
100
- assert .NoError (t , err , "Expected no error unmarshalling response" )
101
- assert .Equal (t , 200 , response .Code , "Response code should be 200" )
102
- assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
103
+ req , err := http .NewRequest (http .MethodGet , "/products/test-id" , nil )
104
+ assert .NoError (t , err , "Expected no error creating request" )
105
+
106
+ rec := httptest .NewRecorder ()
107
+ router .ServeHTTP (rec , req )
108
+
109
+ assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200" )
110
+
111
+ var response response.BaseResponse
112
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
113
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
114
+ assert .Equal (t , 200 , response .Code , "Response code should be 200" )
115
+ assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
116
+ })
117
+
118
+ t .Run ("GetByID_Error" , func (t * testing.T ) {
119
+ gin .SetMode (gin .TestMode )
120
+ mockService := new (mocks.MockProductService )
121
+ productController := NewProductControllerImpl (mockService )
122
+
123
+ router := gin .Default ()
124
+ router .GET ("/products/:productId" , productController .GetByID )
125
+
126
+ mockService .On ("GetByID" , "test-id" ).Return (response.ProductResponse {}, assert .AnError )
127
+
128
+ req , err := http .NewRequest (http .MethodGet , "/products/test-id" , nil )
129
+ assert .NoError (t , err , "Expected no error creating request" )
130
+
131
+ rec := httptest .NewRecorder ()
132
+ router .ServeHTTP (rec , req )
133
+
134
+ assert .Equal (t , http .StatusInternalServerError , rec .Code , "Expected status code 500" )
135
+
136
+ var response response.BaseResponse
137
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
138
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
139
+ assert .Equal (t , 500 , response .Code , "Response code should be 500" )
140
+ assert .Equal (t , "Internal Server Error" , response .Status , "Response status should be Internal Server Error" )
141
+
142
+ mockService .AssertExpectations (t )
143
+ })
103
144
}
104
145
105
146
func TestProductController_UpdateData (t * testing.T ) {
106
- gin .SetMode (gin .TestMode )
107
- mockService := new (MockProductService )
108
- productController := NewProductControllerImpl (mockService )
147
+ t .Run ("UpdateData_Success" , func (t * testing.T ) {
148
+ gin .SetMode (gin .TestMode )
149
+ mockService := new (mocks.MockProductService )
150
+ productController := NewProductControllerImpl (mockService )
151
+
152
+ router := gin .Default ()
153
+ router .PUT ("/products" , productController .UpdateData )
154
+
155
+ mockService .On ("UpdateData" , request.UpdateDataRequest {
156
+ UpdateData : true ,
157
+ }).Return (true , nil )
109
158
110
- router := gin .Default ()
111
- router .PUT ("/products" , productController .UpdateData )
159
+ reqBody , err := json .Marshal (request.UpdateDataRequest {
160
+ UpdateData : true ,
161
+ })
162
+ assert .NoError (t , err , "Expected no error marshalling request" )
112
163
113
- mockService .On ("UpdateData" , request.UpdateDataRequest {
114
- UpdateData : true ,
115
- }).Return (true , nil )
164
+ req , err := http .NewRequest (http .MethodPut , "/products" , bytes .NewBuffer (reqBody ))
165
+ assert .NoError (t , err , "Expected no error creating request" )
116
166
117
- reqBody , err := json .Marshal (request.UpdateDataRequest {
118
- UpdateData : true ,
167
+ rec := httptest .NewRecorder ()
168
+ router .ServeHTTP (rec , req )
169
+
170
+ assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200" )
171
+
172
+ var response response.BaseResponse
173
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
174
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
175
+ assert .Equal (t , 200 , response .Code , "Response code should be 200" )
176
+ assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
119
177
})
120
- assert .NoError (t , err , "Expected no error marshalling request" )
121
178
122
- req , err := http .NewRequest (http .MethodPut , "/products" , bytes .NewBuffer (reqBody ))
123
- assert .NoError (t , err , "Expected no error creating request" )
179
+ t .Run ("UpdateData_Failure" , func (t * testing.T ) {
180
+ gin .SetMode (gin .TestMode )
181
+ mockService := new (mocks.MockProductService )
182
+ productController := NewProductControllerImpl (mockService )
183
+
184
+ router := gin .Default ()
185
+ router .PUT ("/products" , productController .UpdateData )
186
+
187
+ mockService .On ("UpdateData" , request.UpdateDataRequest {
188
+ UpdateData : false ,
189
+ }).Return (false , nil )
190
+
191
+ reqBody , err := json .Marshal (request.UpdateDataRequest {
192
+ UpdateData : false ,
193
+ })
194
+ assert .NoError (t , err , "Expected no error marshalling request" )
195
+
196
+ req , err := http .NewRequest (http .MethodPut , "/products" , bytes .NewBuffer (reqBody ))
197
+ assert .NoError (t , err , "Expected no error creating request" )
124
198
125
- rec := httptest .NewRecorder ()
126
- router .ServeHTTP (rec , req )
199
+ rec := httptest .NewRecorder ()
200
+ router .ServeHTTP (rec , req )
127
201
128
- assert .Equal (t , http .StatusOK , rec .Code , "Expected status code 200 " )
202
+ assert .Equal (t , http .StatusBadRequest , rec .Code , "Expected status code 400 " )
129
203
130
- var response response.BaseResponse
131
- err = json .Unmarshal (rec .Body .Bytes (), & response )
132
- assert .NoError (t , err , "Expected no error unmarshalling response" )
133
- assert .Equal (t , 200 , response .Code , "Response code should be 200" )
134
- assert .Equal (t , "OK" , response .Status , "Response status should be Success" )
204
+ var response response.BaseResponse
205
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
206
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
207
+ assert .Equal (t , 400 , response .Code , "Response code should be 400" )
208
+ assert .Equal (t , "Bad Request" , response .Status , "Response status should be Bad Request" )
209
+ assert .Equal (t , "Error updating data" , response .Message , "Response message should be UpdateData is required" )
210
+
211
+ mockService .AssertExpectations (t )
212
+ })
213
+
214
+ t .Run ("UpdateData_ScrapingError" , func (t * testing.T ) {
215
+ gin .SetMode (gin .TestMode )
216
+ mockService := new (mocks.MockProductService )
217
+ productController := NewProductControllerImpl (mockService )
218
+
219
+ router := gin .Default ()
220
+ router .PUT ("/products" , productController .UpdateData )
221
+
222
+ mockService .On ("UpdateData" , request.UpdateDataRequest {
223
+ UpdateData : true ,
224
+ }).Return (false , assert .AnError )
225
+
226
+ reqBody , err := json .Marshal (request.UpdateDataRequest {
227
+ UpdateData : true ,
228
+ })
229
+ assert .NoError (t , err , "Expected no error marshalling request" )
230
+
231
+ req , err := http .NewRequest (http .MethodPut , "/products" , bytes .NewBuffer (reqBody ))
232
+ assert .NoError (t , err , "Expected no error creating request" )
233
+
234
+ rec := httptest .NewRecorder ()
235
+ router .ServeHTTP (rec , req )
236
+
237
+ assert .Equal (t , http .StatusInternalServerError , rec .Code , "Expected status code 500" )
238
+
239
+ var response response.BaseResponse
240
+ err = json .Unmarshal (rec .Body .Bytes (), & response )
241
+ assert .NoError (t , err , "Expected no error unmarshalling response" )
242
+ assert .Equal (t , 500 , response .Code , "Response code should be 500" )
243
+ assert .Equal (t , "Internal Server Error" , response .Status , "Response status should be Internal Server Error" )
244
+
245
+ mockService .AssertExpectations (t )
246
+ })
135
247
}
0 commit comments