github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/api/queries_issue_test.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/abdfnx/gh-api/internal/ghrepo" 11 "github.com/abdfnx/gh-api/pkg/httpmock" 12 ) 13 14 func TestIssueList(t *testing.T) { 15 http := &httpmock.Registry{} 16 client := NewClient(ReplaceTripper(http)) 17 18 http.Register( 19 httpmock.GraphQL(`query IssueList\b`), 20 httpmock.StringResponse(` 21 { "data": { "repository": { 22 "hasIssuesEnabled": true, 23 "issues": { 24 "nodes": [], 25 "pageInfo": { 26 "hasNextPage": true, 27 "endCursor": "ENDCURSOR" 28 } 29 } 30 } } } 31 `), 32 ) 33 http.Register( 34 httpmock.GraphQL(`query IssueList\b`), 35 httpmock.StringResponse(` 36 { "data": { "repository": { 37 "hasIssuesEnabled": true, 38 "issues": { 39 "nodes": [], 40 "pageInfo": { 41 "hasNextPage": false, 42 "endCursor": "ENDCURSOR" 43 } 44 } 45 } } } 46 `), 47 ) 48 49 repo, _ := ghrepo.FromFullName("OWNER/REPO") 50 _, err := IssueList(client, repo, "open", "", 251, "", "", "") 51 if err != nil { 52 t.Fatalf("unexpected error: %v", err) 53 } 54 55 if len(http.Requests) != 2 { 56 t.Fatalf("expected 2 HTTP requests, seen %d", len(http.Requests)) 57 } 58 var reqBody struct { 59 Query string 60 Variables map[string]interface{} 61 } 62 63 bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) 64 _ = json.Unmarshal(bodyBytes, &reqBody) 65 if reqLimit := reqBody.Variables["limit"].(float64); reqLimit != 100 { 66 t.Errorf("expected 100, got %v", reqLimit) 67 } 68 if _, cursorPresent := reqBody.Variables["endCursor"]; cursorPresent { 69 t.Error("did not expect first request to pass 'endCursor'") 70 } 71 72 bodyBytes, _ = ioutil.ReadAll(http.Requests[1].Body) 73 _ = json.Unmarshal(bodyBytes, &reqBody) 74 if endCursor := reqBody.Variables["endCursor"].(string); endCursor != "ENDCURSOR" { 75 t.Errorf("expected %q, got %q", "ENDCURSOR", endCursor) 76 } 77 } 78 79 func TestIssueList_pagination(t *testing.T) { 80 http := &httpmock.Registry{} 81 client := NewClient(ReplaceTripper(http)) 82 83 http.Register( 84 httpmock.GraphQL(`query IssueList\b`), 85 httpmock.StringResponse(` 86 { "data": { "repository": { 87 "hasIssuesEnabled": true, 88 "issues": { 89 "nodes": [ 90 { 91 "title": "issue1", 92 "labels": { "nodes": [ { "name": "bug" } ], "totalCount": 1 }, 93 "assignees": { "nodes": [ { "login": "user1" } ], "totalCount": 1 } 94 } 95 ], 96 "pageInfo": { 97 "hasNextPage": true, 98 "endCursor": "ENDCURSOR" 99 }, 100 "totalCount": 2 101 } 102 } } } 103 `), 104 ) 105 106 http.Register( 107 httpmock.GraphQL(`query IssueList\b`), 108 httpmock.StringResponse(` 109 { "data": { "repository": { 110 "hasIssuesEnabled": true, 111 "issues": { 112 "nodes": [ 113 { 114 "title": "issue2", 115 "labels": { "nodes": [ { "name": "enhancement" } ], "totalCount": 1 }, 116 "assignees": { "nodes": [ { "login": "user2" } ], "totalCount": 1 } 117 } 118 ], 119 "pageInfo": { 120 "hasNextPage": false, 121 "endCursor": "ENDCURSOR" 122 }, 123 "totalCount": 2 124 } 125 } } } 126 `), 127 ) 128 129 repo := ghrepo.New("OWNER", "REPO") 130 res, err := IssueList(client, repo, "", "", 0, "", "", "") 131 if err != nil { 132 t.Fatalf("IssueList() error = %v", err) 133 } 134 135 assert.Equal(t, 2, res.TotalCount) 136 assert.Equal(t, 2, len(res.Issues)) 137 138 getLabels := func(i Issue) []string { 139 var labels []string 140 for _, l := range i.Labels.Nodes { 141 labels = append(labels, l.Name) 142 } 143 return labels 144 } 145 getAssignees := func(i Issue) []string { 146 var logins []string 147 for _, u := range i.Assignees.Nodes { 148 logins = append(logins, u.Login) 149 } 150 return logins 151 } 152 153 assert.Equal(t, []string{"bug"}, getLabels(res.Issues[0])) 154 assert.Equal(t, []string{"user1"}, getAssignees(res.Issues[0])) 155 assert.Equal(t, []string{"enhancement"}, getLabels(res.Issues[1])) 156 assert.Equal(t, []string{"user2"}, getAssignees(res.Issues[1])) 157 }