Skip to content

Commit

Permalink
🌱Remove sqlmock related code from util/test/test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dumindu committed Sep 14, 2023
1 parent 40b27a3 commit 847d9ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions api/resource/book/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRepository_Create(t *testing.T) {
id := uuid.New()
mock.ExpectBegin()
mock.ExpectExec("^INSERT INTO \"books\" ").
WithArgs(id, "Title", "Author", testUtil.AnyTime{}, "", "", testUtil.AnyTime{}, testUtil.AnyTime{}, nil).
WithArgs(id, "Title", "Author", mockDB.AnyTime{}, "", "", mockDB.AnyTime{}, mockDB.AnyTime{}, nil).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestRepository_Update(t *testing.T) {
id := uuid.New()
mock.ExpectBegin()
mock.ExpectExec("^UPDATE \"books\" SET").
WithArgs("Title", "Author", testUtil.AnyTime{}, id).
WithArgs("Title", "Author", mockDB.AnyTime{}, id).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

Expand All @@ -93,7 +93,7 @@ func TestRepository_Delete(t *testing.T) {
id := uuid.New()
mock.ExpectBegin()
mock.ExpectExec("^UPDATE \"books\" SET \"deleted_at\"").
WithArgs(testUtil.AnyTime{}, id).
WithArgs(mockDB.AnyTime{}, id).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

Expand Down
10 changes: 10 additions & 0 deletions mock/db/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package db

import (
"database/sql/driver"
"time"

"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand All @@ -19,3 +22,10 @@ func NewMockDB() (*gorm.DB, sqlmock.Sqlmock, error) {

return gdb, mock, nil
}

type AnyTime struct{}

func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
11 changes: 0 additions & 11 deletions util/test/test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package test

import (
"database/sql/driver"
"testing"
"time"
)

func NoError(t *testing.T, err error) {
Expand All @@ -17,12 +15,3 @@ func Equal[T comparable](t *testing.T, x, y T) {
t.Fatalf("not equal: %v, %v", x, y)
}
}

// --- For sqlmock ---

type AnyTime struct{}

func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}

0 comments on commit 847d9ab

Please sign in to comment.