-
Notifications
You must be signed in to change notification settings - Fork 22
/
schema_test.go
238 lines (204 loc) · 6.04 KB
/
schema_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package schema_test
import (
"database/sql"
"database/sql/driver"
"log"
"strings"
"github.com/jimsmart/schema"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type testParams struct {
DriverName string
ConnStr string
CreateDDL []string
DropDDL []string
DropFn func()
TableExpRes []string
ViewExpRes []string
TableNamesExpRes [][2]string
ViewNamesExpRes [][2]string
PrimaryKeysExpRes []string
}
func SchemaTestRunner(params *testParams) {
setup := func() (*sql.DB, func()) {
db, err := sql.Open(params.DriverName, params.ConnStr)
if err != nil {
log.Fatalf("sql.Open error %v", err)
}
for _, ddl := range params.CreateDDL {
_, err = db.Exec(ddl)
if err != nil {
// log.Fatalf("db.Exec (create) error %v", err)
log.Printf("db.Exec (create) error %v exec %s", err, ddl)
}
}
doneFn := func() {
for _, ddl := range params.DropDDL {
_, err = db.Exec(ddl)
if err != nil {
// log.Fatalf("db.Exec (drop) error %v", err)
log.Printf("db.Exec (drop) error %v exec %s", err, ddl)
}
}
err = db.Close()
if err != nil {
log.Printf("db.Close error %v", err)
}
if params.DropFn != nil {
params.DropFn()
}
}
return db, doneFn
}
// TODO(js) We should test Tables and Views against empty databases.
Describe("ColumnTypes", func() {
It("should return the column type info for an existing table", func() {
db, done := setup()
defer done()
ci, err := schema.ColumnTypes(db, params.TableNamesExpRes[1][0], params.TableNamesExpRes[1][1])
Expect(err).To(BeNil())
var list []string
for _, c := range ci {
list = append(list, c.Name())
}
Expect(list).To(Equal(params.TableExpRes))
})
It("should return the column type info for an existing table with empty schema param", func() {
db, done := setup()
defer done()
ci, err := schema.ColumnTypes(db, "", params.TableNamesExpRes[1][1])
Expect(err).To(BeNil())
var list []string
for _, c := range ci {
list = append(list, c.Name())
}
Expect(list).To(Equal(params.TableExpRes))
})
It("should return an error for a non-existing table", func() {
db, done := setup()
defer done()
_, err := schema.ColumnTypes(db, "", "XXX-NO-SUCH-TABLE-XXX")
Expect(err).ToNot(BeNil())
})
It("should return the column type info for the view", func() {
db, done := setup()
defer done()
ci, err := schema.ColumnTypes(db, params.ViewNamesExpRes[0][0], params.ViewNamesExpRes[0][1])
Expect(err).To(BeNil())
var list []string
for _, c := range ci {
list = append(list, c.Name())
}
Expect(list).To(Equal(params.ViewExpRes))
})
// TODO(js) check with empty schema param
It("should return the column type info for the view with empty schema param", func() {
db, done := setup()
defer done()
ci, err := schema.ColumnTypes(db, "", params.ViewNamesExpRes[0][1])
Expect(err).To(BeNil())
var list []string
for _, c := range ci {
list = append(list, c.Name())
}
Expect(list).To(Equal(params.ViewExpRes))
})
})
Describe("TableNames", func() {
It("should return the table names", func() {
db, done := setup()
defer done()
sn, err := schema.TableNames(db)
Expect(err).To(BeNil())
Expect(sn).To(Equal(params.TableNamesExpRes))
})
})
Describe("Tables", func() {
It("should return the column type info for all tables", func() {
db, done := setup()
defer done()
sc, err := schema.Tables(db)
Expect(err).To(BeNil())
Expect(sc).To(HaveLen(2))
// TODO(js) Improve / cleanup tests.
// Expect(sc).To(HaveKey())
ci, ok := sc[params.TableNamesExpRes[1]]
Expect(ok).To(BeTrue())
Expect(ci).To(HaveLen(10))
})
})
Describe("ViewNames", func() {
It("should return the view names", func() {
db, done := setup()
defer done()
sn, err := schema.ViewNames(db)
Expect(err).To(BeNil())
Expect(sn).To(Equal(params.ViewNamesExpRes))
})
})
Describe("Views", func() {
It("should return the column type info for all views", func() {
db, done := setup()
defer done()
sc, err := schema.Views(db)
Expect(err).To(BeNil())
Expect(sc).To(HaveLen(1))
ci, ok := sc[params.ViewNamesExpRes[0]]
Expect(ok).To(BeTrue())
Expect(ci).To(HaveLen(2))
})
})
Describe("PrimaryKey", func() {
It("should return the primary key", func() {
db, done := setup()
defer done()
pk, err := schema.PrimaryKey(db, params.TableNamesExpRes[0][0], params.TableNamesExpRes[0][1])
Expect(err).To(BeNil())
Expect(pk).To(Equal(params.PrimaryKeysExpRes))
})
It("should return the primary key when schema param is empty", func() {
db, done := setup()
defer done()
pk, err := schema.PrimaryKey(db, "", params.TableNamesExpRes[0][1])
Expect(err).To(BeNil())
Expect(pk).To(Equal(params.PrimaryKeysExpRes))
})
})
}
var _ = Describe("schema", func() {
Context("using an unsupported (fake) db driver", func() {
sql.Register("fakedb", FakeDb{})
db, _ := sql.Open("fakedb", "")
It("should return errors for every method", func() {
var unknownDriverErr = schema.UnknownDriverError{Driver: "schema_test.FakeDb"}
ci, err := schema.ColumnTypes(db, "", "web_resource")
Expect(ci).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
tn, err := schema.TableNames(db)
Expect(tn).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
ta, err := schema.Tables(db)
Expect(ta).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
vn, err := schema.ViewNames(db)
Expect(vn).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
vw, err := schema.Views(db)
Expect(vw).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
pk, err := schema.PrimaryKey(db, "", "web_resource")
Expect(pk).To(BeNil())
Expect(err).To(MatchError(unknownDriverErr))
Expect(err.Error()).To(Equal("unknown database driver: schema_test.FakeDb"))
})
})
})
type FakeDb struct{}
func (_ FakeDb) Open(name string) (driver.Conn, error) {
return nil, nil
}
// pack a string, normalising its whitespace.
func pack(s string) string {
return strings.Join(strings.Fields(s), " ")
}