forked from smartcontractkit/chainlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
163 lines (142 loc) · 4.58 KB
/
api_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
package web
import (
"net/url"
"testing"
"github.com/manyminds/api2go/jsonapi"
"github.com/stretchr/testify/assert"
)
func TestApi_ParsePaginatedRequest(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sizeParam string
pageParam string
err bool
size int
page int
offset int
}{
{"blank values", "", "", false, 25, 1, 0},
{"valid sizeParam", "10", "", false, 10, 1, 0},
{"valid pageParam", "", "3", false, 25, 3, 50},
{"invalid sizeParam", "xhje", "", true, 0, 0, 0},
{"invalid pageParam", "", "ewjh", true, 0, 0, 0},
{"small sizeParam", "0", "", true, 0, 0, 0},
{"negative pageParam", "", "-1", true, 0, 0, 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
size, page, offset, err := ParsePaginatedRequest(test.sizeParam, test.pageParam)
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.size, size)
assert.Equal(t, test.page, page)
assert.Equal(t, test.offset, offset)
})
}
}
type TestResource struct {
Title string
}
func (r TestResource) GetID() string {
return "1"
}
func (r *TestResource) SetID(value string) error {
return nil
}
func TestApi_NewPaginatedResponse(t *testing.T) {
t.Parallel()
tests := []struct {
name string
path string
size int
page int
count int
resource interface{}
err bool
output string
}{
{
"a single resource",
"/v2/index", 1, 0, 0, TestResource{Title: "Item"},
false, `{"data":{"type":"testResources","id":"1","attributes":{"Title":"Item"}},"meta":{"count":0}}`,
},
{
"a resource collection",
"/v2/index", 1, 0, 0, []TestResource{{Title: "Item 1"}, {Title: "Item 2"}},
false, `{"data":[{"type":"testResources","id":"1","attributes":{"Title":"Item 1"}},{"type":"testResources","id":"1","attributes":{"Title":"Item 2"}}],"meta":{"count":0}}`,
},
{
"first page of collection results",
"/v2/index", 5, 1, 7, []TestResource{{Title: "Item 1"}},
false, `{"links":{"next":"/v2/index?page=2\u0026size=5"},"data":[{"type":"testResources","id":"1","attributes":{"Title":"Item 1"}}],"meta":{"count":7}}`,
},
{
"middle page of collection results",
"/v2/index", 5, 2, 13, []TestResource{{Title: "Item 2"}},
false, `{"links":{"next":"/v2/index?page=3\u0026size=5","prev":"/v2/index?page=1\u0026size=5"},"data":[{"type":"testResources","id":"1","attributes":{"Title":"Item 2"}}],"meta":{"count":13}}`,
},
{
"end page of collection results",
"/v2/index", 5, 3, 13, []TestResource{{Title: "Item 3"}},
false, `{"links":{"prev":"/v2/index?page=2\u0026size=5"},"data":[{"type":"testResources","id":"1","attributes":{"Title":"Item 3"}}],"meta":{"count":13}}`,
},
{
"path with existing query",
"/v2/index?authToken=3123", 1, 0, 2, []TestResource{{Title: "Item 1"}},
false, `{"links":{"next":"/v2/index?authToken=3123\u0026page=1\u0026size=1"},"data":[{"type":"testResources","id":"1","attributes":{"Title":"Item 1"}}],"meta":{"count":2}}`,
},
{
"json marshalling failure",
"/v2/index", 1, 0, 0, "",
true, ``,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
url, err := url.Parse(test.path)
assert.NoError(t, err)
buffer, err := NewPaginatedResponse(*url, test.size, test.page, test.count, test.resource)
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.output, string(buffer))
})
}
}
func TestPagination_ParsePaginatedResponse(t *testing.T) {
t.Parallel()
var docs []TestResource
var links jsonapi.Links
err := ParsePaginatedResponse([]byte(`{"data":[{"type":"testResources","id":"1","attributes":{"Title":"album 1"}}]}`), &docs, &links)
assert.NoError(t, err)
assert.Equal(t, "album 1", docs[0].Title)
// Typo in "type"
err = ParsePaginatedResponse([]byte(`{"data":[{"type":"testNotResources","id":"1","attributes":{}}]}`), &docs, &links)
assert.Error(t, err)
// Typo in "links"
err = ParsePaginatedResponse([]byte(`{"links":[],"data":[{"type":"testResources","id":"1","attributes":{}}]}`), &docs, &links)
assert.Error(t, err)
}
type DummyResource struct {
ID string
}
// GetID returns the ID of this structure for jsonapi serialization.
func (d DummyResource) GetID() string {
return d.ID
}
func TestNewJSONAPIResponse(t *testing.T) {
t.Parallel()
buffer, err := NewJSONAPIResponse(12981)
assert.Error(t, err)
assert.Len(t, buffer, 0)
r := DummyResource{ID: "782"}
buffer, err = NewJSONAPIResponse(&r)
assert.NoError(t, err)
assert.Equal(t, `{"data":{"type":"dummyResources","id":"782","attributes":{"ID":"782"}}}`, string(buffer))
}