-
Notifications
You must be signed in to change notification settings - Fork 2
/
statement_test.go
89 lines (67 loc) · 2.63 KB
/
statement_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
package fireboltgosdk
import (
"context"
"database/sql/driver"
"testing"
)
type driverExecerMock struct {
callCount int
lastQuery string
}
func (c *driverExecerMock) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
c.callCount += 1
c.lastQuery = query
return nil, nil
}
type driverQueryerMock struct {
callCount int
lastQuery string
}
func (c *driverQueryerMock) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
c.callCount += 1
c.lastQuery = query
return nil, nil
}
// TestExecStmt tests that Exec and ExecContext actually calls execer
func TestExecStmt(t *testing.T) {
var queryer driverQueryerMock
var execer driverExecerMock
sql := "SELECT 1 UNION SELECT 2"
stmt := fireboltStmt{queryer: &queryer, execer: &execer, query: sql}
_, err := stmt.Query(nil)
assert(err, nil, t, "queryer returned an error, but shouldn't")
assert(queryer.callCount, 1, t, "queryer wasn't called")
assert(queryer.lastQuery, sql, t, "queryer was called with wrong sql")
_, err = stmt.QueryContext(context.TODO(), nil)
assert(err, nil, t, "queryer returned an error, but shouldn't")
assert(queryer.callCount, 2, t, "queryer wasn't called")
assert(queryer.lastQuery, sql, t, "queryer was called with wrong sql")
assert(execer.callCount, 0, t, "execer was called, but shouldn't")
}
// TestQueryStmt tests that Query and QueryContext actually calls queryer
func TestQueryStmt(t *testing.T) {
var queryer driverQueryerMock
var execer driverExecerMock
sql := "SELECT 1 UNION SELECT 2"
stmt := fireboltStmt{queryer: &queryer, execer: &execer, query: sql}
_, err := stmt.Exec(nil)
assert(err, nil, t, "execer returned an error, but shouldn't")
assert(execer.callCount, 1, t, "execer wasn't called")
assert(execer.lastQuery, sql, t, "execer was called with wrong sql")
_, err = stmt.ExecContext(context.TODO(), nil)
assert(err, nil, t, "execer returned an error, but shouldn't")
assert(execer.callCount, 2, t, "execer wasn't called")
assert(execer.lastQuery, sql, t, "execer was called with wrong sql")
assert(queryer.callCount, 0, t, "queryer was called, but shouldn't")
}
// TestCloseStmt checks, that closing connection resets all variable, and makes statement not usable
func TestCloseStmt(t *testing.T) {
var queryer driverQueryerMock
var execer driverExecerMock
sql := "SELECT 1 UNION SELECT 2"
stmt := fireboltStmt{queryer: &queryer, execer: &execer, query: sql}
stmt.Close()
assert(stmt.execer, nil, t, "execer wasn't reset by stmt")
assert(stmt.queryer, nil, t, "queryer wasn't reset by stmt")
assert(stmt.query, "", t, "query wasn't reset by stmt")
}