-
Notifications
You must be signed in to change notification settings - Fork 1
/
links_test.go
95 lines (89 loc) · 2.65 KB
/
links_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
package pagination_test
import (
"net/url"
"reflect"
"testing"
"github.com/yohgo/pagination"
)
// newLinksDataProvider provides data for the TestNewLinks function.
var newLinksDataProvider = []struct {
name string
url string
count int
links *pagination.Links
}{
{
name: "Successful links creation - no paging, no ordering",
url: "api.demo.com/v1/users",
count: 3,
links: &pagination.Links{
Next: "",
Previous: "",
Self: "api.demo.com/v1/users",
},
},
{
name: "Successful links creation - ordering, no paging",
url: "api.demo.com/v1/users?order_by=name&order=asc",
count: 3,
links: &pagination.Links{
Next: "",
Previous: "",
Self: "api.demo.com/v1/users?order_by=name&order=asc",
},
},
{
name: "Successful links creation - first page",
url: "api.demo.com/v1/users?page=1&limit=3&order_by=name&order=asc",
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",
},
},
{
name: "Successful links creation - second page",
url: "api.demo.com/v1/users?page=2&limit=3&order_by=name&order=asc",
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",
},
},
{
name: "Successful links creation - third page",
url: "api.demo.com/v1/users?page=3&limit=3&order_by=name&order=asc",
count: 3,
links: &pagination.Links{
Next: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=4",
Previous: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=2",
Self: "api.demo.com/v1/users?page=3&limit=3&order_by=name&order=asc",
},
},
{
name: "Successful links creation - last page",
url: "api.demo.com/v1/users?page=4&limit=3&order_by=name&order=asc",
count: 2,
links: &pagination.Links{
Next: "",
Previous: "api.demo.com/v1/users?limit=3&order=asc&order_by=name&page=3",
Self: "api.demo.com/v1/users?page=4&limit=3&order_by=name&order=asc",
},
},
}
// TestNewLinks tests the paginator NewLinks method.
func TestNewLinks(t *testing.T) {
t.Log("NewLinks")
// Check each test case
for _, testcase := range newLinksDataProvider {
t.Log(testcase.name)
url, _ := url.Parse(testcase.url)
links := pagination.NewLinks(url, testcase.count)
// Check links
if !reflect.DeepEqual(testcase.links, links) {
t.Errorf("Expected links to be %q but got %q", testcase.links, links)
}
}
}