-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctional_test.go
128 lines (112 loc) · 3.43 KB
/
functional_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
// Copyright 2024 T-Mobile USA, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// See the LICENSE file for additional language around the disclaimer of warranties.
// Trademark Disclaimer: Neither the name of “T-Mobile, USA” nor the names of
// its contributors may be used to endorse or promote products
package depaginator
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// Number of times to run tests; running the tests multiple times
// should help catch any race conditions or ordering errors
const TestCount = 5
type PagedData struct {
data []string // Actual data
perPage int // Number of results per page
reportItems bool // Report TotalItems
reportPages bool // Report TotalPages
pageAhead int // Number of page requests to create
}
func (pd PagedData) GetPage(_ context.Context, depag State, req PageRequest) ([]string, error) {
// First, update the items and pages
totalItems := 0
if pd.reportItems {
totalItems = len(pd.data)
}
totalPages := 0
if pd.reportPages {
totalPages = len(pd.data) / pd.perPage
if len(pd.data)%pd.perPage == 0 {
totalPages++
}
}
depag.Update(TotalItems(totalItems), TotalPages(totalPages), PerPage(pd.perPage))
// Next, generate the page requests
maxPage := pd.pageAhead
if maxPage < 1 {
maxPage = 1
}
for i := req.PageIndex + 1; i <= maxPage; i++ {
depag.Request(i, nil)
}
// Now generate and return a page
if req.PageIndex*pd.perPage >= len(pd.data) {
return nil, nil
}
subset := pd.data[req.PageIndex*pd.perPage:]
pageLen := pd.perPage
if len(subset) < pageLen {
pageLen = len(subset)
}
dest := make([]string, pageLen)
copy(dest, subset)
return dest, nil
}
func TestBasicFunction(t *testing.T) {
// Run the test several times to try to tickle any race conditions
// or similar errors
for i := 0; i < TestCount; i++ {
t.Run(fmt.Sprintf("basic-%d", i), func(t *testing.T) {
ctx := context.Background()
data := PagedData{
data: []string{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
},
perPage: 3,
pageAhead: 5,
}
result := &ListHandler[string]{}
d := Depaginate[string](ctx, data, result)
err := d.Wait()
assert.NoError(t, err)
assert.Equal(t, data.data, result.Items)
})
}
}
func TestAppendFunction(t *testing.T) {
// Run the test several times to try to tickle any race conditions
// or similar errors
for i := 0; i < TestCount; i++ {
t.Run(fmt.Sprintf("append-%d", i), func(t *testing.T) {
ctx := context.Background()
data := PagedData{
data: []string{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
},
perPage: 3,
pageAhead: 5,
}
result := &ListHandler[string]{}
d := Depaginate[string](ctx, data, result)
err := d.Wait()
assert.NoError(t, err)
d = Depaginate[string](ctx, data, result)
err = d.Wait()
assert.NoError(t, err)
assert.Equal(t, append(data.data, data.data...), result.Items)
})
}
}