forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination_test.go
117 lines (105 loc) · 3.31 KB
/
pagination_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
package query
import (
"testing"
)
func TestParsePagination(t *testing.T) {
// invalid limit
_, err := ParsePagination("1s", "0", "ptoken")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit - invalid syntax")
}
if err.Error() != "pagination: limit - invalid syntax" {
t.Errorf("invalid error: %s - expected: pagination: limit - invalid syntax", err)
}
// negative limit
_, err = ParsePagination("-1", "0", "ptoken")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit must be a positive value")
}
if err.Error() != "pagination: limit must be a positive value" {
t.Errorf("invalid error: %s - expected: pagination: limit must be a positive value", err)
}
// zero limit
_, err = ParsePagination("0", "0", "ptoken")
if err == nil {
t.Error("unexpected nil error - expected: pagination: limit must be a positive value")
}
if err.Error() != "pagination: limit must be a positive value" {
t.Errorf("invalid error: %s - expected: pagination: limit must be a positive value", err)
}
// invalid offset
_, err = ParsePagination("", "0w", "ptoken")
if err == nil {
t.Error("unexpected nil error - expected: pagination: offset - invalid syntax")
}
if err.Error() != "pagination: offset - invalid syntax" {
t.Errorf("invalid error: %s - expected: pagination: offset - invalid syntax", err)
}
// negative offset
_, err = ParsePagination("", "-1", "ptoken")
if err == nil {
t.Error("unexpected nil error - expected: pagination: offset - negative value")
}
if err.Error() != "pagination: offset - negative value" {
t.Errorf("invalid error: %s - expected: pagination: offset - negative value", err)
}
// null offset
p, err := ParsePagination("", "null", "ptoken")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if p.GetOffset() != 0 {
t.Errorf("invalid offset: %d - expected: 0", p.GetOffset())
}
// first page
p, err = ParsePagination("", "0", "ptoken")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !p.FirstPage() {
t.Errorf("invalid value of first page: %v - expected: true", p.FirstPage())
}
p, err = ParsePagination("", "100", "null")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !p.FirstPage() {
t.Errorf("invalid value of first page: %v - expected: true", p.FirstPage())
}
// default limit
if p.DefaultLimit(1000) != 1000 {
t.Errorf("invalid default limit: %d - expected: 1000", p.DefaultLimit(1000))
}
// valid pagination
p, err = ParsePagination("1000", "100", "ptoken")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if p.GetLimit() != 1000 {
t.Errorf("invalid limit: %d - expected: 1000", p.GetLimit())
}
if p.GetOffset() != 100 {
t.Errorf("invalid offset: %d - expected: 100", p.GetOffset())
}
if p.GetPageToken() != "ptoken" {
t.Errorf("invalid page token: %q - expected: ptoken", p.GetPageToken())
}
}
func TestPageInfo(t *testing.T) {
p := new(PageInfo)
if p.NoMore() {
t.Errorf("invalid value of NoMore: %v - expected: false", p.NoMore())
}
p.SetLastOffset()
if !p.NoMore() {
t.Errorf("invalid value of NoMore: %v - expected: true", p.NoMore())
}
p = new(PageInfo)
if p.NoMore() {
t.Errorf("invalid value of NoMore: %v - expected: false", p.NoMore())
}
p.SetLastToken()
if !p.NoMore() {
t.Errorf("invalid value of NoMore: %v - expected: true", p.NoMore())
}
}