forked from andrew-waters/gomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_options_test.go
151 lines (144 loc) · 2.75 KB
/
request_options_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
package gomo
import (
"net/url"
"testing"
)
func TestBody(t *testing.T) {
var w wrapper
Body("foobar")(&w)
body, ok := w.body.(jsonBody)
if !ok {
t.Fatal("failed to set json body")
}
if s, ok := body.Data.(string); !ok || s != "foobar" {
t.Fatal("failed to set data")
}
}
func TestResouceSetters(t *testing.T) {
for _, test := range []struct {
section string
requestResource RequestResource
}{
{
"data",
Data("foobar"),
},
{
"links",
Links("foobar"),
},
{
"included",
Included("foobar"),
},
{
"meta",
Meta("foobar"),
},
} {
t.Run(test.section, func(t *testing.T) {
var w wrapper
test.requestResource(&w)
if len(w.resources) == 0 {
t.Fatal("failed to add resource")
}
rt := w.resources[0]
if rt.section != test.section {
t.Errorf("wrong section: %s", rt.section)
}
if s, ok := rt.target.(string); !ok || s != "foobar" {
t.Fatal("failed to set target")
}
})
}
}
func TestPaginate(t *testing.T) {
var w wrapper
w.query = make(url.Values)
Paginate(200, 100)(&w)
if w.query.Get("page[offset]") != "200" {
t.Error("failed to set offset")
}
if w.query.Get("page[limit]") != "100" {
t.Error("failed to set limit")
}
}
func TestFilter(t *testing.T) {
for _, test := range []struct {
name string
filters []RequestResource
expectedFilter string
}{
{
"single",
[]RequestResource{
Filter("eq(status,live)"),
},
"eq(status,live)",
},
{
"multiple",
[]RequestResource{
Filter("eq(status,live)"),
Filter("like(name,Deck*)"),
},
"like(name,Deck*):eq(status,live)",
},
} {
t.Run(test.name, func(t *testing.T) {
var w wrapper
w.query = make(url.Values)
for _, filter := range test.filters {
filter(&w)
}
filter := w.query.Get("filter")
if filter != test.expectedFilter {
t.Errorf("expected: %s, got %s", test.expectedFilter, filter)
}
})
}
}
func TestInclude(t *testing.T) {
for _, test := range []struct {
name string
includes []RequestResource
expectedInclude string
}{
{
"single",
[]RequestResource{
Include("products"),
},
"products",
},
{
"multiple",
[]RequestResource{
Include("products"),
Include("categories"),
},
"categories,products",
},
} {
t.Run(test.name, func(t *testing.T) {
var w wrapper
w.query = make(url.Values)
for _, include := range test.includes {
include(&w)
}
include := w.query.Get("include")
if include != test.expectedInclude {
t.Errorf("expected: %s, got %s", test.expectedInclude, include)
}
})
}
}
func TestSort(t *testing.T) {
var w wrapper
w.query = make(url.Values)
Sort("foo")(&w)
sort := w.query.Get("sort")
if sort != "foo" {
t.Error("failed to sort")
}
}