-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
159 lines (140 loc) · 3.06 KB
/
main_test.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package migs
import (
"database/sql"
"os"
"testing"
_ "github.com/lib/pq"
)
func insertUsers(con Con) error {
_, err := con.Exec("INSERT INTO users (name) VALUES ($1)", "Linux")
return err
}
func databaseURL() string {
env := os.Getenv("TEST_DATABSE_URL")
if env != "" {
return env
}
return "postgres://localhost/migs_test?sslmode=disable"
}
func testConnect(t *testing.T) *sql.Tx {
db, err := sql.Open("postgres", databaseURL())
if err != nil {
t.Fatal(err)
}
if err := db.Ping(); err != nil {
t.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
return tx
}
func TestPending(t *testing.T) {
if os.Getenv("TEST_WITH_DB") != "true" {
t.SkipNow()
}
migs := New(
"CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, name VARCHAR NOT NULL)",
//insertUsers,
)
tx := testConnect(t)
defer tx.Rollback()
_, err := migs.setup(tx)
if err != nil {
t.Fatal(err)
}
pending, err := migs.Pending(tx)
if err != nil {
t.Fatal(err)
}
if len(pending) != 1 {
t.Errorf("expected 1 pending, was %d", len(pending))
}
err = migs.ExecuteTx(tx)
if err != nil {
t.Fatal(err)
}
migs = New(
"CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, name VARCHAR NOT NULL)",
insertUsers,
)
pending, err = migs.Pending(tx)
if err != nil {
t.Fatal(err)
}
if len(pending) != 1 {
t.Errorf("expected 1 pending, was %d", len(pending))
}
}
func TestRun(t *testing.T) {
if os.Getenv("TEST_WITH_DB") != "true" {
t.SkipNow()
}
migs := New(
"CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, name VARCHAR NOT NULL)",
insertUsers,
)
tx := testConnect(t)
defer tx.Rollback()
err := migs.ExecuteTx(tx)
if err != nil {
t.Errorf("error running migrations: %s", err)
}
var cnt int
if err := tx.QueryRow("SELECT COUNT(1) FROM users").Scan(&cnt); err != nil {
t.Fatal(err)
}
var name string
if err := tx.QueryRow("SELECT name FROM users").Scan(&name); err != nil {
t.Fatal(err)
}
all, err := migs.All(tx)
if err != nil {
t.Fatal(err)
}
tests := map[int]struct{ Has, Want interface{} }{
1: {cnt, 1},
2: {name, "Linux"},
3: {len(all), 2},
4: {all[0].Executed, true},
5: {all[1].Executed, true},
}
for i, tc := range tests {
if tc.Has != tc.Want {
t.Errorf("%d: want=%#v has=%#v", i, tc.Want, tc.Has)
}
}
}
func TestMigrations(t *testing.T) {
if os.Getenv("TEST_WITH_DB") != "true" {
t.SkipNow()
}
tx := testConnect(t)
defer tx.Rollback()
migs := New("CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, email VARCHAR)", migFunc)
_, err := migs.setup(tx)
if err != nil {
t.Fatal(err)
}
all, err := migs.All(tx)
if err != nil {
t.Fatal(err)
}
tests := map[int]struct{ Has, Want interface{} }{
1: {len(all), 2},
2: {all[0].Idx, 1},
3: {all[0].Statement, "CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, email VARCHAR)"},
4: {all[1].Idx, 2},
5: {all[1].Statement, "github.com/dynport/migs.migFunc"},
6: {all[0].Executed, false},
}
for i, tc := range tests {
if tc.Has != tc.Want {
t.Errorf("%d: want=%#v has=%#v", i, tc.Want, tc.Has)
}
}
}
func migFunc(tx Con) error {
return nil
}