diff --git a/api/resource/book/repository_test.go b/api/resource/book/repository_test.go index 336f8cf..ef72675 100644 --- a/api/resource/book/repository_test.go +++ b/api/resource/book/repository_test.go @@ -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() @@ -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() @@ -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() diff --git a/mock/db/db.go b/mock/db/db.go index 9cd67c1..5f3af3f 100644 --- a/mock/db/db.go +++ b/mock/db/db.go @@ -1,6 +1,9 @@ package db import ( + "database/sql/driver" + "time" + "github.com/DATA-DOG/go-sqlmock" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -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 +} diff --git a/util/test/test.go b/util/test/test.go index 2536cdf..2c7bfb7 100644 --- a/util/test/test.go +++ b/util/test/test.go @@ -1,9 +1,7 @@ package test import ( - "database/sql/driver" "testing" - "time" ) func NoError(t *testing.T, err error) { @@ -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 -}