/
upgrade_test.go
100 lines (85 loc) · 3.07 KB
/
upgrade_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
package upgrade
import (
"testing"
"github.com/halverneus/sample/common/db"
"github.com/halverneus/sample/sql/usersql"
)
// NOTE: Testing requires a schema that can be accessed with the following
// settings prior to execution:
var (
host = "127.0.0.1"
port = 3306
schema = "sample"
user = "sample"
password = "sample"
)
func TestDatabaseUpgrade(t *testing.T) {
// Happy path...
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to connect to database: %v", err)
}
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to reconnect to database: %v", err)
}
if err := Database(); nil != err {
t.Errorf("Database upgrade failed: %v", err)
}
// Error path...
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to reconnect to database: %v", err)
}
if err := db.Execute(usersql.CreateEncryptionFunction); nil != err {
t.Errorf("Unable to add pre-existing function: %v", err)
}
if err := Database(); nil == err {
t.Error("Database upgrade succeeded when it should have failed")
}
// Restore database...
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
}
func TestAllUpgrade(t *testing.T) {
// Happy path...
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to connect to database: %v", err)
}
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to reconnect to database: %v", err)
}
if err := All(); nil != err {
t.Errorf("All upgrade failed: %v", err)
}
// Error path...
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
if err := db.Connect(host, port, schema, user, password); nil != err {
t.Errorf("Unable to reconnect to database: %v", err)
}
if err := db.Execute(usersql.CreateEncryptionFunction); nil != err {
t.Errorf("Unable to add pre-existing function: %v", err)
}
if err := All(); nil == err {
t.Error("All upgrade succeeded when it should have failed")
}
// Restore database...
db.Execute("DROP SCHEMA sample")
if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
t.Errorf("Unable to create database: %v", err)
}
}