forked from YuukiHogo/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
138 lines (118 loc) · 2.56 KB
/
testing.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
// Package testing has the database tests.
// All database drivers must pass the Test function.
// This lives in it's own package so it stays a test dependency.
package testing
import (
"bytes"
"fmt"
"io"
"testing"
"time"
"github.com/mattes/migrate/database"
)
// Test runs tests against database implementations.
func Test(t *testing.T, d database.Driver, migration []byte) {
if migration == nil {
panic("test must provide migration reader")
}
TestNilVersion(t, d) // test first
TestLockAndUnlock(t, d)
TestRun(t, d, bytes.NewReader(migration))
TestDrop(t, d)
TestSetVersion(t, d) // also tests Version()
}
func TestNilVersion(t *testing.T, d database.Driver) {
v, _, err := d.Version()
if err != nil {
t.Fatal(err)
}
if v != database.NilVersion {
t.Fatalf("Version: expected version to be NilVersion (-1), got %v", v)
}
}
func TestLockAndUnlock(t *testing.T, d database.Driver) {
// add a timeout, in case there is a deadlock
done := make(chan bool, 1)
go func() {
timeout := time.After(15 * time.Second)
for {
select {
case <-done:
return
case <-timeout:
panic(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d))
}
}
}()
defer func() {
done <- true
}()
// run the locking test ...
if err := d.Lock(); err != nil {
t.Fatal(err)
}
// try to acquire lock again
if err := d.Lock(); err == nil {
t.Fatal("Lock: expected err not to be nil")
}
// unlock
if err := d.Unlock(); err != nil {
t.Fatal(err)
}
// try to lock
if err := d.Lock(); err != nil {
t.Fatal(err)
}
if err := d.Unlock(); err != nil {
t.Fatal(err)
}
}
func TestRun(t *testing.T, d database.Driver, migration io.Reader) {
if migration == nil {
panic("migration can't be nil")
}
if err := d.Run(migration); err != nil {
t.Fatal(err)
}
}
func TestDrop(t *testing.T, d database.Driver) {
if err := d.Drop(); err != nil {
t.Fatal(err)
}
}
func TestSetVersion(t *testing.T, d database.Driver) {
if err := d.SetVersion(1, true); err != nil {
t.Fatal(err)
}
// call again
if err := d.SetVersion(1, true); err != nil {
t.Fatal(err)
}
v, dirty, err := d.Version()
if err != nil {
t.Fatal(err)
}
if !dirty {
t.Fatal("expected dirty")
}
if v != 1 {
t.Fatal("expected version to be 1")
}
if err := d.SetVersion(2, false); err != nil {
t.Fatal(err)
}
// call again
if err := d.SetVersion(2, false); err != nil {
t.Fatal(err)
}
v, dirty, err = d.Version()
if err != nil {
t.Fatal(err)
}
if dirty {
t.Fatal("expected not dirty")
}
if v != 2 {
t.Fatal("expected version to be 2")
}
}