forked from nscuro/dtrack-client
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
util_test.go
115 lines (106 loc) · 2.55 KB
/
util_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
package dtrack
import (
"errors"
"github.com/stretchr/testify/require"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestFetchAll(t *testing.T) {
var wantItems []int
for i := 0; i < 468; i++ {
wantItems = append(wantItems, i)
}
gotItems, err := FetchAll(func(po PageOptions) (p Page[int], err error) {
for i := 0; i < po.PageSize; i++ {
idx := (po.PageSize * (po.PageNumber - 1)) + i
if idx >= len(wantItems) {
break
}
p.Items = append(p.Items, wantItems[idx])
}
p.TotalCount = len(wantItems)
return p, nil
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if diff := cmp.Diff(wantItems, gotItems); diff != "" {
t.Errorf("unexpected items:\n%s", diff)
}
}
func TestFetchAll_PageFetchFuncErr(t *testing.T) {
var testErr = errors.New("test error")
if _, err := FetchAll(
func(po PageOptions) (p Page[int], err error) {
return p, testErr
},
); !errors.Is(err, testErr) {
t.Errorf("expected err but got nil")
}
}
func TestForEach(t *testing.T) {
var (
wantItems []int
gotItems []int
)
for i := 0; i < 468; i++ {
wantItems = append(wantItems, i)
}
if err := ForEach(
func(po PageOptions) (p Page[int], err error) {
for i := 0; i < po.PageSize; i++ {
idx := (po.PageSize * (po.PageNumber - 1)) + i
if idx >= len(wantItems) {
break
}
p.Items = append(p.Items, wantItems[idx])
}
p.TotalCount = len(wantItems)
return p, nil
},
func(item int) error {
gotItems = append(gotItems, item)
return nil
},
); err != nil {
t.Errorf("unexpected error calling ForEach: %s", err)
}
if diff := cmp.Diff(wantItems, gotItems); diff != "" {
t.Errorf("unexpected items:\n%s", diff)
}
}
func TestForEach_PageFetchFuncErr(t *testing.T) {
var testErr = errors.New("test error")
if err := ForEach(
func(po PageOptions) (p Page[int], err error) {
return p, testErr
},
func(item int) error {
return nil
},
); !errors.Is(err, testErr) {
t.Errorf("expected error from pageFetchFunc but got nil")
}
}
func TestForEach_HandlerFuncErr(t *testing.T) {
var testErr = errors.New("test error")
if err := ForEach(
func(po PageOptions) (p Page[int], err error) {
p.Items = []int{0, 1, 2, 3}
p.TotalCount = len(p.Items)
return p, nil
},
func(item int) error {
return testErr
},
); !errors.Is(err, testErr) {
t.Errorf("expected error from handlerFunc but got nil")
}
}
func TestOptionalBoolOf(t *testing.T) {
require.True(t, *OptionalBoolOf(true))
require.False(t, *OptionalBoolOf(false))
}
func TestOptionalBool(t *testing.T) {
require.Nil(t, OptionalBool())
}