Skip to content

Commit 01766a2

Browse files
authored
Merge pull request #6 from pangolin-do-golang/add-tests
test: adiciona testes adicionais para o repo de produto
2 parents cecb3b4 + da962d6 commit 01766a2

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/pangolin-do-golang/tech-challenge-cart-api
33
go 1.23.3
44

55
require (
6+
github.com/DATA-DOG/go-sqlmock v1.5.2
67
github.com/gin-gonic/gin v1.10.0
78
github.com/google/uuid v1.6.0
89
github.com/joho/godotenv v1.5.1

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
2+
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
13
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
24
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
35
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
@@ -67,6 +69,7 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
6769
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
6870
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
6971
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
72+
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
7073
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
7174
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
7275
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=

internal/adapters/db/product_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package db_test
22

33
import (
4+
"github.com/DATA-DOG/go-sqlmock"
45
"github.com/google/uuid"
56
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/db"
67
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
78
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/mock"
11+
"gorm.io/driver/postgres"
12+
"gorm.io/gorm"
1013
"testing"
1114
)
1215

@@ -55,3 +58,36 @@ func TestGetByID_ReturnsError(t *testing.T) {
5558
assert.Nil(t, result)
5659
mockDB.AssertExpectations(t)
5760
}
61+
62+
func TestProductSearch(t *testing.T) {
63+
d, m, err := sqlmock.New()
64+
conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"}))
65+
66+
m.ExpectQuery("SELECT .+").WillReturnRows(
67+
sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}).
68+
AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0).
69+
AddRow(uuid.New(), "Test Product 2", "Test Description 2", "Test Category", 200.0),
70+
)
71+
repo := db.NewPostgresProductRepository(conn)
72+
_, err = repo.Search("search", "category")
73+
74+
assert.NoError(t, err)
75+
}
76+
77+
func TestProductDelete(t *testing.T) {
78+
d, m, err := sqlmock.New()
79+
conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"}))
80+
81+
m.ExpectQuery("SELECT .+").WillReturnRows(
82+
sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}).
83+
AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0),
84+
)
85+
86+
m.ExpectBegin()
87+
m.ExpectExec("UPDATE .+").WillReturnResult(sqlmock.NewResult(0, 1))
88+
m.ExpectCommit()
89+
repo := db.NewPostgresProductRepository(conn)
90+
err = repo.Delete(uuid.New())
91+
92+
assert.NoError(t, err)
93+
}

0 commit comments

Comments
 (0)