-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlTxMock_test.go
251 lines (222 loc) · 5.37 KB
/
mysqlTxMock_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
package mysql
import (
//"database/sql"
"fmt"
"testing"
di "github.com/Ulbora/dbinterface"
)
var tx1m di.Transaction
var mdbtm MyDBMock
var dbitm di.Database
var idtx1m int64
func TestMyDbTxMock_Connect(t *testing.T) {
mdbtm.Host = "localhost:3306"
mdbtm.User = "admin"
mdbtm.Password = "admin"
mdbtm.Database = "testdb"
mdbtm.MockConnectSuccess = true
//dbitm = &mdbtm
dbitm = mdbtm.GetNewDatabase()
suc := dbitm.Connect()
if !suc {
fmt.Println("Could not connect: ")
}
}
func TestMyDbTxMock_Insert(t *testing.T) {
tx1m = dbitm.BeginTransaction()
mdbtm.MockInsertSuccess1 = true
mdbtm.MockInsertID1 = 1
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
suc, id := tx1m.Insert(q, a...)
if !suc || id < 1 {
t.Fail()
} else {
idtx1m = id
fmt.Println("inserted id: ", id)
}
tx1m.Commit()
}
func TestMyDbTxMock_Insert2(t *testing.T) {
tx1m = dbitm.BeginTransaction()
//mdbtm.MockInsertSuccess1 = true
//mdbtm.MockInsertID1 = 1
mdbtm.MockInsertSuccess2 = true
mdbtm.MockInsertID2 = 2
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
//tx1m.Insert(q, a...)
suc, id := tx1m.Insert(q, a...)
fmt.Println("inserted 2 suc: ", suc)
fmt.Println("inserted 2 id: ", id)
if !suc || id < 1 {
t.Fail()
} else {
idtx1m = id
fmt.Println("inserted id: ", id)
}
tx1m.Commit()
}
func TestMyDbTxMock_Insert3(t *testing.T) {
tx1m = dbitm.BeginTransaction()
//mdbtm.MockInsertSuccess1 = true
//mdbtm.MockInsertID1 = 1
mdbtm.MockInsertSuccess3 = true
mdbtm.MockInsertID3 = 2
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
//tx1m.Insert(q, a...)
suc, id := tx1m.Insert(q, a...)
fmt.Println("inserted 2 suc: ", suc)
fmt.Println("inserted 2 id: ", id)
if !suc || id < 1 {
t.Fail()
} else {
idtx1m = id
fmt.Println("inserted id: ", id)
}
tx1m.Commit()
}
func TestMyDbTxMock_Insert4(t *testing.T) {
tx1m = dbitm.BeginTransaction()
//mdbtm.MockInsertSuccess1 = true
//mdbtm.MockInsertID1 = 1
mdbtm.MockInsertSuccess4 = true
mdbtm.MockInsertID4 = 4
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
a = append(a, "test insert 1", "123 main st")
//tx1m.Insert(q, a...)
suc, id := tx1m.Insert(q, a...)
fmt.Println("inserted 4 suc: ", suc)
fmt.Println("inserted 4 id: ", id)
if !suc || id < 1 {
t.Fail()
} else {
idtx1m = id
fmt.Println("inserted id: ", id)
}
tx1m.Commit()
}
func TestMyDbTxMock_Insertfail(t *testing.T) {
tx1m = dbitm.BeginTransaction()
mdbtm.MockInsertSuccess2 = false
var q = "insert into test (name, address) values(?, ?)"
var a []interface{}
//a = append(a, "test insert 1", "123 main st")
suc, id := tx1m.Insert(q, a...)
if suc {
t.Fail()
} else {
fmt.Println("inserted id: ", id)
}
tx1m.Commit()
}
func TestMyDbTxMock_Update(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockUpdateSuccess1 = true
var q = "update test set name = ? , address = ? where id = ? "
var a []interface{}
a = append(a, "test insert 2", "123456 main st", idtx1)
suc := tx1xx.Update(q, a...)
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Update2(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
//mdbtm.MockUpdateSuccess1 = true
mdbtm.MockUpdateSuccess3 = true
var q = "update test set name = ? , address = ? where id = ? "
var a []interface{}
a = append(a, "test insert 2", "123456 main st", idtx1)
tx1xx.Update(q, a...)
suc := tx1xx.Update(q, a...)
fmt.Println("update 2 suc: ", suc)
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Updatefail(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockUpdateSuccess2 = false
var q = "update test11 set name = ? , address = ? where id = ? "
var a []interface{}
a = append(a, "test insert 2", "123456 main st", idtx1)
suc := tx1xx.Update(q, a...)
if suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Delete(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockDeleteSuccess1 = true
var q = "delete from test where id = ? "
var a []interface{}
a = append(a, idtx1)
suc := tx1xx.Delete(q, a...)
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Delete2(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockDeleteSuccess2 = true
var q = "delete from test where id = ? "
var a []interface{}
a = append(a, idtx1)
suc := tx1xx.Delete(q, a...)
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Delete3(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockDeleteSuccess3 = true
var q = "delete from test where id = ? "
var a []interface{}
a = append(a, idtx1)
suc := tx1xx.Delete(q, a...)
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Rollback(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockRollbackSuccess = true
suc := tx1xx.Rollback()
if !suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Deletefail(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockDeleteSuccess2 = false
var q = "delete from test22 where id = ? "
var a []interface{}
a = append(a, idtx1)
suc := tx1xx.Delete(q, a...)
if suc {
t.Fail()
}
tx1xx.Commit()
}
func TestMyDbTxMock_Commit(t *testing.T) {
tx1xx := dbitm.BeginTransaction()
mdbtm.MockCommitSuccess = true
suc := tx1xx.Commit()
if !suc {
t.Fail()
}
tx1xx.Commit()
}