-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_test.go
64 lines (56 loc) · 1.24 KB
/
raw_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
package session
import (
"database/sql"
"os"
"testing"
log "github.com/sirupsen/logrus"
)
var db *sql.DB
func TestMain(m *testing.M) {
db, _ = sql.Open("mysql", "root:12345678@tcp(127.0.0.1:3306)/po?charset=utf8mb4")
code := m.Run()
_ = db.Close()
os.Exit(code)
}
func New() *Session {
return NewSession(db)
}
func TestSession_QueryRow(t *testing.T) {
s := New()
var userName string
var age int
s = s.Raw("select user_name,age from user where user_name = ?", "迈莫")
res := s.QueryRow()
if err := res.Scan(&userName, &age); err != nil {
t.Fatal("failed to query db", err)
}
log.Info("userName--", userName)
log.Info("age--", age)
}
func TestSession_Exec(t *testing.T) {
s := New()
key := "迈莫"
s = s.Raw("insert into user(user_name, age) values(?, ?)", key, 21)
_, err := s.Exec()
if err != nil {
t.Fatal("failed to insert db", err)
}
}
func TestSession_Query(t *testing.T) {
s := New()
var userName string
var age int
s = s.Raw("select user_name, age from user")
rows, err := s.Query()
if err != nil {
t.Fatal("fialed to query db", err)
}
for rows.Next() {
err = rows.Scan(&userName, &age)
if err != nil {
t.Fatal("fialed to query db", err)
}
log.Info("userName--", userName)
log.Info("age--", age)
}
}