-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfwish_db_test.go
135 lines (119 loc) · 2.96 KB
/
fwish_db_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
package fwish_test
import (
"database/sql"
"os"
"testing"
"github.com/rez-go/fwish"
sqlsource "github.com/rez-go/fwish/sources/sql"
)
//TODO:
// - we might want to have the migration file contents here in the code.
// if we need the files, we make a temp dir and write the files there.
// this way, we can be sure that the source files are always consistent.
// - test with search_path set to other schema
// - test with SQLs which use schema explicitly (other than defined schema)
// - test multiple sources
var testDBDSN = "postgres:///?sslmode=disable"
// The schema name we use in the tests. This schema will be dropped
// and recreated multiple times.
var testDBSchemaName = "__fwishtest"
func init() {
if s, exists := os.LookupEnv("TEST_DB_DSN"); exists {
testDBDSN = s
}
if s, exists := os.LookupEnv("TEST_DB_SCHEMA_NAME"); exists {
testDBSchemaName = s
}
}
// Very basic functional test
func TestBasic(t *testing.T) {
mg, err := fwish.NewMigrator("372ce18d-02a2-4cb1-828a-bb470f02fe6e")
if err != nil {
t.Fatal(err)
}
src, err := sqlsource.LoadDir("./test-data/basic")
if err != nil {
t.Fatal(err)
}
err = mg.AddSource(src)
if err != nil {
t.Fatal(err)
}
db, err := sql.Open("postgres", testDBDSN)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`DROP SCHEMA IF EXISTS ` + testDBSchemaName + ` CASCADE`)
if err != nil {
t.Fatal(err)
}
//TODO: check the number of applied migrations
_, err = mg.Migrate(db, testDBSchemaName)
if err != nil {
t.Fatal(err)
}
//TODO: validate
// Apply the migration again
n, err := mg.Migrate(db, testDBSchemaName)
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Fatalf("0 expected, got %d", n)
}
//TODO: should be a defered statement
_, err = db.Exec(`DROP SCHEMA ` + testDBSchemaName + ` CASCADE`)
if err != nil {
t.Fatal(err)
}
}
// Using sqlx instead of stdlib's sql package.
func TestBasicSQLX(t *testing.T) {
mg, err := fwish.NewMigrator("372ce18d-02a2-4cb1-828a-bb470f02fe6e")
if err != nil {
t.Fatal(err)
}
src, err := sqlsource.LoadDir("./test-data/basic")
if err != nil {
t.Fatal(err)
}
err = mg.AddSource(src)
if err != nil {
t.Fatal(err)
}
// The only difference
db, err := sql.Open("postgres", testDBDSN)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`DROP SCHEMA IF EXISTS ` + testDBSchemaName + ` CASCADE`)
if err != nil {
t.Fatal(err)
}
//TODO: check the number of applied migrations
_, err = mg.Migrate(db, testDBSchemaName)
if err != nil {
t.Fatal(err)
}
//TODO: validate
// Apply the migration again
n, err := mg.Migrate(db, testDBSchemaName)
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Fatalf("0 expected, got %d", n)
}
//TODO: should be a defered statement
_, err = db.Exec(`DROP SCHEMA ` + testDBSchemaName + ` CASCADE`)
if err != nil {
t.Fatal(err)
}
}
func TestSchemaID(t *testing.T) {
//TODO: test the one in the DB
}
func TestBadRank(t *testing.T) {
//TODO:
// apply migrations, alter or delete a row from meta table, then validate
}