-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blobstorage): covering tests for BlockMetaRepo, BlobHashRepo and…
… DatabaseTransactions
- Loading branch information
Showing
13 changed files
with
674 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
"github.com/taikoxyz/taiko-mono/packages/blobstorage" | ||
) | ||
|
||
type MockBlobHashRepo struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockBlobHashRepo) FirstByBlobHash(blobHash string) (*blobstorage.BlobHash, error) { | ||
args := m.Called(blobHash) | ||
if item, ok := args.Get(0).(*blobstorage.BlobHash); ok { | ||
return item, args.Error(1) | ||
} | ||
return nil, args.Error(1) | ||
} | ||
|
||
func (m *MockBlobHashRepo) Save(opts blobstorage.SaveBlobHashOpts) error { | ||
return m.Called(opts).Error(0) | ||
} | ||
|
||
func (m *MockBlobHashRepo) DeleteAllAfterBlockID(blockID uint64) error { | ||
return m.Called(blockID).Error(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
blobstorage "github.com/taikoxyz/taiko-mono/packages/blobstorage" | ||
) | ||
|
||
type MockBlockMetaRepo struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockBlockMetaRepo) Save(opts blobstorage.SaveBlockMetaOpts) error { | ||
return m.Called(opts).Error(0) | ||
} | ||
|
||
func (m *MockBlockMetaRepo) FindLatestBlockID() (uint64, error) { | ||
args := m.Called() | ||
return args.Get(0).(uint64), args.Error(1) | ||
} | ||
|
||
func (m *MockBlockMetaRepo) DeleteAllAfterBlockID(blockID uint64) error { | ||
return m.Called(blockID).Error(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package mocks | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/stretchr/testify/mock" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type MockDB struct { | ||
mock.Mock | ||
gormDB *gorm.DB | ||
} | ||
|
||
func (m *MockDB) DB() (*sql.DB, error) { | ||
args := m.Called() | ||
return args.Get(0).(*sql.DB), args.Error(1) | ||
} | ||
|
||
func (m *MockDB) GormDB() *gorm.DB { | ||
args := m.Called() | ||
return args.Get(0).(*gorm.DB) | ||
} | ||
|
||
func (db *MockDB) Begin() *gorm.DB { | ||
args := db.Called() | ||
return args.Get(0).(*gorm.DB) | ||
} | ||
|
||
func (db *MockDB) Commit() error { | ||
args := db.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (db *MockDB) Rollback() error { | ||
args := db.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func NewMockDB(t *testing.T) (*MockDB, sqlmock.Sqlmock, *gorm.DB, func()) { | ||
sqlDB, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("newMockDB: %v", err) | ||
} | ||
|
||
mock.ExpectQuery("select sqlite_version()"). | ||
WillReturnRows(sqlmock.NewRows([]string{"version"}). | ||
AddRow("3.31.1")) | ||
|
||
gormDB, err := gorm.Open(sqlite.Dialector{Conn: sqlDB}, &gorm.Config{}) | ||
if err != nil { | ||
t.Fatalf("newMockDB: %v", err) | ||
} | ||
|
||
mockDB := &MockDB{gormDB: gormDB} | ||
closeDB := func() { sqlDB.Close() } | ||
|
||
return mockDB, mock, gormDB, closeDB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.