-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
paginator_test.go
215 lines (200 loc) · 6.21 KB
/
paginator_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package keysetpagination
import (
"net/url"
"strconv"
"testing"
"github.com/gobuffalo/pop/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testItem struct {
ID string `db:"pk"`
CreatedAt string `db:"created_at"`
}
func (t testItem) PageToken() string {
return t.ID
}
func TestPaginator(t *testing.T) {
t.Run("paginates correctly", func(t *testing.T) {
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "postgres://foo.bar",
})
require.NoError(t, err)
q := pop.Q(c)
paginator := GetPaginator(WithSize(10), WithToken("token"))
q = q.Scope(Paginate[testItem](paginator))
sql, args := q.ToSQL(&pop.Model{Value: new(testItem)})
assert.Equal(t, "SELECT test_items.created_at, test_items.pk FROM test_items AS test_items WHERE \"pk\" > $1 ORDER BY \"pk\" ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"token"}, args)
})
t.Run("paginates correctly mysql", func(t *testing.T) {
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "mysql://user:pass@(host:1337)/database",
})
require.NoError(t, err)
q := pop.Q(c)
paginator := GetPaginator(WithSize(10), WithToken("token"))
q = q.Scope(Paginate[testItem](paginator))
sql, args := q.ToSQL(&pop.Model{Value: new(testItem)})
assert.Equal(t, "SELECT test_items.created_at, test_items.pk FROM test_items AS test_items WHERE `pk` > ? ORDER BY `pk` ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"token"}, args)
})
t.Run("returns correct result", func(t *testing.T) {
items := []testItem{
{ID: "1"},
{ID: "2"},
{ID: "3"},
{ID: "4"},
{ID: "5"},
{ID: "6"},
{ID: "7"},
{ID: "8"},
{ID: "9"},
{ID: "10"},
{ID: "11"},
}
paginator := GetPaginator(WithDefaultSize(10), WithToken("token"))
items, nextPage := Result(items, paginator)
assert.Len(t, items, 10)
assert.Equal(t, "10", nextPage.Token())
assert.Equal(t, 10, nextPage.Size())
})
t.Run("returns correct size and token", func(t *testing.T) {
for _, tc := range []struct {
name string
opts []Option
expectedSize int
expectedToken string
}{
{
name: "default",
opts: nil,
expectedSize: 100,
expectedToken: "",
},
{
name: "with size and token",
opts: []Option{WithSize(10), WithToken("token")},
expectedSize: 10,
expectedToken: "token",
},
{
name: "with custom defaults",
opts: []Option{WithDefaultSize(10), WithDefaultToken("token")},
expectedSize: 10,
expectedToken: "token",
},
{
name: "with custom defaults and size and token",
opts: []Option{WithDefaultSize(10), WithDefaultToken("token"), WithSize(20), WithToken("token2")},
expectedSize: 20,
expectedToken: "token2",
},
{
name: "with size and custom default and max size",
opts: []Option{WithSize(10), WithDefaultSize(20), WithMaxSize(5)},
expectedSize: 5,
},
} {
t.Run(tc.name, func(t *testing.T) {
paginator := GetPaginator(tc.opts...)
assert.Equal(t, tc.expectedSize, paginator.Size())
assert.Equal(t, tc.expectedToken, paginator.Token())
})
}
})
}
func TestParse(t *testing.T) {
for _, tc := range []struct {
name string
q url.Values
expectedSize int
expectedToken string
}{
{
name: "with page token",
q: url.Values{"page_token": {"token3"}},
expectedSize: 100,
expectedToken: "token3",
},
{
name: "with page size",
q: url.Values{"page_size": {"123"}},
expectedSize: 123,
},
{
name: "with page size and page token",
q: url.Values{"page_size": {"123"}, "page_token": {"token5"}},
expectedSize: 123,
expectedToken: "token5",
},
} {
t.Run(tc.name, func(t *testing.T) {
opts, err := Parse(tc.q)
require.NoError(t, err)
paginator := GetPaginator(opts...)
assert.Equal(t, tc.expectedSize, paginator.Size())
assert.Equal(t, tc.expectedToken, paginator.Token())
})
}
t.Run("invalid page size leads to err", func(t *testing.T) {
_, err := Parse(url.Values{"page_size": {"invalid-int"}})
require.ErrorIs(t, err, strconv.ErrSyntax)
})
}
func TestPaginateWithAdditionalColumn(t *testing.T) {
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "postgres://foo.bar",
})
require.NoError(t, err)
for _, tc := range []struct {
d string
opts []Option
e string
args []interface{}
}{
{
d: "with sort by created_at DESC",
opts: []Option{WithToken("pk=token_value/created_at=timestamp"), WithColumn("created_at", "DESC")},
e: "created_at\" < $1 OR (\"created_at\" = $2 AND \"pk\" > $3) ORDER BY \"created_at\" DESC, \"pk\" ASC",
args: []interface{}{"timestamp", "timestamp", "token_value"},
},
{
d: "with sort by created_at ASC",
opts: []Option{WithToken("pk=token_value/created_at=timestamp"), WithColumn("created_at", "ASC")},
e: "created_at\" > $1 OR (\"created_at\" = $2 AND \"pk\" > $3) ORDER BY \"created_at\" ASC, \"pk\" ASC",
args: []interface{}{"timestamp", "timestamp", "token_value"},
},
{
d: "with malformed token",
opts: []Option{WithToken("some/random/token"), WithColumn("created_at", "ASC")},
e: "WHERE \"pk\" > $1 ORDER BY \"pk\"",
args: []interface{}{"some/random/token"},
},
{
d: "with unknown column",
opts: []Option{WithToken("pk=token_value/created_at=timestamp"), WithColumn("unknown_column", "ASC")},
e: "WHERE \"pk\" > $1 ORDER BY \"pk\"",
args: []interface{}{"pk=token_value/created_at=timestamp"},
},
{
d: "with unknown order",
opts: []Option{WithToken("pk=token_value/created_at=timestamp"), WithColumn("created_at", Order("unknown order"))},
e: "WHERE \"pk\" > $1 ORDER BY \"pk\"",
args: []interface{}{"pk=token_value/created_at=timestamp"},
},
} {
t.Run("case="+tc.d, func(t *testing.T) {
opts := append(tc.opts, WithSize(10))
paginator := GetPaginator(opts...)
sql, args := pop.Q(c).
Scope(Paginate[testItem](paginator)).
ToSQL(&pop.Model{Value: new(testItem)})
assert.Contains(t, sql, tc.e)
assert.Contains(t, sql, "LIMIT 11")
assert.Equal(t, tc.args, args)
})
}
}