-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
paginator_test.go
173 lines (158 loc) · 4.92 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
// 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)
q := pop.Q(c)
paginator := GetPaginator(WithSize(10), WithToken("pk=token_value/created_at=timestamp"), WithColumn("created_at", "DESC"))
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 \"created_at\" > $1 OR (\"created_at\" = $2 AND \"pk\" > $3) ORDER BY \"created_at\" DESC, \"pk\" ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"timestamp", "timestamp", "token_value"}, args)
}