-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueries_projects_v2_test.go
322 lines (312 loc) · 9.55 KB
/
queries_projects_v2_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package api
import (
"errors"
"fmt"
"sort"
"strings"
"testing"
"unicode"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUpdateProjectV2Items(t *testing.T) {
var tests = []struct {
name string
httpStubs func(*httpmock.Registry)
expectError bool
}{
{
name: "updates project items",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`mutation UpdateProjectV2Items\b`),
httpmock.GraphQLQuery(`{"data":{"add_000":{"item":{"id":"1"}},"delete_001":{"item":{"id":"2"}}}}`,
func(mutations string, inputs map[string]interface{}) {
expectedMutations := `
mutation UpdateProjectV2Items(
$input_000: AddProjectV2ItemByIdInput!
$input_001: AddProjectV2ItemByIdInput!
$input_002: DeleteProjectV2ItemInput!
$input_003: DeleteProjectV2ItemInput!
) {
add_000: addProjectV2ItemById(input: $input_000) { item { id } }
add_001: addProjectV2ItemById(input: $input_001) { item { id } }
delete_002: deleteProjectV2Item(input: $input_002) { deletedItemId }
delete_003: deleteProjectV2Item(input: $input_003) { deletedItemId }
}`
assert.Equal(t, stripSpace(expectedMutations), stripSpace(mutations))
if len(inputs) != 4 {
t.Fatalf("expected 4 inputs, got %d", len(inputs))
}
i0 := inputs["input_000"].(map[string]interface{})
i1 := inputs["input_001"].(map[string]interface{})
i2 := inputs["input_002"].(map[string]interface{})
i3 := inputs["input_003"].(map[string]interface{})
adds := []string{
fmt.Sprintf("%v -> %v", i0["contentId"], i0["projectId"]),
fmt.Sprintf("%v -> %v", i1["contentId"], i1["projectId"]),
}
removes := []string{
fmt.Sprintf("%v x %v", i2["itemId"], i2["projectId"]),
fmt.Sprintf("%v x %v", i3["itemId"], i3["projectId"]),
}
sort.Strings(adds)
sort.Strings(removes)
assert.Equal(t, []string{"item1 -> project1", "item2 -> project2"}, adds)
assert.Equal(t, []string{"item3 x project3", "item4 x project4"}, removes)
}))
},
},
{
name: "fails to update project items",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`mutation UpdateProjectV2Items\b`),
httpmock.GraphQLMutation(`{"data":{}, "errors": [{"message": "some gql error"}]}`, func(inputs map[string]interface{}) {}),
)
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
client := newTestClient(reg)
repo, _ := ghrepo.FromFullName("OWNER/REPO")
addProjectItems := map[string]string{"project1": "item1", "project2": "item2"}
deleteProjectItems := map[string]string{"project3": "item3", "project4": "item4"}
err := UpdateProjectV2Items(client, repo, addProjectItems, deleteProjectItems)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestProjectsV2ItemsForIssue(t *testing.T) {
var tests = []struct {
name string
httpStubs func(*httpmock.Registry)
expectItems ProjectItems
expectError bool
}{
{
name: "retrieves project items for issue",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query IssueProjectItems\b`),
httpmock.GraphQLQuery(`{"data":{"repository":{"issue":{"projectItems":{"nodes": [{"id":"projectItem1"},{"id":"projectItem2"}]}}}}}`,
func(query string, inputs map[string]interface{}) {}),
)
},
expectItems: ProjectItems{
Nodes: []*ProjectV2Item{
{ID: "projectItem1"},
{ID: "projectItem2"},
},
},
},
{
name: "fails to retrieve project items for issue",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query IssueProjectItems\b`),
httpmock.GraphQLQuery(`{"data":{}, "errors": [{"message": "some gql error"}]}`,
func(query string, inputs map[string]interface{}) {}),
)
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
client := newTestClient(reg)
repo, _ := ghrepo.FromFullName("OWNER/REPO")
issue := &Issue{Number: 1}
err := ProjectsV2ItemsForIssue(client, repo, issue)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expectItems, issue.ProjectItems)
})
}
}
func TestProjectsV2ItemsForPullRequest(t *testing.T) {
var tests = []struct {
name string
httpStubs func(*httpmock.Registry)
expectItems ProjectItems
expectError bool
}{
{
name: "retrieves project items for pull request",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query PullRequestProjectItems\b`),
httpmock.GraphQLQuery(`{"data":{"repository":{"pullRequest":{"projectItems":{"nodes": [{"id":"projectItem3"},{"id":"projectItem4"}]}}}}}`,
func(query string, inputs map[string]interface{}) {}),
)
},
expectItems: ProjectItems{
Nodes: []*ProjectV2Item{
{ID: "projectItem3"},
{ID: "projectItem4"},
},
},
},
{
name: "fails to retrieve project items for pull request",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query PullRequestProjectItems\b`),
httpmock.GraphQLQuery(`{"data":{}, "errors": [{"message": "some gql error"}]}`,
func(query string, inputs map[string]interface{}) {}),
)
},
expectError: true,
},
{
name: "retrieves project items that have status columns",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query PullRequestProjectItems\b`),
httpmock.GraphQLQuery(`{
"data": {
"repository": {
"pullRequest": {
"projectItems": {
"nodes": [
{
"id": "PVTI_lADOB-vozM4AVk16zgK6U50",
"project": {
"id": "PVT_kwDOB-vozM4AVk16",
"title": "Test Project"
},
"status": {
"optionId": "47fc9ee4",
"name": "In Progress"
}
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "MQ"
}
}
}
}
}
}`,
func(query string, inputs map[string]interface{}) {
require.Equal(t, float64(1), inputs["number"])
require.Equal(t, "OWNER", inputs["owner"])
require.Equal(t, "REPO", inputs["name"])
}),
)
},
expectItems: ProjectItems{
Nodes: []*ProjectV2Item{
{
ID: "PVTI_lADOB-vozM4AVk16zgK6U50",
Project: ProjectV2ItemProject{
ID: "PVT_kwDOB-vozM4AVk16",
Title: "Test Project",
},
Status: ProjectV2ItemStatus{
OptionID: "47fc9ee4",
Name: "In Progress",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
client := newTestClient(reg)
repo, _ := ghrepo.FromFullName("OWNER/REPO")
pr := &PullRequest{Number: 1}
err := ProjectsV2ItemsForPullRequest(client, repo, pr)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, tt.expectItems, pr.ProjectItems)
})
}
}
func TestProjectsV2IgnorableError(t *testing.T) {
var tests = []struct {
name string
errMsg string
expectOut bool
}{
{
name: "read scope error",
errMsg: "field requires one of the following scopes: ['read:project']",
expectOut: true,
},
{
name: "repository projectsV2 field error",
errMsg: "Field 'projectsV2' doesn't exist on type 'Repository'",
expectOut: true,
},
{
name: "organization projectsV2 field error",
errMsg: "Field 'projectsV2' doesn't exist on type 'Organization'",
expectOut: true,
},
{
name: "issue projectItems field error",
errMsg: "Field 'projectItems' doesn't exist on type 'Issue'",
expectOut: true,
},
{
name: "pullRequest projectItems field error",
errMsg: "Field 'projectItems' doesn't exist on type 'PullRequest'",
expectOut: true,
},
{
name: "other error",
errMsg: "some other graphql error message",
expectOut: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := errors.New(tt.errMsg)
out := ProjectsV2IgnorableError(err)
assert.Equal(t, tt.expectOut, out)
})
}
}
func stripSpace(str string) string {
var b strings.Builder
b.Grow(len(str))
for _, ch := range str {
if !unicode.IsSpace(ch) {
b.WriteRune(ch)
}
}
return b.String()
}