-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
oci8_test.go
213 lines (181 loc) · 6.47 KB
/
oci8_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
package oci8
import (
"bytes"
"context"
"database/sql"
"flag"
"fmt"
"os"
"reflect"
"sync"
"testing"
"time"
)
// to run database tests
// go test -v github.com/mattn/go-oci8 -args -disableDatabase=false -hostValid type_hostname_here -username type_username_here -password "type_password_here"
// look at test.sh for Oracle user setup example
var (
TestDisableDatabase bool
TestHostValid string
TestHostInvalid string
TestUsername string
TestPassword string
TestContextTimeoutString string
TestContextTimeout time.Duration
TestDatabase string
TestDisableDestructive bool
TestTimeString string
TestDB *sql.DB
TestTypeTime = reflect.TypeOf(time.Time{})
TestTypeByteSlice = reflect.TypeOf([]byte{})
testString1 string
testByteSlice2000 []byte
testByteSlice4000 []byte
testByteSlice32767 []byte
testByteSlice65535 []byte
testByteSlice70000 []byte
benchmarkSelectTableName string
benchmarkSelectTableOnce sync.Once
benchmarkSelectTableCreated bool
)
// testExecResults is for testing an exec query with many sets of args
type testExecResults struct {
query string
execResults []testExecResult
}
// testExecResult is for testing an exec query with single set of args
type testExecResult struct {
args map[string]sql.Out
results map[string]interface{}
}
// testQueryResults is for testing a query with many sets of args
type testQueryResults struct {
query string
queryResults []testQueryResult
}
// testQueryResult is for testing a query with single set of args
type testQueryResult struct {
args []interface{}
results [][]interface{}
}
// TestMain sets up testing
func TestMain(m *testing.M) {
code := setupForTesting()
if code != 0 {
os.Exit(code)
}
code = m.Run()
// drop benchmark select table
if benchmarkSelectTableCreated {
func() {
tableName := benchmarkSelectTableName
query := "drop table " + tableName
ctx, cancel := context.WithTimeout(context.Background(), TestContextTimeout)
stmt, err := TestDB.PrepareContext(ctx, query)
cancel()
if err != nil {
fmt.Println("prepare error:", err)
return
}
ctx, cancel = context.WithTimeout(context.Background(), TestContextTimeout)
_, err = stmt.ExecContext(ctx)
cancel()
if err != nil {
stmt.Close()
fmt.Println("exec error:", err)
return
}
err = stmt.Close()
if err != nil {
fmt.Println("stmt close error:", err)
return
}
}()
}
if !TestDisableDatabase {
err := TestDB.Close()
if err != nil {
fmt.Println("close error:", err)
os.Exit(2)
}
}
os.Exit(code)
}
// setupForTesting sets up flags and connects to test database
func setupForTesting() int {
flag.BoolVar(&TestDisableDatabase, "disableDatabase", true, "set to true to disable the Oracle tests")
flag.StringVar(&TestHostValid, "hostValid", "127.0.0.1", "a host where a Oracle database is running")
flag.StringVar(&TestHostInvalid, "hostInvalid", "169.254.200.200", "a host where a Oracle database is not running")
flag.StringVar(&TestUsername, "username", "", "the username for the Oracle database")
flag.StringVar(&TestPassword, "password", "", "the password for the Oracle database")
flag.StringVar(&TestContextTimeoutString, "contextTimeout", "30s", "the context timeout for queries")
flag.BoolVar(&TestDisableDestructive, "disableDestructive", false, "set to true to disable the destructive Oracle tests")
flag.Parse()
var err error
TestContextTimeout, err = time.ParseDuration(TestContextTimeoutString)
if err != nil {
fmt.Println("parse context timeout error:", err)
return 4
}
if !TestDisableDatabase {
TestDB = testGetDB("")
if TestDB == nil {
return 6
}
}
TestTimeString = time.Now().UTC().Format("20060102150405")
var i int
var buffer bytes.Buffer
for i = 0; i < 1000; i++ {
buffer.WriteRune(rune(i))
}
testString1 = buffer.String()
testByteSlice2000 = make([]byte, 2000)
for i = 0; i < 2000; i++ {
testByteSlice2000[i] = byte(i)
}
testByteSlice4000 = make([]byte, 4000)
for i = 0; i < 4000; i++ {
testByteSlice4000[i] = byte(i)
}
testByteSlice32767 = make([]byte, 32767)
for i = 0; i < 32767; i++ {
testByteSlice32767[i] = byte(i)
}
testByteSlice65535 = make([]byte, 65535)
for i = 0; i < 65535; i++ {
testByteSlice65535[i] = byte(i)
}
testByteSlice70000 = make([]byte, 70000)
for i = 0; i < 70000; i++ {
testByteSlice70000[i] = byte(i)
}
return 0
}
// TestParseDSN tests parsing the DSN
func TestParseDSN(t *testing.T) {
t.Parallel()
const prefetchRows = 0
const prefetchMemory = 4096
const stmtCacheSize = 0
var dsnTests = []struct {
dsnString string
expectedDSN *DSN
}{
{"oracle://xxmc:[email protected]:1521/ORCL?loc=America%2FPhoenix", &DSN{Username: "xxmc", Password: "xxmc", Connect: "107.20.30.169:1521/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: stmtCacheSize, timeLocation: timeLocations[5]}},
{"xxmc/[email protected]:1521/ORCL?loc=America%2FPhoenix", &DSN{Username: "xxmc", Password: "xxmc", Connect: "107.20.30.169:1521/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: stmtCacheSize, timeLocation: timeLocations[5]}},
{"sys/[email protected]:1521/ORCL?loc=America%2FPhoenix&as=sysdba", &DSN{Username: "sys", Password: "syspwd", Connect: "107.20.30.169:1521/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: stmtCacheSize, timeLocation: timeLocations[5], operationMode: 0x00000002}}, // with operationMode: 0x00000002 = C.OCI_SYDBA
{"xxmc/[email protected]:1521/ORCL", &DSN{Username: "xxmc", Password: "xxmc", Connect: "107.20.30.169:1521/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: stmtCacheSize, timeLocation: time.UTC}},
{"xxmc/[email protected]/ORCL", &DSN{Username: "xxmc", Password: "xxmc", Connect: "107.20.30.169/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: stmtCacheSize, timeLocation: time.UTC}},
{"xxmc/[email protected]/ORCL?stmt_cache_size=50", &DSN{Username: "xxmc", Password: "xxmc", Connect: "107.20.30.169/ORCL", prefetchRows: prefetchRows, prefetchMemory: prefetchMemory, stmtCacheSize: 50, timeLocation: time.UTC}},
}
for _, tt := range dsnTests {
actualDSN, err := ParseDSN(tt.dsnString)
if err != nil {
t.Errorf("ParseDSN(%s) got error: %+v", tt.dsnString, err)
}
if !reflect.DeepEqual(actualDSN, tt.expectedDSN) {
t.Errorf("ParseDSN(%s): expected %+v, actual %+v", tt.dsnString, tt.expectedDSN, actualDSN)
}
}
}