Skip to content

Commit 1935192

Browse files
committed
test: adiciona testes para adapters
1 parent a1dc33e commit 1935192

File tree

7 files changed

+660
-10
lines changed

7 files changed

+660
-10
lines changed

internal/adapters/db/cart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type PostgresCartRepository struct {
13-
db *gorm.DB
13+
db IDB
1414
}
1515

1616
type CartPostgres struct {
@@ -24,7 +24,7 @@ func (op CartPostgres) TableName() string {
2424
return "cart"
2525
}
2626

27-
func NewPostgresCartRepository(db *gorm.DB) cart.ICartRepository {
27+
func NewPostgresCartRepository(db IDB) cart.ICartRepository {
2828
return &PostgresCartRepository{db: db}
2929
}
3030

internal/adapters/db/cart_products.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55

66
"github.com/google/uuid"
77
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/cart"
8-
"gorm.io/gorm"
98
)
109

1110
type PostgresCartProductsRepository struct {
12-
db *gorm.DB
11+
db IDB
1312
}
1413

1514
type CartProductsPostgres struct {
@@ -26,7 +25,7 @@ func (op *CartProductsPostgres) TableName() string {
2625
return "cart_products"
2726
}
2827

29-
func NewPostgresCartProductsRepository(db *gorm.DB) cart.ICartProductRepository {
28+
func NewPostgresCartProductsRepository(db IDB) cart.ICartProductRepository {
3029
return &PostgresCartProductsRepository{db: db}
3130
}
3231

@@ -48,7 +47,7 @@ func (p *PostgresCartProductsRepository) Create(_ context.Context, cartID uuid.U
4847

4948
func (p *PostgresCartProductsRepository) GetByCartID(_ context.Context, cartID uuid.UUID) ([]*cart.Product, error) {
5049
var cartProducts []CartProductsPostgres
51-
err := p.db.Where("cart_id = ?", cartID).Find(&cartProducts).Error
50+
err := p.db.Find(&cartProducts, "cart_id = ?", cartID).Error
5251
if err != nil {
5352
return nil, err
5453
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package db_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/uuid"
8+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/db"
9+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/cart"
10+
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/mock"
13+
)
14+
15+
func TestCreateCartProduct_Success(t *testing.T) {
16+
mockDB := new(mocks.IDB)
17+
repo := db.NewPostgresCartProductsRepository(mockDB)
18+
19+
cartID := uuid.New()
20+
product := &cart.Product{
21+
ProductID: uuid.New(),
22+
Quantity: 1,
23+
Comments: "Test comment",
24+
}
25+
26+
mockDB.On("Create", mock.Anything).Return(noErr)
27+
28+
err := repo.Create(context.Background(), cartID, product)
29+
30+
assert.NoError(t, err)
31+
mockDB.AssertExpectations(t)
32+
}
33+
34+
func TestCreateCartProduct_DBError(t *testing.T) {
35+
mockDB := new(mocks.IDB)
36+
repo := db.NewPostgresCartProductsRepository(mockDB)
37+
38+
cartID := uuid.New()
39+
product := &cart.Product{
40+
ProductID: uuid.New(),
41+
Quantity: 1,
42+
Comments: "Test comment",
43+
}
44+
45+
mockDB.On("Create", mock.Anything).Return(withErr)
46+
47+
err := repo.Create(context.Background(), cartID, product)
48+
49+
assert.Error(t, err)
50+
mockDB.AssertExpectations(t)
51+
}
52+
53+
func TestGetCartProductsByCartID_Success(t *testing.T) {
54+
mockDB := new(mocks.IDB)
55+
repo := db.NewPostgresCartProductsRepository(mockDB)
56+
57+
cartID := uuid.New()
58+
expectedProducts := []db.CartProductsPostgres{
59+
{CartID: cartID, ProductID: uuid.New(), Quantity: 1, Comments: "Test comment"},
60+
}
61+
62+
mockDB.On("Find", mock.Anything, mock.Anything, mock.Anything).Return(noErr).Run(func(args mock.Arguments) {
63+
arg := args.Get(0).(*[]db.CartProductsPostgres)
64+
*arg = expectedProducts
65+
})
66+
67+
products, err := repo.GetByCartID(context.Background(), cartID)
68+
69+
assert.NoError(t, err)
70+
assert.Len(t, products, 1)
71+
assert.Equal(t, expectedProducts[0].ProductID, products[0].ProductID)
72+
mockDB.AssertExpectations(t)
73+
}
74+
75+
func TestGetCartProductsByCartID_DBError(t *testing.T) {
76+
mockDB := new(mocks.IDB)
77+
repo := db.NewPostgresCartProductsRepository(mockDB)
78+
79+
cartID := uuid.New()
80+
81+
mockDB.On("Find", mock.Anything, mock.Anything, mock.Anything).Return(withErr)
82+
83+
products, err := repo.GetByCartID(context.Background(), cartID)
84+
85+
assert.Error(t, err)
86+
assert.Nil(t, products)
87+
mockDB.AssertExpectations(t)
88+
}
89+
90+
func TestDeleteCartProductByProductID_Success(t *testing.T) {
91+
mockDB := new(mocks.IDB)
92+
repo := db.NewPostgresCartProductsRepository(mockDB)
93+
94+
cartID := uuid.New()
95+
productID := uuid.New()
96+
97+
mockDB.On("Delete", mock.Anything, "cart_id = ? AND product_id = ?", cartID, productID).Return(noErr)
98+
99+
err := repo.DeleteByProductID(context.Background(), cartID, productID)
100+
101+
assert.NoError(t, err)
102+
mockDB.AssertExpectations(t)
103+
}
104+
105+
func TestDeleteCartProductByProductID_DBError(t *testing.T) {
106+
mockDB := new(mocks.IDB)
107+
repo := db.NewPostgresCartProductsRepository(mockDB)
108+
109+
cartID := uuid.New()
110+
productID := uuid.New()
111+
112+
mockDB.On("Delete", mock.Anything, "cart_id = ? AND product_id = ?", cartID, productID).Return(withErr)
113+
114+
err := repo.DeleteByProductID(context.Background(), cartID, productID)
115+
116+
assert.Error(t, err)
117+
mockDB.AssertExpectations(t)
118+
}
119+
120+
/*
121+
func TestUpdateCartProductByProductID_Success(t *testing.T) {
122+
mockDB := new(mocks.IDB)
123+
repo := db.NewPostgresCartProductsRepository(mockDB)
124+
125+
cartID := uuid.New()
126+
productID := uuid.New()
127+
product := &cart.Product{
128+
ProductID: productID,
129+
Quantity: 2,
130+
Comments: "Updated comment",
131+
}
132+
133+
mockDB.On("Model", mock.Anything).Return(mockDB)
134+
mockDB.On("Where", "cart_id = ? AND product_id = ?", cartID, productID).Return(mockDB)
135+
mockDB.On("Updates", mock.Anything).Return(noErr)
136+
137+
err := repo.UpdateProductByProductID(context.Background(), cartID, productID, product)
138+
139+
assert.NoError(t, err)
140+
mockDB.AssertExpectations(t)
141+
}
142+
143+
func TestUpdateCartProductByProductID_DBError(t *testing.T) {
144+
mockDB := new(mocks.IDB)
145+
repo := db.NewPostgresCartProductsRepository(mockDB)
146+
147+
cartID := uuid.New()
148+
productID := uuid.New()
149+
product := &cart.Product{
150+
ProductID: productID,
151+
Quantity: 2,
152+
Comments: "Updated comment",
153+
}
154+
155+
mockDB.On("Model", mock.Anything).Return(mockDB)
156+
mockDB.On("Where", "cart_id = ? AND product_id = ?", cartID, productID).Return(mockDB)
157+
mockDB.On("Updates", mock.Anything).Return(withErr)
158+
159+
err := repo.UpdateProductByProductID(context.Background(), cartID, productID, product)
160+
161+
assert.Error(t, err)
162+
mockDB.AssertExpectations(t)
163+
}
164+
*/

internal/adapters/db/cart_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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/errutil"
7+
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"gorm.io/gorm"
11+
"testing"
12+
)
13+
14+
var noErr = &gorm.DB{}
15+
var withErr = &gorm.DB{Error: errutil.ErrRecordNotFound}
16+
17+
func TestCreateCart_Success(t *testing.T) {
18+
mockDB := new(mocks.IDB)
19+
mockDB.On("Create", mock.Anything).Return(noErr)
20+
repo := db.NewPostgresCartRepository(mockDB)
21+
22+
clientID := uuid.New()
23+
24+
cart, err := repo.Create(clientID)
25+
26+
assert.NoError(t, err)
27+
assert.NotNil(t, cart)
28+
assert.Equal(t, clientID, cart.ClientID)
29+
mockDB.AssertExpectations(t)
30+
}
31+
32+
func TestCreateCart_DBError(t *testing.T) {
33+
mockDB := new(mocks.IDB)
34+
repo := db.NewPostgresCartRepository(mockDB)
35+
36+
clientID := uuid.New()
37+
mockDB.On("Create", mock.Anything).Return(withErr)
38+
39+
cart, err := repo.Create(clientID)
40+
41+
assert.Error(t, err)
42+
assert.Nil(t, cart)
43+
mockDB.AssertExpectations(t)
44+
}
45+
46+
func TestGetCart_Success(t *testing.T) {
47+
mockDB := new(mocks.IDB)
48+
repo := db.NewPostgresCartRepository(mockDB)
49+
50+
clientID := uuid.New()
51+
expectedCart := &db.CartPostgres{ClientID: clientID}
52+
mockDB.On("First", mock.Anything, "client_id = ?", clientID).Return(noErr).Run(func(args mock.Arguments) {
53+
arg := args.Get(0).(*db.CartPostgres)
54+
*arg = *expectedCart
55+
})
56+
57+
cart, err := repo.Get(clientID)
58+
59+
assert.NoError(t, err)
60+
assert.NotNil(t, cart)
61+
assert.Equal(t, clientID, cart.ClientID)
62+
mockDB.AssertExpectations(t)
63+
}
64+
65+
func TestGetCart_NotFound(t *testing.T) {
66+
mockDB := new(mocks.IDB)
67+
repo := db.NewPostgresCartRepository(mockDB)
68+
69+
clientID := uuid.New()
70+
mockDB.On("First", mock.Anything, "client_id = ?", clientID).Return(withErr)
71+
72+
cart, err := repo.Get(clientID)
73+
74+
assert.Error(t, err)
75+
assert.Nil(t, cart)
76+
assert.Equal(t, errutil.ErrRecordNotFound, err)
77+
mockDB.AssertExpectations(t)
78+
}
79+
80+
func TestGetCart_DBError(t *testing.T) {
81+
mockDB := new(mocks.IDB)
82+
repo := db.NewPostgresCartRepository(mockDB)
83+
84+
clientID := uuid.New()
85+
mockDB.On("First", mock.Anything, "client_id = ?", clientID).Return(withErr)
86+
87+
cart, err := repo.Get(clientID)
88+
89+
assert.Error(t, err)
90+
assert.Nil(t, cart)
91+
mockDB.AssertExpectations(t)
92+
}

internal/adapters/db/interface.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
"gorm.io/gorm"
6+
)
7+
8+
type IDB interface {
9+
Model(value interface{}) (tx *gorm.DB)
10+
Select(query interface{}, args ...interface{}) (tx *gorm.DB)
11+
Where(query interface{}, args ...interface{}) (tx *gorm.DB)
12+
Order(value interface{}) (tx *gorm.DB)
13+
Limit(limit int) (tx *gorm.DB)
14+
Create(value interface{}) (tx *gorm.DB)
15+
CreateInBatches(value interface{}, batchSize int) (tx *gorm.DB)
16+
Save(value interface{}) (tx *gorm.DB)
17+
First(dest interface{}, conds ...interface{}) (tx *gorm.DB)
18+
Take(dest interface{}, conds ...interface{}) (tx *gorm.DB)
19+
Last(dest interface{}, conds ...interface{}) (tx *gorm.DB)
20+
Find(dest interface{}, conds ...interface{}) (tx *gorm.DB)
21+
Update(column string, value interface{}) (tx *gorm.DB)
22+
Delete(value interface{}, conds ...interface{}) (tx *gorm.DB)
23+
Count(count *int64) (tx *gorm.DB)
24+
Row() *sql.Row
25+
Rows() (*sql.Rows, error)
26+
Scan(dest interface{}) (tx *gorm.DB)
27+
Exec(sql string, values ...interface{}) (tx *gorm.DB)
28+
}

internal/adapters/db/product.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66

77
"github.com/google/uuid"
88
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
9-
"gorm.io/gorm"
109
)
1110

1211
type PostgresProductRepository struct {
13-
db *gorm.DB
12+
db IDB
1413
}
1514

1615
type ProductPostgres struct {
@@ -25,14 +24,14 @@ func (pp ProductPostgres) TableName() string {
2524
return "product"
2625
}
2726

28-
func NewPostgresProductRepository(db *gorm.DB) product.Repository {
27+
func NewPostgresProductRepository(db IDB) product.Repository {
2928
return &PostgresProductRepository{db: db}
3029
}
3130

3231
func (repo *PostgresProductRepository) GetByID(id uuid.UUID) (*product.Product, error) {
3332
var dbRecord ProductPostgres
3433

35-
if err := repo.db.Where("id = ? and deleted_at is null", id).First(&dbRecord).Error; err != nil {
34+
if err := repo.db.First(&dbRecord, "id = ? and deleted_at is null", id).Error; err != nil {
3635
return nil, err
3736
}
3837

0 commit comments

Comments
 (0)