-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdboperate_test.go
148 lines (115 loc) · 2.61 KB
/
dboperate_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
package dboperate
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/nickforget/dboperate/protocol"
"testing"
)
/*
CREATE TABLE student (
name varchar(50) DEFAULT NULL,
age int(11) DEFAULT NULL,
no int(20) NOT NULL DEFAULT '0',
PRIMARY KEY (no),
KEY index_student_name (name) USING BTREE
);
*/
var db *DBOperate
func init() {
db = NewDBOperate("mysql", "root:muchinfo@/mysql")
db.ConnDB()
}
func TestDBAccessInsert(t *testing.T) {
stu := &test.Student{
Name: proto.String("zouqiang999"),
Age: proto.Int32(28),
NO: proto.Int32(989),
}
err := db.Insert("student", stu)
if err != nil {
t.Errorf("Insert err %v", err)
}
}
func TestDBAccessQuery(t *testing.T) {
data, err := db.Query("student", []string{}, "", &test.Student{})
if err == nil {
for _, v := range data {
tmp := v.(*test.Student)
t.Log(tmp.GetAge(), tmp.GetName(), tmp.GetNO())
}
} else {
t.Error("Query err ", err)
}
}
func TestDBAccessUpdate(t *testing.T) {
stu := &test.Student{
Name: proto.String("chenyirui"),
Age: proto.Int32(30),
}
err := db.Update("student", stu, &test.Student{
Age: proto.Int32(28),
})
if err != nil {
t.Error("Update err ", err)
}
}
func TestDBAccessDelete(t *testing.T) {
err := db.Delete("student", &test.Student{})
if err != nil {
t.Error("Delete err ", err)
}
}
func TestDBAccessCommit(t *testing.T) {
stu := &test.Student{
Name: proto.String("xiaoyang"),
Age: proto.Int32(20),
NO: proto.Int32(0),
}
err := db.SetNotAutoCommit()
if err != nil {
fmt.Println("SetNotAutoCommit: ", err)
}
for i := int32(0); i < 10; i++ {
stu.NO = proto.Int32(i)
err = db.Insert("student", stu)
if err != nil {
fmt.Println("Insert: ", err)
}
}
db.Commit()
}
func TestDBAccessRollback(t *testing.T) {
stu := &test.Student{
Name: proto.String("xiaoyang"),
Age: proto.Int32(20),
NO: proto.Int32(1),
}
err := db.SetNotAutoCommit()
if err != nil {
fmt.Println("SetNotAutoCommit: ", err)
}
for i := int32(10); i < 20; i++ {
stu.NO = proto.Int32(i)
err = db.Insert("student", stu)
if err != nil {
fmt.Println("Insert: ", err)
}
}
// 查询
revData, err := db.Query("student", []string{}, "", &test.Student{})
if err == nil {
for _, v := range revData {
tmp := *v.(*test.Student)
fmt.Println(tmp.GetName(), tmp.GetName(), tmp.GetAge(), tmp.GetNO())
}
}
db.Rollback()
// 回滚之后再次查询结果
revData, err = db.Query("student", []string{}, "", &test.Student{})
if err == nil {
for _, v := range revData {
tmp := *v.(*test.Student)
fmt.Println(tmp.GetName(), tmp.GetName(), tmp.GetAge(), tmp.GetNO())
}
}
}