forked from de3/golib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_test.go
132 lines (120 loc) · 3.3 KB
/
bind_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
package golib
import (
"net/http"
"net/url"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBindQueryParam(t *testing.T) {
type exampleParam struct {
OrderID string `json:"orderId"`
SoNumber string `json:"soNumber"`
}
tests := []struct {
name, queryParam string
target, wantResult interface{}
wantError, wantEqual bool
}{
{
name: "Testcase #1: Positive",
queryParam: "orderId=0001&soNumber=SO001",
target: new(exampleParam),
wantResult: exampleParam{
OrderID: "0001", SoNumber: "SO001",
},
wantEqual: true,
},
{
name: "Testcase #2: Negative, query param not match with json tag in struct model target",
queryParam: "orderID=0001&soNumber=SO001",
target: new(exampleParam),
wantResult: exampleParam{
OrderID: "0001", SoNumber: "SO001",
},
wantEqual: false,
},
{
name: "Testcase #3: Negative, target is not pointer",
queryParam: "orderId=0001&soNumber=SO001",
target: exampleParam{},
wantResult: exampleParam{
OrderID: "0001", SoNumber: "SO001",
},
wantError: true, wantEqual: false,
},
{
name: "Testcase #4: Positive",
queryParam: "orderId=0001&soNumber=2019:08:28T00:00:00+07:00",
target: new(exampleParam),
wantResult: exampleParam{
OrderID: "0001", SoNumber: "2019:08:28T00:00:00+07:00",
},
wantEqual: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest("GET", "/api/order?"+tt.queryParam, nil)
assert.NoError(t, err)
err = BindQueryParam(req.URL, tt.target)
if tt.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
targetValue := reflect.ValueOf(tt.target)
if targetValue.Kind() == reflect.Ptr {
targetValue = targetValue.Elem()
}
if tt.wantEqual {
assert.Equal(t, tt.wantResult, targetValue.Interface())
} else {
assert.NotEqual(t, tt.wantResult, targetValue.Interface())
}
})
}
}
func TestParseFromQueryParam(t *testing.T) {
type Embed struct {
Page int `json:"page"`
Offset int `json:"-"`
Sort string `json:"sort,omitempty" default:"desc" lower:"true"`
}
type params struct {
Embed
IsActive bool `json:"isActive"`
Ptr *string `json:"ptr"`
}
t.Run("Testcase #1: Positive", func(t *testing.T) {
urlVal, err := url.ParseQuery("page=1&ptr=val&isActive=true")
assert.NoError(t, err)
var p params
err = ParseFromQueryParam(urlVal, &p)
assert.NoError(t, err)
assert.Equal(t, p.Page, 1)
assert.Equal(t, *p.Ptr, "val")
assert.Equal(t, p.IsActive, true)
})
t.Run("Testcase #2: Negative, invalid data type (string to int in struct)", func(t *testing.T) {
urlVal, err := url.ParseQuery("page=undefined")
assert.NoError(t, err)
var p params
err = ParseFromQueryParam(urlVal, &p)
assert.Error(t, err)
})
t.Run("Testcase #3: Negative, invalid data type (not boolean)", func(t *testing.T) {
urlVal, err := url.ParseQuery("isActive=terue")
assert.NoError(t, err)
var p params
err = ParseFromQueryParam(urlVal, &p)
assert.Error(t, err)
})
t.Run("Testcase #4: Negative, invalid target type (not pointer)", func(t *testing.T) {
urlVal, err := url.ParseQuery("isActive=true")
assert.NoError(t, err)
var p params
err = ParseFromQueryParam(urlVal, p)
assert.Error(t, err)
})
}