-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqldb_test.go
274 lines (248 loc) · 5.28 KB
/
mysqldb_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
package mysql
import (
"fmt"
di "github.com/Ulbora/dbinterface"
"strconv"
"testing"
_ "github.com/go-sql-driver/mysql"
)
var dbi di.Database
var dbi2 di.Database
var iid1 int64
func TestMySQLDb_Connectfail(t *testing.T) {
var mdb MyDB
mdb.Host = "localhost:3306"
mdb.User = "admin"
mdb.Password = "admin1"
mdb.Database = "testdb1"
// dbi = &mdb
dbi = mdb.GetNewDatabase()
suc := dbi.Connect()
if suc {
t.Fail()
}
}
func TestMySQLDb_Connect(t *testing.T) {
var mdb MyDB
mdb.Host = "localhost:3306"
mdb.User = "admin"
mdb.Password = "admin"
mdb.Database = "testdb"
// dbi = &mdb
dbi = mdb.GetNewDatabase()
suc := dbi.Connect()
if !suc {
t.Fail()
}
}
func TestMySQLDb_Connect2(t *testing.T) {
var mdb MyDB
mdb.Host = "localhost:3306"
mdb.User = "admin"
mdb.Password = "admin"
mdb.Database = "ffl_list_10012018"
// dbi2 = &mdb
dbi2 = mdb.GetNewDatabase()
suc := dbi2.Connect()
if !suc {
t.Fail()
}
}
func TestMySQLDb_Test(t *testing.T) {
var rtn bool
var q = "select count(*) from test "
var a []interface{}
rowPtr := dbi.Test(q, a...)
if rowPtr != nil {
foundRow := rowPtr.Row
int64Val, err2 := strconv.ParseInt(foundRow[0], 10, 0)
fmt.Print("Records found during test ")
fmt.Println(int64Val)
if err2 != nil {
fmt.Print(err2)
}
if int64Val >= 0 {
rtn = true
}
}
if !rtn {
t.Fail()
}
}
func TestMySQLDb_Insertfail(t *testing.T) {
var q = "insert into test22 (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
suc, id := dbi.Insert(q, a...)
if suc {
t.Fail()
} else {
fmt.Println("inserted id: ", id)
}
}
func TestMySQLDb_Insertfail2(t *testing.T) {
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
//a = append(a, "test insert 1", "123 main st")
suc, id := dbi.Insert(q, a...)
if suc {
t.Fail()
} else {
fmt.Println("inserted id: ", id)
}
}
func TestMySQLDb_Insert(t *testing.T) {
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
suc, id := dbi.Insert(q, a...)
if !suc || id < 1 {
t.Fail()
} else {
iid1 = id
fmt.Println("inserted id: ", id)
}
}
func TestMySQLDb_Updatefail(t *testing.T) {
var q = "update test1 set name = ? , address = ? where id = ? "
var a []interface{}
a = append(a, "test insert 2", "123456 main st", iid1)
suc := dbi.Update(q, a...)
if suc {
t.Fail()
}
}
func TestMySQLDb_Updatefail2(t *testing.T) {
var q = "update test set name = ? , address = ? where id = ? "
var a []interface{}
//a = append(a, "test insert 2", "123456 main st", iid1)
suc := dbi.Update(q, a...)
if suc {
t.Fail()
}
}
func TestMySQLDb_Update(t *testing.T) {
var q = "update test set name = ? , address = ? where id = ? "
var a []interface{}
a = append(a, "test insert 2", "123456 main st", iid1)
suc := dbi.Update(q, a...)
if !suc {
t.Fail()
}
}
func TestMySQLDb_Get(t *testing.T) {
var rtn bool
var q = "select * from test where id = ? "
var a []interface{}
a = append(a, iid1)
rowPtr := dbi.Get(q, a...)
if rowPtr != nil {
foundRow := rowPtr.Row
fmt.Print("Get ")
fmt.Println(foundRow)
//fmt.Println("Get results: --------------------------")
int64Val, err2 := strconv.ParseInt(foundRow[0], 10, 0)
if err2 != nil {
fmt.Print(err2)
}
if iid1 != int64Val {
fmt.Print(iid1)
fmt.Print(" != ")
fmt.Println(int64Val)
t.Fail()
} else {
fmt.Print("found id")
fmt.Print(" = ")
fmt.Println(int64Val)
rtn = true
}
} else {
fmt.Println("database read failed")
t.Fail()
}
if !rtn {
t.Fail()
}
}
func TestMySQLDb_Getfail(t *testing.T) {
//var rtn bool
var q = "select * from test22 where id = ? "
var a []interface{}
a = append(a, iid1)
rowPtr := dbi.Get(q, a...)
if rowPtr != nil {
foundRow := rowPtr.Row
fmt.Print("Get in fail ")
fmt.Println(foundRow)
if len(foundRow) > 0 {
t.Fail()
}
}
}
func TestMySQLDb_GetList(t *testing.T) {
var suc bool
var q = "select * from test where address = ? "
var a []interface{}
a = append(a, "123456 main st")
rowsPtr := dbi.GetList(q, a...)
if rowsPtr != nil {
foundRows := rowsPtr.Rows
fmt.Println("rows found: ", foundRows)
//fmt.Println("GetList results: --------------------------")
fmt.Println("rows found count: ", len(foundRows))
if len(foundRows) > 0 {
suc = true
}
} else {
fmt.Println("database read failed")
t.Fail()
}
if !suc {
t.Fail()
}
}
func TestMySQLDb_GetListfail(t *testing.T) {
var q = "select * from test222 where address = ? "
var a []interface{}
a = append(a, "123456 main st")
rowsPtr := dbi.GetList(q, a...)
if rowsPtr != nil {
foundRow := rowsPtr.Rows
fmt.Print("Get in fail ")
fmt.Println(foundRow)
if len(foundRow) > 0 {
t.Fail()
}
}
}
func TestMySQLDb_Deletefail(t *testing.T) {
var q = "delete from test1 where id = ? "
var a []interface{}
a = append(a, iid1)
suc := dbi.Delete(q, a...)
if suc {
t.Fail()
}
}
func TestMySQLDb_Delete(t *testing.T) {
var q = "delete from test where id = ? "
var a []interface{}
a = append(a, iid1)
suc := dbi.Delete(q, a...)
if !suc {
t.Fail()
}
}
func TestMySQLDb_BeginTransaction(t *testing.T) {
trx := dbi.BeginTransaction()
fmt.Println("tx", trx)
if trx == nil {
t.Fail()
}
}
func TestMySQLDb_Close(t *testing.T) {
suc := dbi.Close()
if !suc {
t.Fail()
}
}