-
Notifications
You must be signed in to change notification settings - Fork 1
/
select_test.go
111 lines (91 loc) · 2.2 KB
/
select_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
package gatsby
import "testing"
import "github.com/c9s/gatsby/sqlutils"
func chkResult(t *testing.T, res *Result) {
if res.Error != nil {
t.Fatal(res.Error, res.Sql)
}
}
func TestSelectQueryWithAlias(t *testing.T) {
var db = openDB()
db.Query("delete from staffs;")
staff := Staff{Name: "John", Gender: "m", Phone: "0975277696"}
chkResult(t, Create(db, &staff, DriverPg))
defer Delete(db, &staff)
var selectClause = sqlutils.BuildSelectClauseWithAlias(&staff, "s")
t.Log(selectClause)
rows, err := db.Query(selectClause)
if err != nil {
t.Fatal(err)
}
data, err := CreateStructSliceFromRows(&staff, rows)
if err != nil {
t.Fatal(err)
}
var staffs = data.([]Staff)
for _, s := range staffs {
t.Log(s)
}
db.Query("delete from staffs;")
}
func BenchmarkSelectQuery(b *testing.B) {
var db = openDB()
staff := Staff{Name: "John", Gender: "m", Phone: "0975277696"}
Create(db, &staff, DriverPg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
rows, err := QuerySelect(db, &staff)
if err != nil {
b.Fatal(err)
}
if _, err := CreateStructSliceFromRows(&staff, rows); err != nil {
b.Fatal(err)
}
}
Delete(db, &staff)
}
func TestSelectQuery(t *testing.T) {
var db = openDB()
staff := Staff{Name: "John", Gender: "m", Phone: "0975277696"}
chkResult(t, Create(db, &staff, DriverPg))
rows, err := QuerySelect(db, &staff)
if err != nil {
t.Fatal(err)
}
var count = 0
for rows.Next() {
count++
}
if count == 0 {
t.Fatal("select 0 record")
}
Delete(db, &staff)
}
func TestSelect(t *testing.T) {
var db = openDB()
staff := Staff{Name: "John", Gender: "m", Phone: "0975277696"}
chkResult(t, Create(db, &staff, DriverPg))
staff2 := Staff{Name: "Mary", Gender: "m", Phone: "0975277696"}
chkResult(t, Create(db, &staff2, DriverPg))
staff3 := Staff{Name: "Jack", Gender: "m", Phone: "0975277696"}
chkResult(t, Create(db, &staff3, DriverPg))
items, result := Select(db, &staff)
chkResult(t, result)
staffs := items.([]Staff)
if len(staffs) == 0 {
t.Fatal("found 0 record")
}
for _, s := range staffs {
t.Log(s.Id)
if s.Name == "" {
t.Fatal("Empty name")
}
if s.Id > 0 {
var res = Delete(db, &s)
if res.Error != nil {
t.Fatal(res.Error)
}
}
}
_ = staffs
}