Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Jun 27, 2023
1 parent 51204ae commit 4cd6b86
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dagstore/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func (s *shardRepo) Get(ctx context.Context, key ds.Key) (value []byte, err erro

func (s *shardRepo) Has(ctx context.Context, key ds.Key) (exists bool, err error) {
var count int64
if err := s.DB.WithContext(ctx).Where("`key` = ?", key.BaseNamespace()).Count(&count).Error; err != nil {
if err := s.DB.Model(&internalShard{}).WithContext(ctx).Where("`key` = ?", key.BaseNamespace()).
Count(&count).Error; err != nil {
return false, nil
}
return count > 0, nil
Expand Down
123 changes: 123 additions & 0 deletions dagstore/shard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package dagstore

import (
"context"
"encoding/json"
"regexp"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/filecoin-project/venus/venus-shared/testutil"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/stretchr/testify/assert"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

var columns = []string{"key", "url", "transient_path", "state", "lazy", "error"}

func setup(t *testing.T) (shardRepo, sqlmock.Sqlmock, func()) {
sqlDB, mock, err := sqlmock.New()
assert.NoError(t, err)

mock.ExpectQuery("SELECT VERSION()").WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(""))

gormDB, err := gorm.Open(mysql.New(mysql.Config{
Conn: sqlDB,
}))
assert.NoError(t, err)

return shardRepo{DB: gormDB}, mock, func() {
_ = sqlDB.Close()
}
}

func TestGet(t *testing.T) {
r, mock, close := setup(t)
defer close()

var shard internalShard
testutil.Provide(t, &shard)
data, err := json.Marshal(shard)
assert.NoError(t, err)

mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `shards` WHERE `key` = ? LIMIT 1")).WithArgs(shard.Key).
WillReturnRows(sqlmock.NewRows(columns).
AddRow(shard.Key, shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error))

res, err := r.Get(context.Background(), ds.NewKey(shard.Key))
assert.NoError(t, err)
assert.Equal(t, data, res)
}

func TestHas(t *testing.T) {
r, mock, close := setup(t)
defer close()

var shard internalShard
testutil.Provide(t, &shard)

mock.ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM `shards` WHERE `key` = ?")).WithArgs(shard.Key).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))

res, err := r.Has(context.Background(), ds.NewKey(shard.Key))
assert.NoError(t, err)
assert.True(t, res)
}

func TestQuery(t *testing.T) {
r, mock, close := setup(t)
defer close()

var shard internalShard
testutil.Provide(t, &shard)
data, err := json.Marshal(shard)
assert.NoError(t, err)

mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `shards`")).WillReturnRows(sqlmock.NewRows(columns).
AddRow(shard.Key, shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error))

res, err := r.Query(context.Background(), query.Query{})
assert.NoError(t, err)
e, err := res.Rest()
assert.NoError(t, err)
assert.Equal(t, 1, len(e))
assert.Equal(t, data, e[0].Value)
}

func TestPut(t *testing.T) {
r, mock, close := setup(t)
defer close()

var shard internalShard
testutil.Provide(t, &shard)
data, err := json.Marshal(shard)
assert.NoError(t, err)

mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta("UPDATE `shards` SET `url`=?,`transient_path`=?,`state`=?,`lazy`=?,`error`=? WHERE `key` = ?")).
WithArgs(shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error, shard.Key).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

err = r.Put(context.Background(), ds.NewKey(shard.Key), data)
assert.NoError(t, err)
}

func TestDelete(t *testing.T) {
r, mock, close := setup(t)
defer close()

var shard internalShard
testutil.Provide(t, &shard)

mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM `shards` WHERE `key` = ?")).WithArgs(shard.Key).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

err := r.Delete(context.Background(), ds.NewKey(shard.Key))
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion dagstore/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewDAGStore(ctx context.Context, cfg *config.DAGStoreConfig, marketApi Mark
return nil, nil, fmt.Errorf("failed to create dagstore datastore in %s: %w", datastoreDir, err)
}

if len(cfg.MysqlShard.ConnectionString) != 0 {
if cfg.MysqlShard != nil && len(cfg.MysqlShard.ConnectionString) != 0 {
shardRepo, err := newShardRepo(cfg.MysqlShard)
if err != nil {
return nil, nil, fmt.Errorf("failed to shard repo: %v", err)
Expand Down

0 comments on commit 4cd6b86

Please sign in to comment.