-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathmigrate_test.go
More file actions
89 lines (73 loc) · 1.76 KB
/
migrate_test.go
File metadata and controls
89 lines (73 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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(28), cur)
}
func testMigrate(t *testing.T) {
assert.NoError(t, Migrate(pgDB))
cur, err := Current(pgDB)
assert.NoError(t, err)
assert.Equal(t, int64(28), cur)
}
func testRollback(t *testing.T) {
version, err := Current(pgDB)
assert.NoError(t, err)
assert.Equal(t, int64(28), 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)
}