forked from ilibs/gosql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
83 lines (64 loc) · 1.49 KB
/
util_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
package gosql
import (
"reflect"
"testing"
)
func TestUtil_inSlice(t *testing.T) {
s := []string{"a", "b", "c"}
if !inSlice("a", s) {
t.Error("in slice find error")
}
if inSlice("b", s) {
t.Error("in slice exist error")
}
}
func TestUtil_zeroValueFilter(t *testing.T) {
user := &Users{
Id: 1,
Name: "test",
}
rv := reflect.Indirect(reflect.ValueOf(user))
fields := mapper.FieldMap(rv)
m := zeroValueFilter(fields, nil)
if _, ok := m["email"]; ok {
t.Error("email zero value not filter")
}
if _, ok := m["creatd_at"]; ok {
t.Error("creatd_at zero value not filter")
}
if _, ok := m["updated_at"]; ok {
t.Error("updated_at zero value not filter")
}
m2 := zeroValueFilter(fields, []string{"email"})
if _, ok := m2["email"]; !ok {
t.Error("email shouldn't be filtered")
}
}
func TestUtil_structAutoTime(t *testing.T) {
user := &Users{
Id: 1,
Name: "test",
}
rv := reflect.Indirect(reflect.ValueOf(user))
fields := mapper.FieldMap(rv)
structAutoTime(fields, AUTO_CREATE_TIME_FIELDS)
if user.CreatedAt.IsZero() {
t.Error("auto time fail")
}
}
func TestUtil_sortedParamKeys(t *testing.T) {
m := map[string]interface{}{
"id": 1,
"name": "test",
"email": "test@test.com",
"created_at": "2018-07-11 11:58:21",
"updated_at": "2018-07-11 11:58:21",
}
keySort := []string{"created_at", "email", "id", "name", "updated_at"}
s := sortedParamKeys(m)
for i, k := range s {
if k != keySort[i] {
t.Error("sort error", k)
}
}
}