|
1
|
|
|
package database |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/stretchr/testify/assert" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
func TestNewPagination(t *testing.T) { |
|
10
|
|
|
tests := []struct { |
|
11
|
|
|
name string |
|
12
|
|
|
opts []PaginationOption |
|
13
|
|
|
wantSize uint32 |
|
14
|
|
|
wantToken string |
|
15
|
|
|
}{ |
|
16
|
|
|
{ |
|
17
|
|
|
name: "Default size", |
|
18
|
|
|
opts: []PaginationOption{}, |
|
19
|
|
|
wantSize: 0, |
|
20
|
|
|
wantToken: "", |
|
21
|
|
|
}, |
|
22
|
|
|
{ |
|
23
|
|
|
name: "Custom size and token", |
|
24
|
|
|
opts: []PaginationOption{ |
|
25
|
|
|
Size(50), |
|
26
|
|
|
Token("abc123"), |
|
27
|
|
|
}, |
|
28
|
|
|
wantSize: 50, |
|
29
|
|
|
wantToken: "abc123", |
|
30
|
|
|
}, |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
for _, tt := range tests { |
|
34
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
35
|
|
|
p := NewPagination(tt.opts...) |
|
36
|
|
|
|
|
37
|
|
|
if p.size != tt.wantSize { |
|
38
|
|
|
t.Errorf("NewPagination() size = %d, want %d", p.size, tt.wantSize) |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if p.token != tt.wantToken { |
|
42
|
|
|
t.Errorf("NewPagination() token = %s, want %s", p.token, tt.wantToken) |
|
43
|
|
|
} |
|
44
|
|
|
}) |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
func TestPagination(t *testing.T) { |
|
49
|
|
|
// Test default page size |
|
50
|
|
|
p := NewPagination() |
|
51
|
|
|
if p.PageSize() != 0 { |
|
52
|
|
|
t.Errorf("Expected default page size of %d, but got %d", 0, p.PageSize()) |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Test custom page size |
|
56
|
|
|
p = NewPagination(Size(25)) |
|
57
|
|
|
if p.PageSize() != 25 { |
|
58
|
|
|
t.Errorf("Expected page size of 25, but got %d", p.PageSize()) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Test token |
|
62
|
|
|
p = NewPagination(Token("my-token")) |
|
63
|
|
|
if p.Token() != "my-token" { |
|
64
|
|
|
t.Errorf("Expected token of 'my-token', but got '%s'", p.Token()) |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
func TestNoopContinuousToken(t *testing.T) { |
|
69
|
|
|
token := NewNoopContinuousToken() |
|
70
|
|
|
|
|
71
|
|
|
// Test Encode |
|
72
|
|
|
encodedToken := token.Encode() |
|
73
|
|
|
assert.Empty(t, encodedToken.String()) |
|
74
|
|
|
|
|
75
|
|
|
// Test Decode |
|
76
|
|
|
decodedToken, err := encodedToken.Decode() |
|
77
|
|
|
assert.NoError(t, err) |
|
78
|
|
|
assert.Empty(t, decodedToken.(NoopContinuousToken).Value) |
|
79
|
|
|
|
|
80
|
|
|
// Test Encode and Decode |
|
81
|
|
|
assert.Empty(t, decodedToken.(NoopContinuousToken).Value) |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
func TestNewCursorPagination(t *testing.T) { |
|
85
|
|
|
tests := []struct { |
|
86
|
|
|
name string |
|
87
|
|
|
opts []CursorPaginationOption |
|
88
|
|
|
wantSort string |
|
89
|
|
|
wantCursor string |
|
90
|
|
|
}{ |
|
91
|
|
|
{ |
|
92
|
|
|
name: "Default size", |
|
93
|
|
|
opts: []CursorPaginationOption{}, |
|
94
|
|
|
wantSort: "", |
|
95
|
|
|
wantCursor: "", |
|
96
|
|
|
}, |
|
97
|
|
|
{ |
|
98
|
|
|
name: "Custom size and token", |
|
99
|
|
|
opts: []CursorPaginationOption{ |
|
100
|
|
|
Sort("entity_id"), |
|
101
|
|
|
Cursor("abc123"), |
|
102
|
|
|
}, |
|
103
|
|
|
wantSort: "entity_id", |
|
104
|
|
|
wantCursor: "abc123", |
|
105
|
|
|
}, |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
for _, tt := range tests { |
|
109
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
110
|
|
|
p := NewCursorPagination(tt.opts...) |
|
111
|
|
|
|
|
112
|
|
|
if p.sort != tt.wantSort { |
|
113
|
|
|
t.Errorf("NewCursorPagination() size = %s, want %s", p.sort, tt.wantSort) |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if p.cursor != tt.wantCursor { |
|
117
|
|
|
t.Errorf("NewCursorPagination() cursor = %s, want %s", p.cursor, tt.wantCursor) |
|
118
|
|
|
} |
|
119
|
|
|
}) |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
func TestCursorPagination(t *testing.T) { |
|
124
|
|
|
// Test default page size |
|
125
|
|
|
p := NewCursorPagination() |
|
126
|
|
|
if p.Sort() != "" { |
|
127
|
|
|
t.Errorf("Expected default sort empty string, but got %s", p.Sort()) |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Test custom page size |
|
131
|
|
|
p = NewCursorPagination(Sort("entity_id")) |
|
132
|
|
|
if p.Sort() != "entity_id" { |
|
133
|
|
|
t.Errorf("Expected sort entity_id, but got %s", p.Sort()) |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Test token |
|
137
|
|
|
p = NewCursorPagination(Cursor("my-cursor")) |
|
138
|
|
|
if p.Cursor() != "my-cursor" { |
|
139
|
|
|
t.Errorf("Expected token of 'my-cursor', but got '%s'", p.Cursor()) |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Test limit |
|
143
|
|
|
p = NewCursorPagination(Limit(100)) |
|
144
|
|
|
if p.Limit() != 100 { |
|
145
|
|
|
t.Errorf("Expected limit of 100, but got %d", p.Limit()) |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// Test default limit |
|
149
|
|
|
p = NewCursorPagination() |
|
150
|
|
|
if p.Limit() != 0 { |
|
151
|
|
|
t.Errorf("Expected default limit of 0, but got %d", p.Limit()) |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// Test multiple options together |
|
155
|
|
|
p = NewCursorPagination(Sort("entity_id"), Cursor("my-cursor"), Limit(50)) |
|
156
|
|
|
if p.Sort() != "entity_id" { |
|
157
|
|
|
t.Errorf("Expected sort entity_id, but got %s", p.Sort()) |
|
158
|
|
|
} |
|
159
|
|
|
if p.Cursor() != "my-cursor" { |
|
160
|
|
|
t.Errorf("Expected cursor of 'my-cursor', but got '%s'", p.Cursor()) |
|
161
|
|
|
} |
|
162
|
|
|
if p.Limit() != 50 { |
|
163
|
|
|
t.Errorf("Expected limit of 50, but got %d", p.Limit()) |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|