-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues_test.go
139 lines (131 loc) · 3.44 KB
/
issues_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
129
130
131
132
133
134
135
136
137
138
139
package ghx
import (
"context"
"errors"
"testing"
"github.com/bevicted/ghx/ghxtest"
"github.com/google/go-github/v62/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMapIssuesOfRepo(t *testing.T) {
t.Parallel()
const (
testOwner = "testOwner"
testRepo = "testRepo"
)
type listByRepoReturnValue struct {
Issues []*github.Issue
Res *github.Response
Err error
}
testCtx := context.Background()
for _, tc := range []struct {
name string
handlerErr error
expectIssueCount int
expectErr error
listByRepoReturnValues []listByRepoReturnValue
}{
{
name: "ok - 0 issues",
listByRepoReturnValues: []listByRepoReturnValue{
{
Res: &github.Response{},
},
},
},
{
name: "ok - 1 page, 1 issue",
listByRepoReturnValues: []listByRepoReturnValue{
{
Issues: ghxtest.NewEmptyIssues(t, 1),
Res: &github.Response{},
},
},
expectIssueCount: 1,
},
{
name: "ok - 2 pages, 8 issues",
listByRepoReturnValues: []listByRepoReturnValue{
{
Issues: ghxtest.NewEmptyIssues(t, 5),
Res: &github.Response{NextPage: 2},
},
{
Issues: ghxtest.NewEmptyIssues(t, 3),
Res: &github.Response{},
},
},
expectIssueCount: 8,
},
{
name: "err - 1 page, listByRepoErr on page 1",
listByRepoReturnValues: []listByRepoReturnValue{
{
Err: errors.New("listByRepoErr"),
},
},
expectErr: errors.New("listByRepoErr"),
},
{
name: "err - 2 pages, 8 issues, listByRepoErr on page 2",
listByRepoReturnValues: []listByRepoReturnValue{
{
Issues: ghxtest.NewEmptyIssues(t, 5),
Res: &github.Response{NextPage: 2},
},
{
Err: errors.New("listByRepoErr"),
},
},
expectIssueCount: 5,
expectErr: errors.New("listByRepoErr"),
},
{
name: "err - 2 pages, handlerErr fails before listByRepoErr",
listByRepoReturnValues: []listByRepoReturnValue{
{
Issues: ghxtest.NewEmptyIssues(t, 5),
Res: &github.Response{NextPage: 2},
},
{
Err: errors.New("listByRepoErr"),
},
},
handlerErr: errors.New("handlerErr"),
expectErr: errors.New("handlerErr"),
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
opts := &github.IssueListByRepoOptions{
Milestone: "testMilestone",
State: "testState",
Assignee: "testAssignee",
}
var listByRepoCallCount int
mapByRepo := newMapByRepoF(NewIssuesService(&IssuesServiceF{
ListByRepo: func(actualCtx context.Context, actualOwner, actualRepo string, actualOpts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error) {
assert.Equal(t, testCtx, actualCtx)
assert.Equal(t, testOwner, actualOwner)
assert.Equal(t, testRepo, actualRepo)
assert.Same(t, opts, actualOpts)
require.Less(t, listByRepoCallCount, len(tc.listByRepoReturnValues), "ListByRepo called more times than it has return values mocked")
rv := tc.listByRepoReturnValues[listByRepoCallCount]
listByRepoCallCount++
return rv.Issues, rv.Res, rv.Err
},
}))
var issueCount int
assert.Equal(t, tc.expectErr, mapByRepo(testCtx, testOwner, testRepo, opts, func(_ *github.Issue) error {
if tc.handlerErr == nil {
issueCount++
}
return tc.handlerErr
}))
assert.Equal(t, tc.expectIssueCount, issueCount, "handled different amount of issues than expected")
})
}
}