Skip to content

Commit 428cd70

Browse files
committed
test: adiciona testes finais
1 parent eb478fc commit 428cd70

File tree

4 files changed

+209
-5
lines changed

4 files changed

+209
-5
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package db_test
2+
3+
import (
4+
"github.com/google/uuid"
5+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/db"
6+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
7+
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"testing"
11+
)
12+
13+
func TestGetByID_ReturnsProduct(t *testing.T) {
14+
mockDB := new(mocks.IDB)
15+
repo := db.NewPostgresProductRepository(mockDB)
16+
17+
productID := uuid.New()
18+
expectedProduct := &product.Product{
19+
Id: productID,
20+
Name: "Test Product",
21+
Description: "Test Description",
22+
Category: "Test Category",
23+
Price: 100.0,
24+
}
25+
26+
mockDB.On("First", mock.Anything, "id = ? and deleted_at is null", productID).Return(noErr).Run(func(args mock.Arguments) {
27+
arg := args.Get(0).(*db.ProductPostgres)
28+
*arg = db.ProductPostgres{
29+
BaseModel: db.BaseModel{ID: productID},
30+
Name: "Test Product",
31+
Description: "Test Description",
32+
Category: "Test Category",
33+
Price: 100.0,
34+
}
35+
})
36+
37+
result, err := repo.GetByID(productID)
38+
39+
assert.NoError(t, err)
40+
assert.Equal(t, expectedProduct, result)
41+
mockDB.AssertExpectations(t)
42+
}
43+
44+
func TestGetByID_ReturnsError(t *testing.T) {
45+
mockDB := new(mocks.IDB)
46+
repo := db.NewPostgresProductRepository(mockDB)
47+
48+
productID := uuid.New()
49+
50+
mockDB.On("First", mock.Anything, "id = ? and deleted_at is null", productID).Return(withErr)
51+
52+
result, err := repo.GetByID(productID)
53+
54+
assert.Error(t, err)
55+
assert.Nil(t, result)
56+
mockDB.AssertExpectations(t)
57+
}

internal/adapters/rest/controller/cart_controller_test.go

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,25 @@ func TestAddProduct_Err(t *testing.T) {
6666
router.ServeHTTP(w, req)
6767

6868
if w.Code != http.StatusInternalServerError {
69-
t.Errorf("Expected status 200, got %d", w.Code)
69+
t.Errorf("Expected status 500, got %d", w.Code)
70+
}
71+
}
72+
73+
func TestEditProduct_InvalidPayload(t *testing.T) {
74+
gin.SetMode(gin.TestMode)
75+
router := gin.Default()
76+
mockService := new(mocks.IService)
77+
ctrl := controller.NewCartController(mockService)
78+
router.POST("/cart/edit-product", ctrl.EditProduct)
79+
80+
payload := `{"client_id":"invalid-uuid","product_id":"123e4567-e89b-12d3-a456-426614174001","quantity":1}`
81+
req, _ := http.NewRequest(http.MethodPost, "/cart/edit-product", strings.NewReader(payload))
82+
req.Header.Set("Content-Type", "application/json")
83+
w := httptest.NewRecorder()
84+
router.ServeHTTP(w, req)
85+
86+
if w.Code != http.StatusBadRequest {
87+
t.Errorf("Expected status 400, got %d", w.Code)
7088
}
7189
}
7290

@@ -89,6 +107,43 @@ func TestEditProduct_Success(t *testing.T) {
89107
}
90108
}
91109

110+
func TestEditProduct_Error(t *testing.T) {
111+
gin.SetMode(gin.TestMode)
112+
router := gin.Default()
113+
mockService := new(mocks.IService)
114+
mockService.On("EditProduct", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("error"))
115+
ctrl := controller.NewCartController(mockService)
116+
router.POST("/cart/edit-product", ctrl.EditProduct)
117+
118+
payload := `{"client_id":"123e4567-e89b-12d3-a456-426614174000","product_id":"123e4567-e89b-12d3-a456-426614174001","quantity":2}`
119+
req, _ := http.NewRequest(http.MethodPost, "/cart/edit-product", strings.NewReader(payload))
120+
req.Header.Set("Content-Type", "application/json")
121+
w := httptest.NewRecorder()
122+
router.ServeHTTP(w, req)
123+
124+
if w.Code != http.StatusInternalServerError {
125+
t.Errorf("Expected status 500, got %d", w.Code)
126+
}
127+
}
128+
129+
func TestRemoveProduct_InvalidPayload(t *testing.T) {
130+
gin.SetMode(gin.TestMode)
131+
router := gin.Default()
132+
mockService := new(mocks.IService)
133+
ctrl := controller.NewCartController(mockService)
134+
router.POST("/cart/remove-product", ctrl.RemoveProduct)
135+
136+
payload := `{"client_id":"invalid-uuid","product_id":"123e4567-e89b-12d3-a456-426614174001","quantity":1}`
137+
req, _ := http.NewRequest(http.MethodPost, "/cart/remove-product", strings.NewReader(payload))
138+
req.Header.Set("Content-Type", "application/json")
139+
w := httptest.NewRecorder()
140+
router.ServeHTTP(w, req)
141+
142+
if w.Code != http.StatusBadRequest {
143+
t.Errorf("Expected status 400, got %d", w.Code)
144+
}
145+
}
146+
92147
func TestRemoveProduct_Success(t *testing.T) {
93148
gin.SetMode(gin.TestMode)
94149
router := gin.Default()
@@ -108,6 +163,43 @@ func TestRemoveProduct_Success(t *testing.T) {
108163
}
109164
}
110165

166+
func TestRemoveProduct_Error(t *testing.T) {
167+
gin.SetMode(gin.TestMode)
168+
router := gin.Default()
169+
mockService := new(mocks.IService)
170+
mockService.On("RemoveProduct", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("error"))
171+
ctrl := controller.NewCartController(mockService)
172+
router.POST("/cart/remove-product", ctrl.RemoveProduct)
173+
174+
payload := `{"client_id":"123e4567-e89b-12d3-a456-426614174000","product_id":"123e4567-e89b-12d3-a456-426614174001"}`
175+
req, _ := http.NewRequest(http.MethodPost, "/cart/remove-product", strings.NewReader(payload))
176+
req.Header.Set("Content-Type", "application/json")
177+
w := httptest.NewRecorder()
178+
router.ServeHTTP(w, req)
179+
180+
if w.Code != http.StatusInternalServerError {
181+
t.Errorf("Expected status 500, got %d", w.Code)
182+
}
183+
}
184+
185+
func TestOverview_InvalidPayload(t *testing.T) {
186+
gin.SetMode(gin.TestMode)
187+
router := gin.Default()
188+
mockService := new(mocks.IService)
189+
ctrl := controller.NewCartController(mockService)
190+
router.POST("/cart/overview", ctrl.Overview)
191+
192+
payload := `{"client_id":"invalid-uuid"}`
193+
req, _ := http.NewRequest(http.MethodPost, "/cart/overview", strings.NewReader(payload))
194+
req.Header.Set("Content-Type", "application/json")
195+
w := httptest.NewRecorder()
196+
router.ServeHTTP(w, req)
197+
198+
if w.Code != http.StatusBadRequest {
199+
t.Errorf("Expected status 400, got %d", w.Code)
200+
}
201+
}
202+
111203
func TestOverview_Success(t *testing.T) {
112204
gin.SetMode(gin.TestMode)
113205
router := gin.Default()
@@ -126,3 +218,22 @@ func TestOverview_Success(t *testing.T) {
126218
t.Errorf("Expected status 200, got %d", w.Code)
127219
}
128220
}
221+
222+
func TestOverview_Error(t *testing.T) {
223+
gin.SetMode(gin.TestMode)
224+
router := gin.Default()
225+
mockService := new(mocks.IService)
226+
mockService.On("GetFullCart", mock.Anything).Return(nil, errors.New("error"))
227+
ctrl := controller.NewCartController(mockService)
228+
router.POST("/cart/overview", ctrl.Overview)
229+
230+
payload := `{"client_id":"123e4567-e89b-12d3-a456-426614174000"}`
231+
req, _ := http.NewRequest(http.MethodPost, "/cart/overview", strings.NewReader(payload))
232+
req.Header.Set("Content-Type", "application/json")
233+
w := httptest.NewRecorder()
234+
router.ServeHTTP(w, req)
235+
236+
if w.Code != http.StatusInternalServerError {
237+
t.Errorf("Expected status 500, got %d", w.Code)
238+
}
239+
}

internal/adapters/rest/controller/product_controller_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ func TestSearchProducts_Success(t *testing.T) {
2828
}
2929
}
3030

31+
func TestSearchProducts_Error(t *testing.T) {
32+
gin.SetMode(gin.TestMode)
33+
router := gin.Default()
34+
mockService := new(mocks.IProductService)
35+
mockService.On("Search", mock.Anything, mock.Anything).Return(nil, errors.New("error"))
36+
ctrl := controller.NewProductController(mockService)
37+
router.GET("/product", ctrl.Search)
38+
39+
req, _ := http.NewRequest(http.MethodGet, "/product?search=phone&category=electronics", nil)
40+
w := httptest.NewRecorder()
41+
router.ServeHTTP(w, req)
42+
43+
if w.Code != http.StatusInternalServerError {
44+
t.Errorf("Expected status 500, got %d", w.Code)
45+
}
46+
}
47+
3148
func TestDeleteProduct_Success(t *testing.T) {
3249
gin.SetMode(gin.TestMode)
3350
router := gin.Default()
@@ -49,7 +66,6 @@ func TestDeleteProduct_InvalidID(t *testing.T) {
4966
gin.SetMode(gin.TestMode)
5067
router := gin.Default()
5168
mockService := new(mocks.IProductService)
52-
mockService.On("Search", mock.Anything).Return(errors.New("error"))
5369
ctrl := controller.NewProductController(mockService)
5470
router.DELETE("/product/:id", ctrl.Delete)
5571

@@ -61,3 +77,20 @@ func TestDeleteProduct_InvalidID(t *testing.T) {
6177
t.Errorf("Expected status 400, got %d", w.Code)
6278
}
6379
}
80+
81+
func TestDeleteProduct_Error(t *testing.T) {
82+
gin.SetMode(gin.TestMode)
83+
router := gin.Default()
84+
mockService := new(mocks.IProductService)
85+
mockService.On("Delete", mock.Anything).Return(errors.New("error"))
86+
ctrl := controller.NewProductController(mockService)
87+
router.DELETE("/product/:id", ctrl.Delete)
88+
89+
w := httptest.NewRecorder()
90+
req, _ := http.NewRequest(http.MethodDelete, "/product/123e4567-e89b-12d3-a456-426614174000", nil)
91+
router.ServeHTTP(w, req)
92+
93+
if w.Code != http.StatusInternalServerError {
94+
t.Errorf("Expected status 500, got %d", w.Code)
95+
}
96+
}

internal/adapters/rest/server/server_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server_test
22

33
import (
44
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
5+
"github.com/stretchr/testify/mock"
56
"net/http"
67
"net/http/httptest"
78
"testing"
@@ -45,9 +46,10 @@ func TestRegisterProductHandlers_CallsHandler(t *testing.T) {
4546
gin.SetMode(gin.TestMode)
4647
router := gin.Default()
4748
productService := new(mocks.IProductService)
49+
productService.On("Search", mock.Anything, mock.Anything).Return(nil, nil)
4850
handler.RegisterProductHandlers(router, productService)
4951

50-
req, _ := http.NewRequest(http.MethodGet, "/products", nil)
52+
req, _ := http.NewRequest(http.MethodGet, "/product", nil)
5153
w := httptest.NewRecorder()
5254
router.ServeHTTP(w, req)
5355

@@ -58,11 +60,12 @@ func TestRegisterCartHandlers_CallsHandler(t *testing.T) {
5860
gin.SetMode(gin.TestMode)
5961
router := gin.Default()
6062
cartService := new(mocks.IService)
63+
cartService.On("GetFullCart").Return(nil, nil)
6164
handler.RegisterCartHandlers(router, cartService)
6265

63-
req, _ := http.NewRequest(http.MethodGet, "/cart", nil)
66+
req, _ := http.NewRequest(http.MethodPost, "/cart/overview", nil)
6467
w := httptest.NewRecorder()
6568
router.ServeHTTP(w, req)
6669

67-
assert.Equal(t, http.StatusOK, w.Code)
70+
assert.Equal(t, http.StatusBadRequest, w.Code)
6871
}

0 commit comments

Comments
 (0)