-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_issue_test.go
157 lines (141 loc) · 3.7 KB
/
queries_issue_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
package api
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/httpmock"
)
func TestIssueList(t *testing.T) {
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))
http.Register(
httpmock.GraphQL(`query IssueList\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issues": {
"nodes": [],
"pageInfo": {
"hasNextPage": true,
"endCursor": "ENDCURSOR"
}
}
} } }
`),
)
http.Register(
httpmock.GraphQL(`query IssueList\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issues": {
"nodes": [],
"pageInfo": {
"hasNextPage": false,
"endCursor": "ENDCURSOR"
}
}
} } }
`),
)
repo, _ := ghrepo.FromFullName("OWNER/REPO")
_, err := IssueList(client, repo, "open", []string{}, "", 251, "", "", "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(http.Requests) != 2 {
t.Fatalf("expected 2 HTTP requests, seen %d", len(http.Requests))
}
var reqBody struct {
Query string
Variables map[string]interface{}
}
bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
_ = json.Unmarshal(bodyBytes, &reqBody)
if reqLimit := reqBody.Variables["limit"].(float64); reqLimit != 100 {
t.Errorf("expected 100, got %v", reqLimit)
}
if _, cursorPresent := reqBody.Variables["endCursor"]; cursorPresent {
t.Error("did not expect first request to pass 'endCursor'")
}
bodyBytes, _ = ioutil.ReadAll(http.Requests[1].Body)
_ = json.Unmarshal(bodyBytes, &reqBody)
if endCursor := reqBody.Variables["endCursor"].(string); endCursor != "ENDCURSOR" {
t.Errorf("expected %q, got %q", "ENDCURSOR", endCursor)
}
}
func TestIssueList_pagination(t *testing.T) {
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))
http.Register(
httpmock.GraphQL(`query IssueList\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issues": {
"nodes": [
{
"title": "issue1",
"labels": { "nodes": [ { "name": "bug" } ], "totalCount": 1 },
"assignees": { "nodes": [ { "login": "user1" } ], "totalCount": 1 }
}
],
"pageInfo": {
"hasNextPage": true,
"endCursor": "ENDCURSOR"
},
"totalCount": 2
}
} } }
`),
)
http.Register(
httpmock.GraphQL(`query IssueList\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issues": {
"nodes": [
{
"title": "issue2",
"labels": { "nodes": [ { "name": "enhancement" } ], "totalCount": 1 },
"assignees": { "nodes": [ { "login": "user2" } ], "totalCount": 1 }
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "ENDCURSOR"
},
"totalCount": 2
}
} } }
`),
)
repo := ghrepo.New("OWNER", "REPO")
res, err := IssueList(client, repo, "", nil, "", 0, "", "", "")
if err != nil {
t.Fatalf("IssueList() error = %v", err)
}
assert.Equal(t, 2, res.TotalCount)
assert.Equal(t, 2, len(res.Issues))
getLabels := func(i Issue) []string {
var labels []string
for _, l := range i.Labels.Nodes {
labels = append(labels, l.Name)
}
return labels
}
getAssignees := func(i Issue) []string {
var logins []string
for _, u := range i.Assignees.Nodes {
logins = append(logins, u.Login)
}
return logins
}
assert.Equal(t, []string{"bug"}, getLabels(res.Issues[0]))
assert.Equal(t, []string{"user1"}, getAssignees(res.Issues[0]))
assert.Equal(t, []string{"enhancement"}, getLabels(res.Issues[1]))
assert.Equal(t, []string{"user2"}, getAssignees(res.Issues[1]))
}