1- package api
21
3- import (
4- "errors"
5- "fmt"
6- "sort"
7- "strings"
8- "testing"
9- "unicode"
10-
11- "github.com/cli/cli/v2/internal/ghrepo"
12- "github.com/cli/cli/v2/pkg/httpmock"
13- "github.com/stretchr/testify/assert"
14- "github.com/stretchr/testify/require"
15- )
16-
17- func TestUpdateProjectV2Items (t * testing.T ) {
18- var tests = []struct {
19- name string
20- httpStubs func (* httpmock.Registry )
21- expectError bool
222 }{
233 {
244 name : "updates project items" ,
@@ -64,17 +44,7 @@ func TestUpdateProjectV2Items(t *testing.T) {
6444 },
6545 {
6646 name : "fails to update project items" ,
67- httpStubs : func (reg * httpmock.Registry ) {
68- reg .Register (
69- httpmock .GraphQL (`mutation UpdateProjectV2Items\b` ),
70- httpmock .GraphQLMutation (`{"data":{}, "errors": [{"message": "some gql error"}]}` , func (inputs map [string ]interface {}) {}),
71- )
72- },
73- expectError : true ,
74- },
75- }
76-
77- for _ , tt := range tests {
47+ httpStubs : f {
7848 t .Run (tt .name , func (t * testing.T ) {
7949 reg := & httpmock.Registry {}
8050 defer reg .Verify (t )
@@ -84,19 +54,7 @@ func TestUpdateProjectV2Items(t *testing.T) {
8454 client := newTestClient (reg )
8555 repo , _ := ghrepo .FromFullName ("OWNER/REPO" )
8656 addProjectItems := map [string ]string {"project1" : "item1" , "project2" : "item2" }
87- deleteProjectItems := map [string ]string {"project3" : "item3" , "project4" : "item4" }
88- err := UpdateProjectV2Items (client , repo , addProjectItems , deleteProjectItems )
89- if tt .expectError {
90- assert .Error (t , err )
91- } else {
92- assert .NoError (t , err )
93- }
94- })
95- }
96- }
97-
98- func TestProjectsV2ItemsForIssue (t * testing.T ) {
99- var tests = []struct {
57+ deleteP = []struct {
10058 name string
10159 httpStubs func (* httpmock.Registry )
10260 expectItems ProjectItems
@@ -120,40 +78,15 @@ func TestProjectsV2ItemsForIssue(t *testing.T) {
12078 },
12179 {
12280 name : "fails to retrieve project items for issue" ,
123- httpStubs : func (reg * httpmock.Registry ) {
124- reg .Register (
125- httpmock .GraphQL (`query IssueProjectItems\b` ),
126- httpmock .GraphQLQuery (`{"data":{}, "errors": [{"message": "some gql error"}]}` ,
127- func (query string , inputs map [string ]interface {}) {}),
128- )
129- },
130- expectError : true ,
131- },
132- }
133-
134- for _ , tt := range tests {
81+ httpStubs : func (reg * http {
13582 t .Run (tt .name , func (t * testing.T ) {
13683 reg := & httpmock.Registry {}
13784 defer reg .Verify (t )
13885 if tt .httpStubs != nil {
13986 tt .httpStubs (reg )
14087 }
14188 client := newTestClient (reg )
142- repo , _ := ghrepo .FromFullName ("OWNER/REPO" )
143- issue := & Issue {Number : 1 }
144- err := ProjectsV2ItemsForIssue (client , repo , issue )
145- if tt .expectError {
146- assert .Error (t , err )
147- } else {
148- assert .NoError (t , err )
149- }
150- assert .Equal (t , tt .expectItems , issue .ProjectItems )
151- })
152- }
153- }
154-
155- func TestProjectsV2ItemsForPullRequest (t * testing.T ) {
156- var tests = []struct {
89+ repo , _ := ghrepo.FromFullNa = []struct {
15790 name string
15891 httpStubs func (* httpmock.Registry )
15992 expectItems ProjectItems
@@ -200,18 +133,7 @@ func TestProjectsV2ItemsForPullRequest(t *testing.T) {
200133 {
201134 "id": "PVTI_lADOB-vozM4AVk16zgK6U50",
202135 "project": {
203- "id": "PVT_kwDOB-vozM4AVk16",
204- "title": "Test Project"
205- },
206- "status": {
207- "optionId": "47fc9ee4",
208- "name": "In Progress"
209- }
210- }
211- ],
212- "pageInfo": {
213- "hasNextPage": false,
214- "endCursor": "MQ"
136+ "i
215137 }
216138 }
217139 }
@@ -228,44 +150,15 @@ func TestProjectsV2ItemsForPullRequest(t *testing.T) {
228150 expectItems : ProjectItems {
229151 Nodes : []* ProjectV2Item {
230152 {
231- ID : "PVTI_lADOB-vozM4AVk16zgK6U50" ,
232- Project : ProjectV2ItemProject {
233- ID : "PVT_kwDOB-vozM4AVk16" ,
234- Title : "Test Project" ,
235- },
236- Status : ProjectV2ItemStatus {
237- OptionID : "47fc9ee4" ,
238- Name : "In Progress" ,
239- },
240- },
241- },
242- },
243- },
244- }
245-
246- for _ , tt := range tests {
153+ I {
247154 t .Run (tt .name , func (t * testing.T ) {
248155 reg := & httpmock.Registry {}
249156 defer reg .Verify (t )
250157 if tt .httpStubs != nil {
251158 tt .httpStubs (reg )
252159 }
253160 client := newTestClient (reg )
254- repo , _ := ghrepo .FromFullName ("OWNER/REPO" )
255- pr := & PullRequest {Number : 1 }
256- err := ProjectsV2ItemsForPullRequest (client , repo , pr )
257- if tt .expectError {
258- require .Error (t , err )
259- } else {
260- require .NoError (t , err )
261- }
262- require .Equal (t , tt .expectItems , pr .ProjectItems )
263- })
264- }
265- }
266-
267- func TestProjectsV2IgnorableError (t * testing.T ) {
268- var tests = []struct {
161+ repo , _ := ghrepo.FromFullName ("OW = []struct {
269162 name string
270163 errMsg string
271164 expectOut bool
@@ -288,20 +181,7 @@ func TestProjectsV2IgnorableError(t *testing.T) {
288181 {
289182 name : "issue projectItems field error" ,
290183 errMsg : "Field 'projectItems' doesn't exist on type 'Issue'" ,
291- expectOut : true ,
292- },
293- {
294- name : "pullRequest projectItems field error" ,
295- errMsg : "Field 'projectItems' doesn't exist on type 'PullRequest'" ,
296- expectOut : true ,
297- },
298- {
299- name : "other error" ,
300- errMsg : "some other graphql error message" ,
301- expectOut : false ,
302- },
303- }
304- for _ , tt := range tests {
184+ expectOut : tru {
305185 t .Run (tt .name , func (t * testing.T ) {
306186 err := errors .New (tt .errMsg )
307187 out := ProjectsV2IgnorableError (err )
0 commit comments