-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx_test.go
323 lines (284 loc) · 7.15 KB
/
x_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package mx
import (
"database/sql"
"encoding/json"
"os"
"reflect"
"strconv"
"testing"
)
// go test -coverprofile=c.out
// go tool cover -html=c.out -o coverage.html
var db, _ = NewDataBase(os.Getenv("MX_DSN"))
var UserTable = db.Table("user")
func TestQuery1(t *testing.T) {
sr := UserTable.Query("SELECT * FROM user WHERE id = ?", 2)
for sr.rows.Next() {
vals := make([]*sql.RawBytes, 7)
for i := 0; i < 7; i++ {
vals[i] = &sql.RawBytes{}
}
t.Log(sr.Scan(&vals))
t.Log(vals)
for _, val := range vals {
t.Log(val)
}
}
}
func TestQuery2(t *testing.T) {
sr := UserTable.Query("SELECT * FROM user WHERE id IN (?,?,?)", 1, 2, 3)
for sr.rows.Next() {
cols, err := sr.rows.Columns()
if err != nil {
t.Fatal(err)
}
t.Log(cols)
cts, err := sr.rows.ColumnTypes()
if err != nil {
t.Fatal(err)
}
for _, ct := range cts {
length, _ := ct.Length()
t.Log(ct.Name(), ct.DatabaseTypeName(), ct.ScanType().Name(), length)
}
vals := make([]*sql.RawBytes, 7)
for i := 0; i < len(cols); i++ {
vals[i] = &sql.RawBytes{}
}
t.Log(sr.Scan(&vals))
t.Log(vals)
for _, val := range vals {
t.Log(val)
}
}
}
// IN 使用的时候有两种情况
// 第一种 IN长度为0的时候应该查询所有数据
// 第二种 IN长度为0的时候应该查询不到数据
// Deprecated: 弃用此方法
// fmt.Println(ms.rvp.Kind(), ms.rvp.CanAddr(), ms.rvp.Elem().CanAddr())
func TestNewModelStruct(t *testing.T) {
v := User{}
ms, err := NewModelStruct(v)
t.Log(ms, err)
tests := []any{
User{},
}
for _, test := range tests {
_ = test
}
}
type User struct {
DefaultTime `json:"default_time"`
ID uint32 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
UID int `json:"uid"`
IgnoreMe int `mx:"-" json:"ignore_me"`
AfterFindCount int `mx:"-" json:"after_find_count"`
Weapon Weapon `json:"weapon"`
Gems []Gem `json:"gem"`
}
type Weapon struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
Lv string `json:"lv"`
DefaultTime
}
type Gem struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Name string `json:"name"`
Lv string `json:"lv"`
AfterFindCount int `mx:"-" json:"after_find_count"`
History []History `json:"history"`
DefaultTime
}
type History struct {
ID int `json:"id"`
Remark string `json:"remark"`
}
func (g *Gem) AfterFind() error {
g.AfterFindCount++
return nil
}
func (u *User) AfterFind() error {
u.AfterFindCount++
return nil
}
type DefaultTime struct {
Ctime string `json:"ctime"`
Utime string `json:"utime"`
}
// go test -benchmem -bench "^(BenchmarkReflectFunc)|(BenchmarkAssertionFunc)$"
// goos: windows
// goarch: amd64
// pkg: github.com/shesuyo/mx
// Benchmark_ReflectFunc-16 3000000 453 ns/op 208 B/op 7 allocs/op
// Benchmark_AssertionFunc-16 200000000 6.98 ns/op 0 B/op 0 allocs/op
// PASS
// ok github.com/shesuyo/mx 3.951s
func BenchmarkReflectFunc(b *testing.B) {
u := reflect.ValueOf(&User{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
if af := u.MethodByName(AfterFind); af.IsValid() {
af.Call(nil)
}
}
}
func BenchmarkAssertionFunc(b *testing.B) {
var u any = &User{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if af, ok := u.(AfterFinder); ok {
af.AfterFind()
}
}
}
// go test -benchmem -bench "^(BenchmarkMapGet)|(BenchmarkSliceGet.{1,2})$"
// goos: windows
// goarch: amd64
// pkg: github.com/shesuyo/mx
// BenchmarkMapGet-16 100000000 11.1 ns/op 0 B/op 0 allocs/op
// BenchmarkSliceGet0-16 2000000000 0.84 ns/op 0 B/op 0 allocs/op
// BenchmarkSliceGet10-16 300000000 5.05 ns/op 0 B/op 0 allocs/op
// BenchmarkSliceGet19-16 100000000 11.8 ns/op 0 B/op 0 allocs/op
// PASS
// ok github.com/shesuyo/mx 6.167s
func BenchmarkMapGet(b *testing.B) {
m := make(map[string]Columns)
for i := 0; i < 20; i++ {
m["field"+strconv.Itoa(i)] = Columns{}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m["field19"]
}
}
type KeyWithColumns struct {
key string
cols Columns
}
func BenchmarkSliceGet0(b *testing.B) {
m := []KeyWithColumns{}
for i := 0; i < 20; i++ {
m = append(m, KeyWithColumns{key: "field" + strconv.Itoa(i), cols: Columns{}})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 20; j++ {
if m[j].key == "field0" {
break
}
}
}
}
func BenchmarkSliceGet10(b *testing.B) {
m := []KeyWithColumns{}
for i := 0; i < 20; i++ {
m = append(m, KeyWithColumns{key: "field" + strconv.Itoa(i), cols: Columns{}})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 20; j++ {
if m[j].key == "field10" {
break
}
}
}
}
func BenchmarkSliceGet19(b *testing.B) {
m := []KeyWithColumns{}
for i := 0; i < 20; i++ {
m = append(m, KeyWithColumns{key: "field" + strconv.Itoa(i), cols: Columns{}})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 20; j++ {
if m[j].key == "field19" {
break
}
}
}
}
func BenchmarkClone(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = UserTable.Clone()
}
}
func TestQuery(t *testing.T) {
sr := UserTable.Query("SELECT * FROM user WHERE id = ?", 2)
for sr.rows.Next() {
vals := make([]*sql.RawBytes, 7)
for i := 0; i < 7; i++ {
vals[i] = &sql.RawBytes{}
}
t.Log(sr.Scan(&vals))
t.Log(vals)
}
}
// 1s 1_000ms 1_000_000us 1_000_000_000ns
// BenchmarkQuery-16 10000 109262 ns/op 1624 B/op 41 allocs/op
func BenchmarkQuery(b *testing.B) {
for i := 0; i < b.N; i++ {
sr := UserTable.Query("SELECT * FROM user WHERE id = ?", 2)
for sr.rows.Next() {
vals := make([]*sql.RawBytes, 7)
for i := 0; i < 7; i++ {
vals[i] = &sql.RawBytes{}
}
sr.Scan(&vals)
}
}
}
func TestTableToStruct(t *testing.T) {
u := User{}
UserTable.Where("id = ?", 2).ToStruct(&u)
if u.AfterFindCount != 1 {
t.Fatal("AfterFind Err")
}
}
// BenchmarkReflectAStruct-16 3122 383770 ns/op 15140 B/op 389 allocs/op 加载一个结构体和一个slice
// BenchmarkReflectAStruct-16 9615 117831 ns/op 3676 B/op 98 allocs/op
func BenchmarkReflectAStruct(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = UserTable.Where("id = ?", 2).ToStruct(&User{})
}
}
// has one
// has many
// many to many
func isSlice(args any) bool {
return reflect.TypeOf(args).Kind() == reflect.Slice
}
func TestIsSlice(t *testing.T) {
args := []any{}
if !isSlice(args) {
t.Fatal("fail with is slice")
}
}
func BenchmarkIsSlice(b *testing.B) {
args := []any{}
for i := 0; i < b.N; i++ {
isSlice(args)
}
}
func JSONStringify(v any) string {
bs, err := json.Marshal(v)
if err != nil {
return ""
}
return string(bs)
}
func TestToStructSliceNested(t *testing.T) {
us := []User{}
UserTable.DataBase.debug = true
UserTable.Where("id IN(1,2)").ToStruct(&us)
t.Log("user len:", len(us))
for _, u := range us {
t.Log(u.ID, u.Name, u.Weapon.Name)
t.Log("gems len:", len(u.Gems))
}
}