-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_test.go
70 lines (63 loc) · 1.64 KB
/
meta_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
package dbutil
import (
"reflect"
"testing"
"time"
)
type Times struct {
CreateTime time.Time
ModifyTime time.Time
}
type User struct {
Id int64
Name string
Age int64
D string `db:"detail"`
Times
}
func comparePtr(t *testing.T, name string, right, real interface{}) {
if !reflect.DeepEqual(right, real) {
t.Errorf("ptr mismatch: %s", name)
}
}
func TestMetaFields(t *testing.T) {
var u User
meta := GetTableMeta(reflect.TypeOf(u))
if vs, err := meta.GetScanFields([]string{"id", "name", "age", "detail", "create_time", "modify_time"}, &u); err == nil {
if len(vs) != 6 {
t.Errorf("invalid fields: %v", vs)
} else {
comparePtr(t, "id", &u.Id, vs[0])
comparePtr(t, "name", &u.Name, vs[1])
comparePtr(t, "age", &u.Age, vs[2])
comparePtr(t, "detail", &u.D, vs[3])
comparePtr(t, "create_time", &u.CreateTime, vs[4])
comparePtr(t, "modify_time", &u.ModifyTime, vs[5])
}
} else {
t.Errorf("get fields fail: %v", err)
}
}
func TestMetaPrepareFields(t *testing.T) {
var u User
meta := GetTableMeta(reflect.TypeOf(u))
p, err := meta.PrepareScan([]string{"id", "name", "age", "detail", "create_time", "modify_time"})
if err != nil {
t.Errorf("prepare fail: %v", err)
return
}
if vs, err := p.GetScanFields(&u); err == nil {
if len(vs) != 6 {
t.Errorf("invalid fields: %v", vs)
} else {
comparePtr(t, "id", &u.Id, vs[0])
comparePtr(t, "name", &u.Name, vs[1])
comparePtr(t, "age", &u.Age, vs[2])
comparePtr(t, "detail", &u.D, vs[3])
comparePtr(t, "create_time", &u.CreateTime, vs[4])
comparePtr(t, "modify_time", &u.ModifyTime, vs[5])
}
} else {
t.Errorf("get fields fail: %v", err)
}
}