-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-deploy_test.go
122 lines (105 loc) · 2.61 KB
/
schema-deploy_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
package sqldb
import (
"testing"
"github.com/jmoiron/sqlx"
)
func TestDeploySchema(t *testing.T) {
c := NewSQLite(SQLiteInMemoryFilepathRaceSafe)
c.LoggingLevel = LogLevelDebug
createTable := `
CREATE TABLE IF NOT EXISTS users (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Username TEXT NOT NULL
)
`
c.DeployQueries = []string{createTable}
insertInitial := func(c *sqlx.DB) error {
insert := "INSERT INTO users (Username) VALUES (?)"
_, err := c.Exec(insert, "[email protected]")
return err
}
c.DeployFuncs = []QueryFunc{insertInitial}
opts := &DeploySchemaOptions{
CloseConnection: false,
}
err := c.DeploySchema(opts)
if err != nil {
t.Fatal(err)
return
}
if !c.Connected() {
t.Fatal("Connection should still be connected.")
return
}
//Try inserting.
insert2 := `INSERT INTO users (Username) VALUES (?)`
_, err = c.connection.Exec(insert2, "[email protected]")
if err != nil {
t.Fatal(err)
return
}
//Make sure results were inserted (DeployFunc and extra query).
q := "SELECT Count(ID) FROM Users"
var count int64
err = c.Connection().Get(&count, q)
if err != nil {
t.Fatal("Could not query.")
return
} else if count != 2 {
t.Fatal("Data not inserted correctly.", count)
return
}
//Try deploying to an already connected to db. This must fail.
err = c.DeploySchema(opts)
if err != ErrConnected {
t.Fatal("Error about db already connected should have occured.")
return
}
//Close connection
c.Close()
//Try deploying with an invalid config.
c.SQLitePath = ""
err = c.DeploySchema(opts)
if err == nil {
t.Fatal("Error about invalid config should have occured.")
return
}
//Try deploying with a bad deploy func.
c.SQLitePath = SQLiteInMemoryFilepathRaceSafe
insertInitial = func(c *sqlx.DB) error {
q := "SELECT INTO users VALUES (?)"
_, err := c.Exec(q, "[email protected]")
return err
}
c.DeployFuncs = []QueryFunc{insertInitial}
err = c.DeploySchema(opts)
if err == nil {
t.Fatal("Error should have occured because of bad deploy func.")
return
}
if c.Connected() {
t.Fatal("Connection should be closed after deploy func error.")
return
}
}
func TestDeploySchemaAndClose(t *testing.T) {
c := NewSQLite(SQLiteInMemoryFilepathRaceSafe)
createTable := `
CREATE TABLE IF NOT EXISTS users (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Username TEXT NOT NULL
)
`
c.DeployQueries = []string{createTable}
err := c.DeploySchema(nil)
if err != nil {
t.Fatal(err)
return
}
//Make sure connection is connected.
if c.Connected() {
t.Fatal("Connection should be connected")
}
//Close connection
c.Close()
}