-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrations_test.go
61 lines (52 loc) · 1.07 KB
/
migrations_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
package testinfra_test
import (
"database/sql"
"testing"
_ "github.com/lib/pq"
"github.com/qiscus/testinfra"
)
func TestRunDBMigration(t *testing.T) {
dsn, close := testinfra.Postgres("")
defer close()
db, err := sql.Open("postgres", dsn)
if err != nil {
t.Fatal("cannot create connection to database")
}
// run the first migration
testinfra.RunDBMigration(dsn, "_fixtures/assets")
_, err = db.Exec(`
insert into users
values ('user1', 'password');
`)
if err != nil {
t.Error("error while querying database", err)
}
querySelect := `
select * from users
where username='user1';
`
rows, err := db.Query(querySelect)
if err != nil {
t.Error("error while querying database", err)
}
count := 0
for rows.Next() {
count++
}
if count != 1 {
t.Error("should have 1 row")
}
// reset database
testinfra.RunDBMigration(dsn, "_fixtures/assets")
rows, err = db.Query(querySelect)
if err != nil {
t.Error("error while querying database", err)
}
count = 0
for rows.Next() {
count++
}
if count != 0 {
t.Error("database must be cleared")
}
}