-
Notifications
You must be signed in to change notification settings - Fork 1
/
page_test.go
186 lines (178 loc) · 4.84 KB
/
page_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package pagination_test
import (
"errors"
"net/url"
"reflect"
"testing"
"github.com/yohgo/pagination"
)
// User contains the data required for a unique user.
type User struct {
ID uint64
Name string
Surname string
}
// newPageDataProvider provides data for the TestNewPage function.
var newPageDataProvider = []struct {
name string
url string
results interface{}
want *pagination.Page
err error
}{
{
name: "Page creation fails - invalid url query",
url: "api.demo.com/v1/users?page=invalid_page&limit=invalid_limit&order_by=&order=invalid_order",
want: nil,
err: errors.New("Page is invalid"),
},
{
name: "Page creation fails - invalid results",
url: "api.demo.com/v1/users?page=1&limit=3&order_by=name&order=asc",
results: "invalid results",
want: nil,
err: errors.New("The provided collection is not a slice"),
},
{
name: "Successful page creation - no paging, no ordering, empty results",
url: "api.demo.com/v1/users",
results: []*User{},
want: &pagination.Page{
Count: 0,
Links: &pagination.Links{
Self: "api.demo.com/v1/users",
Previous: "",
Next: "",
},
Results: []*User{},
},
err: nil,
},
{
name: "Successful page creation - no paging, no ordering",
url: "api.demo.com/v1/users",
results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
},
want: &pagination.Page{
Count: 1,
Links: &pagination.Links{
Self: "api.demo.com/v1/users",
Previous: "",
Next: "",
},
Results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
},
},
err: nil,
},
{
name: "Successful page creation - ordering, no paging",
url: "api.demo.com/v1/users?order_by=name&order=asc",
results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
want: &pagination.Page{
Count: 3,
Links: &pagination.Links{
Self: "api.demo.com/v1/users?order_by=name&order=asc",
Previous: "",
Next: "",
},
Results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
},
err: nil,
},
{
name: "Successful page creation - first page",
url: "api.demo.com/v1/users?page=1&limit=3&order_by=name&order=asc",
results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
want: &pagination.Page{
Count: 3,
Links: &pagination.Links{
Next: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=2",
Previous: "",
Self: "api.demo.com/v1/users?page=1&limit=3&order_by=name&order=asc",
},
Results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
},
err: nil,
},
{
name: "Successful page creation - second page",
url: "api.demo.com/v1/users?page=2&limit=3&order_by=name&order=asc",
results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
want: &pagination.Page{
Count: 3,
Links: &pagination.Links{
Next: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=3",
Previous: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=1",
Self: "api.demo.com/v1/users?page=2&limit=3&order_by=name&order=asc",
},
Results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
{ID: 3, Name: "Paul", Surname: "Johnson"},
},
},
err: nil,
},
{
name: "Successful page creation - third page",
url: "api.demo.com/v1/users?page=3&limit=3&order_by=surname&order=desc",
results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
},
want: &pagination.Page{
Count: 2,
Links: &pagination.Links{
Next: "",
Previous: "api.demo.com/v1/users?limit=3&order=desc&order_by=surname&page=2",
Self: "api.demo.com/v1/users?page=3&limit=3&order_by=surname&order=desc",
},
Results: []*User{
{ID: 1, Name: "John", Surname: "Smith"},
{ID: 2, Name: "Jill", Surname: "Doe"},
},
},
err: nil,
},
}
// TestNewPage tests the paginator NewPage method.
func TestNewPage(t *testing.T) {
t.Log("NewPage")
// Check each test case
for _, testcase := range newPageDataProvider {
t.Log(testcase.name)
url, _ := url.Parse(testcase.url)
got, err := pagination.NewPage(url, testcase.results)
// Check error
if !reflect.DeepEqual(testcase.err, err) {
t.Errorf("Expected error to be %q but got %q", testcase.err, err)
}
// Check page
if !reflect.DeepEqual(testcase.want, got) {
t.Errorf("Expected page to be %q but got %q", testcase.want, got)
}
}
}