-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_request_test.go
More file actions
260 lines (242 loc) · 6.78 KB
/
Copy pathcursor_request_test.go
File metadata and controls
260 lines (242 loc) · 6.78 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package pageable
import (
"net/url"
"testing"
)
func TestCursorRequestFromQuery(t *testing.T) {
tests := []struct {
name string
values url.Values
expectedCursor string
expectedSize int
expectedSort []Sort
}{
{
name: "defaults on empty values",
values: url.Values{},
expectedCursor: "",
expectedSize: DefaultCursorSize,
expectedSort: nil,
},
{
name: "with cursor and size",
values: url.Values{"cursor": {"abc123"}, "size": {"50"}},
expectedCursor: "abc123",
expectedSize: 50,
},
{
name: "size exceeds max",
values: url.Values{"size": {"5000"}},
expectedCursor: "",
expectedSize: MaxCursorSize,
},
{
name: "negative size clamps to default",
values: url.Values{"size": {"-5"}},
expectedCursor: "",
expectedSize: DefaultCursorSize,
},
{
name: "non-numeric size",
values: url.Values{"size": {"abc"}},
expectedCursor: "",
expectedSize: DefaultCursorSize,
},
{
name: "with sort",
values: url.Values{"sort": {"created_at,desc", "id,asc"}},
expectedCursor: "",
expectedSize: DefaultCursorSize,
expectedSort: []Sort{
{Field: "created_at", Direction: DESC},
{Field: "id", Direction: ASC},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := CursorRequestFromQuery(tt.values)
if req.Cursor != tt.expectedCursor {
t.Errorf("Cursor = %q, want %q", req.Cursor, tt.expectedCursor)
}
if req.Size != tt.expectedSize {
t.Errorf("Size = %d, want %d", req.Size, tt.expectedSize)
}
if tt.expectedSort == nil {
if req.Sort != nil {
t.Errorf("Sort = %v, want nil", req.Sort)
}
} else {
if len(req.Sort) != len(tt.expectedSort) {
t.Fatalf("Sort length = %d, want %d", len(req.Sort), len(tt.expectedSort))
}
for i, s := range req.Sort {
if s.Field != tt.expectedSort[i].Field || s.Direction != tt.expectedSort[i].Direction {
t.Errorf("Sort[%d] = %v, want %v", i, s, tt.expectedSort[i])
}
}
}
})
}
}
func TestNewCursorRequest(t *testing.T) {
tests := []struct {
name string
cursor string
size int
expectedSize int
}{
{"valid", "abc", 25, 25},
{"negative size", "abc", -1, DefaultCursorSize},
{"zero size", "abc", 0, DefaultCursorSize},
{"size over max", "abc", 5000, MaxCursorSize},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := NewCursorRequest(tt.cursor, tt.size, nil)
if req.Size != tt.expectedSize {
t.Errorf("Size = %d, want %d", req.Size, tt.expectedSize)
}
})
}
}
func TestCursorRequestHasCursor(t *testing.T) {
withCursor := CursorRequest{Cursor: "abc123"}
if !withCursor.HasCursor() {
t.Error("HasCursor() should be true")
}
withoutCursor := CursorRequest{Cursor: ""}
if withoutCursor.HasCursor() {
t.Error("HasCursor() should be false")
}
}
func TestCursorRequestDecodedCursor(t *testing.T) {
// Empty cursor returns empty CursorData
req := CursorRequest{Cursor: ""}
data, err := req.DecodedCursor()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if data.Value != "" {
t.Errorf("expected empty value, got %q", data.Value)
}
// Valid cursor data
original := CursorData{Value: "42", Extra: map[string]string{"ts": "12345"}}
encoded, _ := EncodeCursor(original)
req = CursorRequest{Cursor: encoded}
data, err = req.DecodedCursor()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if data.Value != "42" {
t.Errorf("Value = %q, want %q", data.Value, "42")
}
if data.Extra["ts"] != "12345" {
t.Errorf("Extra[ts] = %q, want %q", data.Extra["ts"], "12345")
}
}
func TestCursorRequestSortableFields(t *testing.T) {
// Removes disallowed fields
req := CursorRequest{
Size: 20,
Sort: []Sort{{Field: "name", Direction: ASC}, {Field: "secret", Direction: DESC}},
}
req = req.SortableFields("name", "id")
if len(req.Sort) != 1 || req.Sort[0].Field != "name" {
t.Errorf("Sort = %v, want [{name asc}]", req.Sort)
}
// All disallowed
req = CursorRequest{
Size: 20,
Sort: []Sort{{Field: "secret", Direction: ASC}},
}
req = req.SortableFields("name", "id")
if req.Sort != nil {
t.Errorf("Sort = %v, want nil", req.Sort)
}
}
func TestCursorRequestMapSortFields(t *testing.T) {
tests := []struct {
name string
sort []Sort
fieldMap map[string]string
expected []Sort
}{
{
name: "maps matching fields",
sort: []Sort{{Field: "createdAt", Direction: DESC}, {Field: "id", Direction: ASC}},
fieldMap: map[string]string{"createdAt": "created_at"},
expected: []Sort{{Field: "created_at", Direction: DESC}, {Field: "id", Direction: ASC}},
},
{
name: "nil sorts",
sort: nil,
fieldMap: map[string]string{"createdAt": "created_at"},
expected: nil,
},
{
name: "empty map returns sorts unchanged",
sort: []Sort{{Field: "name", Direction: ASC}},
fieldMap: map[string]string{},
expected: []Sort{{Field: "name", Direction: ASC}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := CursorRequest{Size: 20, Sort: tt.sort}
req = req.MapSortFields(tt.fieldMap)
if tt.expected == nil {
if req.Sort != nil {
t.Errorf("Sort = %v, want nil", req.Sort)
}
} else {
if len(req.Sort) != len(tt.expected) {
t.Fatalf("Sort length = %d, want %d", len(req.Sort), len(tt.expected))
}
for i, s := range req.Sort {
if s.Field != tt.expected[i].Field || s.Direction != tt.expected[i].Direction {
t.Errorf("Sort[%d] = %v, want %v", i, s, tt.expected[i])
}
}
}
})
}
}
func TestCursorRequestWithDefaultSort(t *testing.T) {
// Applies default when no sort is set
req := CursorRequest{Size: 20}
req = req.WithDefaultSort(Sort{Field: "id", Direction: ASC})
if len(req.Sort) != 1 || req.Sort[0].Field != "id" {
t.Errorf("Sort = %v, want [{id asc}]", req.Sort)
}
// Does not override existing sort
req = CursorRequest{Size: 20, Sort: []Sort{{Field: "name", Direction: DESC}}}
req = req.WithDefaultSort(Sort{Field: "id", Direction: ASC})
if len(req.Sort) != 1 || req.Sort[0].Field != "name" {
t.Errorf("Sort = %v, want [{name desc}]", req.Sort)
}
}
func TestCursorRequestOrderBy(t *testing.T) {
req := CursorRequest{
Size: 20,
Sort: []Sort{
{Field: "created_at", Direction: DESC},
{Field: "id", Direction: ASC},
},
}
expected := "created_at desc, id asc"
if got := req.OrderBy(); got != expected {
t.Errorf("OrderBy() = %q, want %q", got, expected)
}
// No sorts
req = CursorRequest{Size: 20}
if got := req.OrderBy(); got != "" {
t.Errorf("OrderBy() = %q, want empty", got)
}
}
func TestCursorRequestLimit(t *testing.T) {
req := CursorRequest{Size: 25}
if got := req.Limit(); got != 26 {
t.Errorf("Limit() = %d, want 26", got)
}
}