bbgo_origin/pkg/service/db_test.go
2024-01-19 15:28:56 +08:00

45 lines
863 B
Go

package service
import (
"context"
"testing"
"github.com/c9s/rockhopper/v2"
"github.com/stretchr/testify/assert"
)
func prepareDB(t *testing.T) (*rockhopper.DB, error) {
dialect, err := rockhopper.LoadDialect("sqlite3")
if !assert.NoError(t, err) {
return nil, err
}
assert.NotNil(t, dialect)
db, err := rockhopper.Open("sqlite3", dialect, ":memory:")
if !assert.NoError(t, err) {
return nil, err
}
assert.NotNil(t, db)
_, err = db.CurrentVersion()
if !assert.NoError(t, err) {
return nil, err
}
var loader rockhopper.SqlMigrationLoader
migrations, err := loader.Load("../../migrations/sqlite3")
if !assert.NoError(t, err) {
return nil, err
}
assert.NotEmpty(t, migrations)
ctx := context.Background()
err = rockhopper.Up(ctx, db, migrations, 0, 0)
assert.NoError(t, err, "should migrate successfully")
return db, err
}