Skip to content

Commit 71f8cb6

Browse files
committed
update handlers
1 parent 5386588 commit 71f8cb6

File tree

4 files changed

+1052
-13
lines changed

4 files changed

+1052
-13
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package addresses
2+
3+
import (
4+
"bullet-cloud-api/internal/models"
5+
"context"
6+
7+
"github.com/google/uuid"
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
// MockAddressRepository is a mock type for the AddressRepository interface
12+
type MockAddressRepository struct {
13+
mock.Mock
14+
}
15+
16+
// Create provides a mock function with given fields: ctx, address
17+
func (_m *MockAddressRepository) Create(ctx context.Context, address *models.Address) (*models.Address, error) {
18+
ret := _m.Called(ctx, address)
19+
20+
var r0 *models.Address
21+
if rf, ok := ret.Get(0).(func(context.Context, *models.Address) *models.Address); ok {
22+
r0 = rf(ctx, address)
23+
} else {
24+
if ret.Get(0) != nil {
25+
r0 = ret.Get(0).(*models.Address)
26+
}
27+
}
28+
29+
var r1 error
30+
if rf, ok := ret.Get(1).(func(context.Context, *models.Address) error); ok {
31+
r1 = rf(ctx, address)
32+
} else {
33+
r1 = ret.Error(1)
34+
}
35+
36+
return r0, r1
37+
}
38+
39+
// FindByUserID provides a mock function with given fields: ctx, userID
40+
func (_m *MockAddressRepository) FindByUserID(ctx context.Context, userID uuid.UUID) ([]models.Address, error) {
41+
ret := _m.Called(ctx, userID)
42+
43+
var r0 []models.Address
44+
if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID) []models.Address); ok {
45+
r0 = rf(ctx, userID)
46+
} else {
47+
if ret.Get(0) != nil {
48+
r0 = ret.Get(0).([]models.Address)
49+
}
50+
}
51+
52+
var r1 error
53+
if rf, ok := ret.Get(1).(func(context.Context, uuid.UUID) error); ok {
54+
r1 = rf(ctx, userID)
55+
} else {
56+
r1 = ret.Error(1)
57+
}
58+
59+
return r0, r1
60+
}
61+
62+
// FindByUserAndID provides a mock function with given fields: ctx, userID, addressID
63+
func (_m *MockAddressRepository) FindByUserAndID(ctx context.Context, userID, addressID uuid.UUID) (*models.Address, error) {
64+
ret := _m.Called(ctx, userID, addressID)
65+
66+
var r0 *models.Address
67+
if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) *models.Address); ok {
68+
r0 = rf(ctx, userID, addressID)
69+
} else {
70+
if ret.Get(0) != nil {
71+
r0 = ret.Get(0).(*models.Address)
72+
}
73+
}
74+
75+
var r1 error
76+
if rf, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
77+
r1 = rf(ctx, userID, addressID)
78+
} else {
79+
r1 = ret.Error(1)
80+
}
81+
82+
return r0, r1
83+
}
84+
85+
// Update provides a mock function with given fields: ctx, userID, addressID, address
86+
func (_m *MockAddressRepository) Update(ctx context.Context, userID, addressID uuid.UUID, address *models.Address) (*models.Address, error) {
87+
ret := _m.Called(ctx, userID, addressID, address)
88+
89+
var r0 *models.Address
90+
if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID, *models.Address) *models.Address); ok {
91+
r0 = rf(ctx, userID, addressID, address)
92+
} else {
93+
if ret.Get(0) != nil {
94+
r0 = ret.Get(0).(*models.Address)
95+
}
96+
}
97+
98+
var r1 error
99+
if rf, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID, *models.Address) error); ok {
100+
r1 = rf(ctx, userID, addressID, address)
101+
} else {
102+
r1 = ret.Error(1)
103+
}
104+
105+
return r0, r1
106+
}
107+
108+
// Delete provides a mock function with given fields: ctx, userID, addressID
109+
func (_m *MockAddressRepository) Delete(ctx context.Context, userID, addressID uuid.UUID) error {
110+
ret := _m.Called(ctx, userID, addressID)
111+
112+
var r0 error
113+
if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
114+
r0 = rf(ctx, userID, addressID)
115+
} else {
116+
r0 = ret.Error(0)
117+
}
118+
119+
return r0
120+
}
121+
122+
// SetDefault provides a mock function with given fields: ctx, userID, addressID
123+
func (_m *MockAddressRepository) SetDefault(ctx context.Context, userID, addressID uuid.UUID) error {
124+
ret := _m.Called(ctx, userID, addressID)
125+
126+
var r0 error
127+
if rf, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
128+
r0 = rf(ctx, userID, addressID)
129+
} else {
130+
r0 = ret.Error(0)
131+
}
132+
133+
return r0
134+
}

internal/handlers/category_handler_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ import (
2323
// setupCategoryTest creates mock repositories, handler, middleware, and router for category tests.
2424
func setupCategoryTest(t *testing.T) (*categories.MockCategoryRepository, *users.MockUserRepository, *handlers.CategoryHandler, *auth.Middleware, *mux.Router) {
2525
mockCategoryRepo := new(categories.MockCategoryRepository)
26-
mockUserRepo := new(users.MockUserRepository) // Needed for middleware
27-
categoryHandler := handlers.NewCategoryHandler(mockCategoryRepo)
26+
// Call the base setup
27+
mockUserRepo, authMiddleware, router := setupBaseTest(t) // setupBaseTest is in the same package
2828

29-
// Use a fixed test secret for predictable tokens
30-
testJwtSecret := "test-secret-for-jwt-please-change" // Use the same secret as product tests for consistency
31-
authMiddleware := auth.NewMiddleware(testJwtSecret, mockUserRepo)
29+
categoryHandler := handlers.NewCategoryHandler(mockCategoryRepo)
3230

33-
router := mux.NewRouter()
3431
apiV1 := router.PathPrefix("/api").Subrouter()
3532

3633
// Public category routes

internal/handlers/product_handler_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@ import (
2020
"github.com/stretchr/testify/mock"
2121
)
2222

23-
// --- Test Setup Helper ---
24-
25-
// setupProductTest creates mock repositories, handler, middleware, and router for tests.
26-
func setupProductTest(t *testing.T) (*products.MockProductRepository, *users.MockUserRepository, *handlers.ProductHandler, *auth.Middleware, *mux.Router) {
27-
mockProductRepo := new(products.MockProductRepository)
28-
mockUserRepo := new(users.MockUserRepository) // Needed for middleware
29-
productHandler := handlers.NewProductHandler(mockProductRepo)
23+
// --- Test Setup Helpers ---
3024

25+
// setupBaseTest creates common mocks and components for handler tests.
26+
func setupBaseTest(t *testing.T) (*users.MockUserRepository, *auth.Middleware, *mux.Router) {
27+
mockUserRepo := new(users.MockUserRepository)
3128
// Use a fixed test secret for predictable tokens
3229
testJwtSecret := "test-secret-for-jwt-please-change"
3330
authMiddleware := auth.NewMiddleware(testJwtSecret, mockUserRepo)
3431

3532
router := mux.NewRouter()
33+
34+
return mockUserRepo, authMiddleware, router
35+
}
36+
37+
// setupProductTest creates mock repositories, handler, middleware, and router for tests.
38+
func setupProductTest(t *testing.T) (*products.MockProductRepository, *users.MockUserRepository, *handlers.ProductHandler, *auth.Middleware, *mux.Router) {
39+
mockProductRepo := new(products.MockProductRepository)
40+
// Call the base setup
41+
mockUserRepo, authMiddleware, router := setupBaseTest(t)
42+
43+
productHandler := handlers.NewProductHandler(mockProductRepo)
44+
3645
apiV1 := router.PathPrefix("/api").Subrouter()
3746

3847
// Public routes

0 commit comments

Comments
 (0)