-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpagination_test.go
116 lines (106 loc) · 2.31 KB
/
pagination_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
package activity_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/bzimmer/activity"
)
type paginator struct {
err bool
total int
count int
page int
called int
iterations int
}
func (p *paginator) PageSize() int {
return p.page
}
func (p *paginator) Count() int {
return p.total
}
func (p *paginator) Do(_ context.Context, _ activity.Pagination) (int, error) {
if p.err {
return 0, errors.New("error in paginator.do")
}
p.called++
p.total += p.count
return p.count, nil
}
func TestPaginate(t *testing.T) {
t.Parallel()
a := assert.New(t)
tests := []struct {
name string
err string
pagination activity.Pagination
paginator *paginator
}{
{
name: "error in paginator.do",
err: "error in paginator.do",
pagination: activity.Pagination{
Total: 100, Count: 10, Start: 1,
},
paginator: &paginator{err: true},
},
{
name: "total less than zero",
err: "total less than zero",
pagination: activity.Pagination{
Total: -1, Count: 10, Start: 1,
},
paginator: &paginator{err: true},
},
{
name: "simple",
pagination: activity.Pagination{
Total: 100, Count: 10, Start: 1,
},
paginator: &paginator{iterations: 1},
},
{
name: "optimize",
pagination: activity.Pagination{
Total: 10, Count: 100, Start: 1,
},
paginator: &paginator{page: 100, total: 10, count: 100, iterations: 1},
},
{
name: "total <= count",
pagination: activity.Pagination{
Total: 10, Count: 100, Start: 1,
},
paginator: &paginator{total: 10, count: 100, iterations: 1},
},
{
name: "start == 0 && count <= 0",
pagination: activity.Pagination{
Total: 10, Count: 0, Start: 0,
},
paginator: &paginator{total: 10, count: 100, iterations: 1},
},
{
name: "two iterations",
pagination: activity.Pagination{
Total: 10, Count: 0, Start: 0,
},
paginator: &paginator{total: 5, count: 3, iterations: 2},
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := activity.Paginate(context.Background(), tt.paginator, tt.pagination)
if tt.err != "" {
a.Error(err)
a.Contains(err.Error(), tt.err)
return
}
a.NoError(err)
a.Equal(tt.paginator.iterations, tt.paginator.called)
})
}
}