mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-04-23 03:00:50 -04:00
Co-authored-by: colin <102356659+colinlyguo@users.noreply.github.com> Co-authored-by: Rohit Narurkar <rohit.narurkar@proton.me> Co-authored-by: colinlyguo <colinlyguo@scroll.io> Co-authored-by: colinlyguo <colinlyguo@users.noreply.github.com> Co-authored-by: Mengran Lan <mengran@scroll.io> Co-authored-by: amoylan2 <amoylan2@users.noreply.github.com> Co-authored-by: Mengran Lan <lanmengran@qq.com> Co-authored-by: Suuuuuuperrrrr fred <FredrikaPhililip@proton.me> Co-authored-by: sbaizet <74511063+sbaizet-ledger@users.noreply.github.com> Co-authored-by: caseylove <casey4love@foxmail.com> Co-authored-by: BoxChen <13927203+nishuzumi@users.noreply.github.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: georgehao <georgehao@users.noreply.github.com>
90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
package migrate
|
|
|
|
import (
|
|
"database/sql"
|
|
"testing"
|
|
|
|
_ "github.com/lib/pq"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"scroll-tech/common/testcontainers"
|
|
)
|
|
|
|
var (
|
|
testApps *testcontainers.TestcontainerApps
|
|
pgDB *sql.DB
|
|
)
|
|
|
|
func setupEnv(t *testing.T) {
|
|
// Start db container.
|
|
testApps = testcontainers.NewTestcontainerApps()
|
|
assert.NoError(t, testApps.StartPostgresContainer())
|
|
gormClient, err := testApps.GetGormDBClient()
|
|
assert.NoError(t, err)
|
|
pgDB, err = gormClient.DB()
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
defer func() {
|
|
if testApps != nil {
|
|
testApps.Free()
|
|
}
|
|
}()
|
|
m.Run()
|
|
}
|
|
|
|
func TestMigrate(t *testing.T) {
|
|
setupEnv(t)
|
|
t.Run("testCurrent", testCurrent)
|
|
t.Run("testStatus", testStatus)
|
|
t.Run("testResetDB", testResetDB)
|
|
t.Run("testMigrate", testMigrate)
|
|
t.Run("testRollback", testRollback)
|
|
}
|
|
|
|
func testCurrent(t *testing.T) {
|
|
cur, err := Current(pgDB)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), cur)
|
|
}
|
|
|
|
func testStatus(t *testing.T) {
|
|
status := Status(pgDB)
|
|
assert.NoError(t, status)
|
|
}
|
|
|
|
func testResetDB(t *testing.T) {
|
|
assert.NoError(t, ResetDB(pgDB))
|
|
cur, err := Current(pgDB)
|
|
assert.NoError(t, err)
|
|
// total number of tables.
|
|
assert.Equal(t, int64(22), cur)
|
|
}
|
|
|
|
func testMigrate(t *testing.T) {
|
|
assert.NoError(t, Migrate(pgDB))
|
|
cur, err := Current(pgDB)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(22), cur)
|
|
}
|
|
|
|
func testRollback(t *testing.T) {
|
|
version, err := Current(pgDB)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(22), version)
|
|
|
|
assert.NoError(t, Rollback(pgDB, nil))
|
|
|
|
cur, err := Current(pgDB)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, version, cur+1)
|
|
|
|
targetVersion := int64(0)
|
|
assert.NoError(t, Rollback(pgDB, &targetVersion))
|
|
|
|
cur, err = Current(pgDB)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), cur)
|
|
}
|