-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
98 lines (89 loc) · 2.18 KB
/
client_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
package session
import (
"context"
"testing"
log "github.com/sirupsen/logrus"
)
func Newclient() (client *Client, err error) {
setting := Settings{
DriverName: "mysql",
User: "root",
Password: "12345678",
Database: "po",
Host: "127.0.0.1:3306",
Options: map[string]string{"charset": "utf8mb4"},
}
return NewClient(setting)
}
func TestSession_Insert(t *testing.T) {
user := &Users{
Name: "迈莫coding",
Age: 1,
}
statement := NewStatement()
statement = statement.SetTableName("memo").
InsertStruct(user)
client, _ := Newclient()
client.Insert(context.Background(), statement)
}
func TestSession_FindOne(t *testing.T) {
statement := NewStatement()
statement = statement.SetTableName("user").
AndEqual("user_name", "迈莫").
Select("user_name,age")
client, err := Newclient()
if err != nil {
log.Error(err)
return
}
user := &User{}
_ = client.FindOne(context.Background(), statement, user)
log.Info(user)
}
func TestSession_FindAll(t *testing.T) {
statement := NewStatement()
statement = statement.SetTableName("user").
Select("user_name,age")
client, _ := Newclient()
var user []User
_ = client.FindAll(context.Background(), statement, &user)
log.Info(user)
}
func TestSession_Delete(t *testing.T) {
statement := NewStatement()
statement = statement.SetTableName("memo").
AndEqual("name", "迈莫coding")
client, _ := Newclient()
client.Delete(context.Background(), statement)
}
func TestSession_Update(t *testing.T) {
user := &Users{
Name: "迈莫",
Age: 1,
}
statement := NewStatement()
statement = statement.SetTableName("user").
UpdateStruct(user).
AndEqual("user_name", "迈莫")
client, _ := Newclient()
client.Update(context.Background(), statement)
}
func TestClient_Transaction(t *testing.T) {
user := &Users{
Name: "迈莫",
Age: 1,
}
client, _ := Newclient()
statement := NewStatement()
statement = statement.SetTableName("user").
AndEqual("age", 21).Select("user_name,age")
res, err := client.Transaction(func(ctx context.Context, client2 *Client) (interface{}, error) {
err := client2.FindOne(ctx, statement, user)
return user, err
})
if err != nil {
log.Error(err)
return
}
log.Info(res)
}