This repository has been archived by the owner. It is now read-only.
forked from nakagami/firebirdsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_test.go
370 lines (322 loc) · 10.7 KB
/
driver_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2013-2016 Hajime Nakagami
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"database/sql"
"reflect"
"testing"
"time"
)
func TestBasic(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_basic.fdb")
defer conn.Close()
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
var sql string
var n int
sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='FOO'"
err = conn.QueryRow(sql).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n > 0 {
conn.Exec("DROP TABLE foo")
}
sql = `
CREATE TABLE foo (
a INTEGER NOT NULL,
b VARCHAR(30) NOT NULL UNIQUE,
c VARCHAR(1024),
d DECIMAL(16,3) DEFAULT -0.123,
e DATE DEFAULT '1967-08-11',
f TIMESTAMP DEFAULT '1967-08-11 23:45:01',
g TIME DEFAULT '23:45:01',
h BLOB SUB_TYPE 1,
i DOUBLE PRECISION DEFAULT 0.0,
j FLOAT DEFAULT 0.0,
PRIMARY KEY (a),
CONSTRAINT CHECK_A CHECK (a <> 0)
)
`
conn.Exec(sql)
_, err = conn.Exec("CREATE TABLE foo (a INTEGER)")
if err == nil {
t.Fatalf("Need metadata update error")
}
if err.Error()[:29] != "unsuccessful metadata update\n" {
t.Fatalf("Bad message:%v", err.Error())
}
// 3 records insert
conn.Exec("insert into foo(a, b, c, h) values (1, 'a', 'b','This is a memo')")
conn.Exec("insert into foo(a, b, c, e, g, i, j) values (2, 'A', 'B', '1999-01-25', '00:00:01', 0.1, 0.1)")
conn.Exec("insert into foo(a, b, c, e, g, i, j) values (3, 'X', 'Y', '2001-07-05', '00:01:02', 0.2, 0.2)")
err = conn.QueryRow("select count(*) cnt from foo").Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n != 3 {
t.Fatalf("Error bad record count: %v", n)
}
rows, err := conn.Query("select a, b, c, d, e, f, g, h, i, j from foo")
var a int
var b, c string
var d float64
var e time.Time
var f time.Time
var g time.Time
var h []byte
var i float64
var j float32
for rows.Next() {
rows.Scan(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j)
}
stmt, _ := conn.Prepare("select count(*) from foo where a=? and b=? and d=? and e=? and f=? and g=?")
ep := time.Date(1967, 8, 11, 0, 0, 0, 0, time.UTC)
fp := time.Date(1967, 8, 11, 23, 45, 1, 0, time.UTC)
gp, err := time.Parse("15:04:05", "23:45:01")
err = stmt.QueryRow(1, "a", -0.123, ep, fp, gp).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n != 1 {
t.Fatalf("Error bad record count: %v", n)
}
}
func TestReturning(t *testing.T) {
conn, _ := sql.Open("firebirdsql_createdb", "SYSDBA:masterkey@localhost:3050/tmp/go_test_returning.fdb")
defer conn.Close()
conn.Exec(`
CREATE TABLE test_returning (
f1 integer NOT NULL,
f2 integer default 2,
f3 varchar(20) default 'abc')`)
conn.Close()
conn, _ = sql.Open("firebirdsql", "SYSDBA:masterkey@localhost:3050/tmp/go_test_returning.fdb")
for i := 0; i < 2; i++ {
rows, err := conn.Query("INSERT INTO test_returning (f1) values (1) returning f2, f3")
if err != nil {
t.Fatalf("Error Insert returning : %v", err)
}
var f2 int
var f3 string
rows.Next()
rows.Scan(&f2, &f3)
if f2 != 2 || f3 != "abc" {
t.Fatalf("Bad value insert returning: %v,%v", f2, f3)
}
}
}
func TestInsertBlobsWithParams(t *testing.T) {
conn, _ := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_insert_blobs_with_params.fdb")
conn.Exec("CREATE TABLE test_blobs (f1 BLOB SUB_TYPE 0, f2 BLOB SUB_TYPE 1)")
defer conn.Close()
s0 := "Test Text"
b0 := []byte{0, 1, 2, 3, 4}
if _, err := conn.Exec("INSERT INTO test_blobs (f1, f2) values (?, ?)", s0, b0); err != nil {
t.Fatalf("Error inserting blobs with params: %v", err)
}
var s string
var b []byte
err := conn.QueryRow("SELECT f1, f2 from test_blobs").Scan(&s, &b)
if err != nil {
t.Fatalf("Error in query: %v", err)
}
if s != s0 {
t.Fatalf("Text blob: expected <%s>, got <%s>", s0, s)
}
if !reflect.DeepEqual(b, b0) {
t.Fatalf("Binary blob: expected <%v>, got <%v> (%s)", b0, b, string(b))
}
}
func TestError(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_error.fdb")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
_, err = conn.Exec("incorrect sql statement")
if err == nil || err.Error() != "Dynamic SQL Error\nSQL error code = -104\nToken unknown - line 1, column 1\nincorrect\n" {
t.Fatalf("Incorrect error")
}
}
func TestRole(t *testing.T) {
conn1, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_role.fdb")
if err != nil {
t.Fatalf("Error creating: %v", err)
}
conn1.Exec("CREATE TABLE test_role (f1 integer)")
conn1.Exec("INSERT INTO test_role (f1) values (1)")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
conn1.Exec("CREATE ROLE DRIVERROLE")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT DRIVERROLE TO SYSDBA")
if err != nil {
t.Fatalf("Error creating role: %v", err)
}
conn1.Exec("GRANT SELECT ON test_role TO DRIVERROLE")
if err != nil {
t.Fatalf("Error granting right to role: %v", err)
}
conn1.Close()
conn2, err := sql.Open("firebirdsql", "sysdba:masterkey@localhost:3050/tmp/go_test_role.fdb?role=driverrole")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
rows, err := conn2.Query("SELECT f1 FROM test_role")
defer conn2.Close()
if err != nil {
t.Fatalf("Error Query: %v", err)
}
for rows.Next() {
}
}
func TestInsertTimestamp(t *testing.T) {
const (
sqlSchema = "CREATE TABLE TEST (VAL1 TIMESTAMP, VAL2 TIMESTAMP, VAL3 TIMESTAMP, VAL4 TIMESTAMP);"
sqlInsert = "INSERT INTO TEST (VAL1, VAL2, VAL3, VAL4) VALUES (?, ?, ?, '2015/2/9 19:25:50.7405');"
sqlSelect = "SELECT * FROM TEST;"
)
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_timestamp.fdb")
if err != nil {
t.Fatalf("Error creating: %v", err)
}
defer conn.Close()
_, err = conn.Exec(sqlSchema)
if err != nil {
t.Fatalf("Error creating schema: %v", err)
}
dt1 := time.Date(2015, 2, 9, 19, 25, 50, 740500000, time.UTC)
dt2 := "2015/2/9 19:25:50.7405"
dt3 := "2015-2-9 19:25:50.7405"
if _, err = conn.Exec(sqlInsert, dt1, dt2, dt3); err != nil {
t.Fatalf("Error executing insert: %s", err)
}
var rt1, rt2, rt3, rt4 time.Time
err = conn.QueryRow(sqlSelect).Scan(&rt1, &rt2, &rt3, &rt4)
if err != nil {
t.Fatalf("Unexpected error in select: %s", err)
}
if rt1 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt1)
}
if rt2 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt2)
}
if rt3 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt3)
}
if rt4 != dt1 {
t.Errorf("Expected <%v>, got <%v>", dt1, rt4)
}
}
/*
func TestBoolean(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_fb3.fdb")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
var sql string
var n int
sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='TEST_FB3'"
err = conn.QueryRow(sql).Scan(&n)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if n > 0 {
conn.Exec("DROP TABLE test_fb3")
}
sql = `
CREATE TABLE test_fb3 (
b BOOLEAN
)
`
conn.Exec(sql)
conn.Exec("insert into test_fb3(b) values (true)")
conn.Exec("insert into test_fb3(b) values (false)")
var b bool
err = conn.QueryRow("select * from test_fb3 where b is true").Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != true{
conn.Exec("Invalid boolean value")
}
err = conn.QueryRow("select * from test_fb3 where b is false").Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != false{
conn.Exec("Invalid boolean value")
}
stmt, _ := conn.Prepare("select * from test_fb3 where b=?")
err = stmt.QueryRow(true).Scan(&b)
if err != nil {
t.Fatalf("Error QueryRow: %v", err)
}
if b != false{
conn.Exec("Invalid boolean value")
}
defer conn.Close()
}
*/
func TestLegacyAuthWireCrypt(t *testing.T) {
var n int
conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test_connect.fdb")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
conn, err = sql.Open("firebirdsql", "sysdba:masterkey@localhost:3050/tmp/go_test_connect.fdb?auth_plugin_anme=Legacy_Auth")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
conn, err = sql.Open("firebirdsql", "sysdba:masterkey@localhost:3050/tmp/go_test_connect.fdb?wire_crypt=false")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
conn, err = sql.Open("firebirdsql", "sysdba:masterkey@localhost:3050/tmp/go_test_connect.fdb?auth_plugin_name=Legacy_Auth&wire_auth=true")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
conn.Close()
conn, err = sql.Open("firebirdsql", "sysdba:masterkey@localhost:3050/tmp/go_test_connect.fdb?auth_plugin_name=Legacy_Auth&wire_auth=false")
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
conn.Close()
}
func TestErrorConnect(t *testing.T) {
var n int
conn, err := sql.Open("firebirdsql", "foo:bar@something_wrong_hostname:3050/dbname")
if err != nil {
t.Fatalf("Error occured at sql.Open()")
}
err = conn.QueryRow("SELECT Count(*) FROM rdb$relations").Scan(&n)
if err == nil {
t.Fatalf("Error not occured")
}
conn.Close()
}