forked from ulule/paging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
174 lines (139 loc) · 5.1 KB
/
utils_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
package paging
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidateLimitOffset(t *testing.T) {
is := assert.New(t)
is.True(ValidateLimitOffset(20, 10))
is.True(ValidateLimitOffset(1, 1))
is.True(ValidateLimitOffset(1, 1))
is.True(ValidateLimitOffset(1, 0))
is.True(ValidateLimitOffset(0, 1))
is.False(ValidateLimitOffset(-1, 1))
is.False(ValidateLimitOffset(1, -1))
}
func TestGetLimitFromRequest(t *testing.T) {
is := assert.New(t)
options := NewOptions()
// We define a default limit...
options.DefaultLimit = 40
request, _ := http.NewRequest("GET", "http://example.com", nil)
is.Equal(int64(40), GetLimitFromRequest(request, options))
// But even we define a default limit, user must be able to override it
// by setting it in the URL.
options.DefaultLimit = 10
request, _ = http.NewRequest("GET", "http://example.com/?limit=33", nil)
is.Equal(int64(33), GetLimitFromRequest(request, options))
// If user use a different query string key, we must set the default one.
options.DefaultLimit = 20
options.LimitKeyName = "perpage"
request, _ = http.NewRequest("GET", "http://example.com/?limit=33", nil)
is.Equal(int64(20), GetLimitFromRequest(request, options))
// Now, let's check with a good query string key.
options.DefaultLimit = 20
options.LimitKeyName = "perpage"
request, _ = http.NewRequest("GET", "http://example.com/?perpage=56", nil)
is.Equal(int64(56), GetLimitFromRequest(request, options))
// We restrict with a max limit
options.MaxLimit = 15
options.LimitKeyName = "limit"
request, _ = http.NewRequest("GET", "http://example.com/?limit=100", nil)
is.Equal(int64(15), GetLimitFromRequest(request, options))
}
func TestGetOffsetFromRequest(t *testing.T) {
is := assert.New(t)
options := NewOptions()
// No offset in URL, we should have 0.
request, _ := http.NewRequest("GET", "http://example.com", nil)
is.Equal(int64(0), GetOffsetFromRequest(request, options))
// Offset in URL, let's get it.
request, _ = http.NewRequest("GET", "http://example.com?offset=42", nil)
is.Equal(int64(42), GetOffsetFromRequest(request, options))
// If user use a different query string key, we must set the default one.
request, _ = http.NewRequest("GET", "http://example.com?offshore=90", nil)
is.Equal(int64(0), GetOffsetFromRequest(request, options))
// Now, let's check with a good query string key.
options.OffsetKeyName = "mayday"
request, _ = http.NewRequest("GET", "http://example.com?mayday=90", nil)
is.Equal(int64(90), GetOffsetFromRequest(request, options))
}
func TestGetCursorFromRequest(t *testing.T) {
is := assert.New(t)
options := NewOptions()
// No cursor in URL, we should have 0.
request, _ := http.NewRequest("GET", "http://example.com", nil)
is.Equal(int64(0), GetCursorFromRequest(request, options))
// Offset in URL, let's get it.
request, _ = http.NewRequest("GET", "http://example.com?since=42", nil)
is.Equal(int64(42), GetCursorFromRequest(request, options))
// If user use a different query string key, we must set the default one.
request, _ = http.NewRequest("GET", "http://example.com?offshore=90", nil)
is.Equal(int64(0), GetCursorFromRequest(request, options))
// Now, let's check with a good query string key.
options.CursorOptions.KeyName = "mayday"
request, _ = http.NewRequest("GET", "http://example.com?mayday=90", nil)
is.Equal(int64(90), GetCursorFromRequest(request, options))
}
func TestGenerateOffsetURI(t *testing.T) {
is := assert.New(t)
options := NewOptions()
is.Equal("?limit=10&offset=40", GenerateOffsetURI(int64(10), int64(40), options))
options.LimitKeyName = "l"
options.OffsetKeyName = "o"
is.Equal("?l=14&o=60", GenerateOffsetURI(int64(14), int64(60), options))
}
func TestGenerateCursorURI(t *testing.T) {
is := assert.New(t)
options := NewOptions()
is.Equal("?limit=10&since=40", GenerateCursorURI(int64(10), int64(40), options))
options.LimitKeyName = "l"
options.CursorOptions.KeyName = "o"
is.Equal("?l=14&o=60", GenerateCursorURI(int64(14), int64(60), options))
}
func Test_GetLastElementField(t *testing.T) {
last := getLastElementField(
[]struct{ Fieldname int }{
{Fieldname: 1},
{Fieldname: 2},
{Fieldname: 3}},
"Fieldname")
assert.New(t).Equal(3, last)
}
func Test_GetLastElementField_Int(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected panic")
}
expected := "can't get last element of a value of type int"
if r.(string) != expected {
t.Fatalf("expected %q, got %q", expected, r)
}
}()
getLastElementField(1, "fieldname")
}
func Test_GetLastElementField_IntSlice(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected panic")
}
expected := "can't get fieldname \"fieldname\" of an element of type int"
if r.(string) != expected {
t.Fatalf("expected %q, got %q", expected, r)
}
}()
getLastElementField([]int{1}, "fieldname")
}
func Test_GetLen(t *testing.T) {
assert.New(t).Equal(3, getLen([]int{1, 2, 3}))
}
func Test_PopLastElement(t *testing.T) {
array := &[]int{1, 2, 3}
last, remaining := popLastElement(array)
is := assert.New(t)
is.Equal(3, last)
is.Equal(&[]int{1, 2}, remaining)
}