-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.go
108 lines (98 loc) · 2.66 KB
/
core_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
package sqldata
import (
"context"
"testing"
)
type mytestperson struct {
Id uint8 `sql:"id"`
Age uint8 `sql:"age"`
Name string `sql:"name"`
}
func TestConnect(t *testing.T) {
var conf *configDb
conf = &configDb{
DriverName: "mysql",
Addr: "xiaojianhe:123456@tcp(127.0.0.1:3306)/my?charset=utf8",
Retry: 2,
}
mytest, err := newConnDb(conf)
if err != nil {
t.Fatalf("fail to connect. [err:%v]", err)
}
ctx := context.Background()
rows, err := mytest.query(ctx, "SELECT * FROM infos limit 3")
if err != nil {
t.Fatalf("===== Connect get data. [err:%v]", err)
}
datas := mytest.rowsMap(rows)
t.Logf("=====Connect gat data : %v", datas)
//arows, err := mytest.query(ctx, "SELECT id,age,name FROM infos limit 3")
//if err != nil {
// t.Fatalf("===== Connect get data. [err:%v]", err)
//}
//myp := myperson{}
//adatas := mytest.rowsObject(arows,myp) //@todo here是问题
//t.Fatalf("=====Connect gat object : %v", adatas)
//for ppi:=0;ppi<len(adatas);ppi++ {
// pppcc:=datas[ppi]
// t.Log("======= indata:",pppcc["id"])
//}
}
func TestPrepare(t *testing.T) {
var conf *configDb
conf = &configDb{
DriverName: "mysql",
Addr: "xiaojianhe:123456@tcp(127.0.0.1:3306)/my?charset=utf8",
Retry: 2,
}
mytest, err := newConnDb(conf)
if err != nil {
t.Fatalf("fail to connect. [err:%v]", err)
}
//ctx := context.Background()
stmt, err := mytest.prepare("INSERT INTO `infos` (`name`, `age`) VALUES (?,?),(?,?)")
defer stmt.Close()
if err != nil {
t.Fatalf("prepare. [err:%v]", err)
}
result, err := mytest.execFromStmt(stmt, "肖2", 30, "肖2", 30)
if err != nil {
t.Fatalf("execFromStmt . [err:%v]", err)
}
id, _ := result.LastInsertId()
t.Logf("insert id:%v", id)
}
func TestCommit(t *testing.T) {
var conf *configDb
conf = &configDb{
DriverName: "mysql",
Addr: "xiaojianhe:123456@tcp(127.0.0.1:3306)/my?charset=utf8",
Retry: 2,
}
mytest, err := newConnDb(conf)
if err != nil {
t.Fatalf("fail to connect. [err:%v]", err)
}
btx, err := mytest.begin()
if err != nil {
t.Fatalf("====begin -> begin . [err:%v]", err)
}
//ctx := context.Background()
stmt, err := mytest.txPrepare(btx, "INSERT INTO `infos` (`name`, `age`) VALUES (?,?),(?,?)")
defer stmt.Close()
if err != nil {
t.Fatalf("====prepare from begin. [err:%v]", err)
}
result, err := mytest.execFromStmt(stmt, "肖cc2", 30, "肖cc2", 30)
if err != nil {
t.Fatalf("execFromStmt . [err:%v]", err)
}
id, _ := result.LastInsertId()
err = mytest.commit(btx)
if err != nil {
rerr := mytest.rollback(btx)
t.Logf("==== rollback :%v", rerr)
t.Fatalf("====commit -> commit . [err:%v]", err)
}
t.Logf("==== commit insert id:%v", id)
}