Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/pangolin-do-golang/tech-challenge-cart-api
go 1.23.3

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
Expand Down Expand Up @@ -67,6 +69,7 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
Expand Down
36 changes: 36 additions & 0 deletions internal/adapters/db/product_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package db_test

import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/google/uuid"
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/db"
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"testing"
)

Expand Down Expand Up @@ -55,3 +58,36 @@ func TestGetByID_ReturnsError(t *testing.T) {
assert.Nil(t, result)
mockDB.AssertExpectations(t)
}

func TestProductSearch(t *testing.T) {
d, m, err := sqlmock.New()
conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"}))

m.ExpectQuery("SELECT .+").WillReturnRows(
sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}).
AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0).
AddRow(uuid.New(), "Test Product 2", "Test Description 2", "Test Category", 200.0),
)
repo := db.NewPostgresProductRepository(conn)
_, err = repo.Search("search", "category")

assert.NoError(t, err)
}

func TestProductDelete(t *testing.T) {
d, m, err := sqlmock.New()
conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"}))

m.ExpectQuery("SELECT .+").WillReturnRows(
sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}).
AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0),
)

m.ExpectBegin()
m.ExpectExec("UPDATE .+").WillReturnResult(sqlmock.NewResult(0, 1))
m.ExpectCommit()
repo := db.NewPostgresProductRepository(conn)
err = repo.Delete(uuid.New())

assert.NoError(t, err)
}
Loading