X Tutup
Skip to content

Commit 13b9c98

Browse files
committed
Match named queries in test stubs
1 parent d96e88c commit 13b9c98

File tree

7 files changed

+355
-297
lines changed

7 files changed

+355
-297
lines changed

api/queries_repo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,11 @@ func RepoAssignableUsers(client *Client, repo ghrepo.Interface) ([]RepoAssignee,
754754
"endCursor": (*githubv4.String)(nil),
755755
}
756756

757-
v4 := githubv4.NewClient(client.http)
757+
gql := graphQLClient(client.http)
758758

759759
var users []RepoAssignee
760760
for {
761-
err := v4.Query(context.Background(), &query, variables)
761+
err := gql.QueryNamed(context.Background(), "RepositoryAssignableUsers", &query, variables)
762762
if err != nil {
763763
return nil, err
764764
}
@@ -798,11 +798,11 @@ func RepoLabels(client *Client, repo ghrepo.Interface) ([]RepoLabel, error) {
798798
"endCursor": (*githubv4.String)(nil),
799799
}
800800

801-
v4 := githubv4.NewClient(client.http)
801+
gql := graphQLClient(client.http)
802802

803803
var labels []RepoLabel
804804
for {
805-
err := v4.Query(context.Background(), &query, variables)
805+
err := gql.QueryNamed(context.Background(), "RepositoryLabelList", &query, variables)
806806
if err != nil {
807807
return nil, err
808808
}
@@ -842,11 +842,11 @@ func RepoMilestones(client *Client, repo ghrepo.Interface) ([]RepoMilestone, err
842842
"endCursor": (*githubv4.String)(nil),
843843
}
844844

845-
v4 := githubv4.NewClient(client.http)
845+
gql := graphQLClient(client.http)
846846

847847
var milestones []RepoMilestone
848848
for {
849-
err := v4.Query(context.Background(), &query, variables)
849+
err := gql.QueryNamed(context.Background(), "RepositoryMilestoneList", &query, variables)
850850
if err != nil {
851851
return nil, err
852852
}

command/issue_test.go

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"io/ioutil"
7-
"os"
87
"os/exec"
98
"regexp"
109
"strings"
@@ -14,19 +13,19 @@ import (
1413
"github.com/cli/cli/pkg/httpmock"
1514
"github.com/cli/cli/test"
1615
"github.com/google/go-cmp/cmp"
16+
"github.com/stretchr/testify/assert"
1717
)
1818

1919
func TestIssueStatus(t *testing.T) {
2020
initBlankContext("", "OWNER/REPO", "master")
2121
http := initFakeHTTP()
2222
http.StubRepoResponse("OWNER", "REPO")
2323
http.Register(
24-
httpmock.GraphQL(`\bviewer\b`),
24+
httpmock.GraphQL(`query UserCurrent\b`),
2525
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
26-
27-
jsonFile, _ := os.Open("../test/fixtures/issueStatus.json")
28-
defer jsonFile.Close()
29-
http.StubResponse(200, jsonFile)
26+
http.Register(
27+
httpmock.GraphQL(`query IssueStatus\b`),
28+
httpmock.FileResponse("../test/fixtures/issueStatus.json"))
3029

3130
output, err := RunCommand("issue status")
3231
if err != nil {
@@ -53,17 +52,17 @@ func TestIssueStatus_blankSlate(t *testing.T) {
5352
http := initFakeHTTP()
5453
http.StubRepoResponse("OWNER", "REPO")
5554
http.Register(
56-
httpmock.GraphQL(`\bviewer\b`),
55+
httpmock.GraphQL(`query UserCurrent\b`),
5756
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
58-
59-
http.StubResponse(200, bytes.NewBufferString(`
60-
{ "data": { "repository": {
61-
"hasIssuesEnabled": true,
62-
"assigned": { "nodes": [] },
63-
"mentioned": { "nodes": [] },
64-
"authored": { "nodes": [] }
65-
} } }
66-
`))
57+
http.Register(
58+
httpmock.GraphQL(`query IssueStatus\b`),
59+
httpmock.StringResponse(`
60+
{ "data": { "repository": {
61+
"hasIssuesEnabled": true,
62+
"assigned": { "nodes": [] },
63+
"mentioned": { "nodes": [] },
64+
"authored": { "nodes": [] }
65+
} } }`))
6766

6867
output, err := RunCommand("issue status")
6968
if err != nil {
@@ -93,14 +92,14 @@ func TestIssueStatus_disabledIssues(t *testing.T) {
9392
http := initFakeHTTP()
9493
http.StubRepoResponse("OWNER", "REPO")
9594
http.Register(
96-
httpmock.GraphQL(`\bviewer\b`),
95+
httpmock.GraphQL(`query UserCurrent\b`),
9796
httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
98-
99-
http.StubResponse(200, bytes.NewBufferString(`
100-
{ "data": { "repository": {
101-
"hasIssuesEnabled": false
102-
} } }
103-
`))
97+
http.Register(
98+
httpmock.GraphQL(`query IssueStatus\b`),
99+
httpmock.StringResponse(`
100+
{ "data": { "repository": {
101+
"hasIssuesEnabled": false
102+
} } }`))
104103

105104
_, err := RunCommand("issue status")
106105
if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" {
@@ -112,10 +111,9 @@ func TestIssueList(t *testing.T) {
112111
initBlankContext("", "OWNER/REPO", "master")
113112
http := initFakeHTTP()
114113
http.StubRepoResponse("OWNER", "REPO")
115-
116-
jsonFile, _ := os.Open("../test/fixtures/issueList.json")
117-
defer jsonFile.Close()
118-
http.StubResponse(200, jsonFile)
114+
http.Register(
115+
httpmock.GraphQL(`query IssueList\b`),
116+
httpmock.FileResponse("../test/fixtures/issueList.json"))
119117

120118
output, err := RunCommand("issue list")
121119
if err != nil {
@@ -145,13 +143,20 @@ func TestIssueList_withFlags(t *testing.T) {
145143
initBlankContext("", "OWNER/REPO", "master")
146144
http := initFakeHTTP()
147145
http.StubRepoResponse("OWNER", "REPO")
148-
149-
http.StubResponse(200, bytes.NewBufferString(`
150-
{ "data": { "repository": {
151-
"hasIssuesEnabled": true,
152-
"issues": { "nodes": [] }
153-
} } }
154-
`))
146+
http.Register(
147+
httpmock.GraphQL(`query IssueList\b`),
148+
httpmock.GraphQLQuery(`
149+
{ "data": { "repository": {
150+
"hasIssuesEnabled": true,
151+
"issues": { "nodes": [] }
152+
} } }`, func(_ string, params map[string]interface{}) {
153+
assert.Equal(t, "probablyCher", params["assignee"].(string))
154+
assert.Equal(t, "foo", params["author"].(string))
155+
assert.Equal(t, "me", params["mention"].(string))
156+
assert.Equal(t, "1.x", params["milestone"].(string))
157+
assert.Equal(t, []interface{}{"web", "bug"}, params["labels"].([]interface{}))
158+
assert.Equal(t, []interface{}{"OPEN"}, params["states"].([]interface{}))
159+
}))
155160

156161
output, err := RunCommand("issue list -a probablyCher -l web,bug -s open -A foo --mention me --milestone 1.x")
157162
if err != nil {
@@ -163,26 +168,6 @@ func TestIssueList_withFlags(t *testing.T) {
163168
No issues match your search in OWNER/REPO
164169
165170
`)
166-
167-
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
168-
reqBody := struct {
169-
Variables struct {
170-
Assignee string
171-
Labels []string
172-
States []string
173-
Author string
174-
Mention string
175-
Milestone string
176-
}
177-
}{}
178-
_ = json.Unmarshal(bodyBytes, &reqBody)
179-
180-
eq(t, reqBody.Variables.Assignee, "probablyCher")
181-
eq(t, reqBody.Variables.Labels, []string{"web", "bug"})
182-
eq(t, reqBody.Variables.States, []string{"OPEN"})
183-
eq(t, reqBody.Variables.Author, "foo")
184-
eq(t, reqBody.Variables.Mention, "me")
185-
eq(t, reqBody.Variables.Milestone, "1.x")
186171
}
187172

188173
func TestIssueList_withInvalidLimitFlag(t *testing.T) {
@@ -371,10 +356,7 @@ func TestIssueView_Preview(t *testing.T) {
371356
initBlankContext("", "OWNER/REPO", tc.ownerRepo)
372357
http := initFakeHTTP()
373358
http.StubRepoResponse("OWNER", "REPO")
374-
375-
jsonFile, _ := os.Open(tc.fixture)
376-
defer jsonFile.Close()
377-
http.StubResponse(200, jsonFile)
359+
http.Register(httpmock.GraphQL(`query IssueByNumber\b`), httpmock.FileResponse(tc.fixture))
378360

379361
output, err := RunCommand(tc.command)
380362
if err != nil {
@@ -513,10 +495,10 @@ func TestIssueCreate_metadata(t *testing.T) {
513495
defer http.Verify(t)
514496

515497
http.Register(
516-
httpmock.GraphQL(`\bviewerPermission\b`),
498+
httpmock.GraphQL(`query RepositoryNetwork\b`),
517499
httpmock.StringResponse(httpmock.RepoNetworkStubResponse("OWNER", "REPO", "master", "WRITE")))
518500
http.Register(
519-
httpmock.GraphQL(`\bhasIssuesEnabled\b`),
501+
httpmock.GraphQL(`query RepositoryInfo\b`),
520502
httpmock.StringResponse(`
521503
{ "data": { "repository": {
522504
"id": "REPOID",
@@ -525,7 +507,7 @@ func TestIssueCreate_metadata(t *testing.T) {
525507
} } }
526508
`))
527509
http.Register(
528-
httpmock.GraphQL(`\bu000:`),
510+
httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`),
529511
httpmock.StringResponse(`
530512
{ "data": {
531513
"u000": { "login": "MonaLisa", "id": "MONAID" },
@@ -536,7 +518,7 @@ func TestIssueCreate_metadata(t *testing.T) {
536518
} }
537519
`))
538520
http.Register(
539-
httpmock.GraphQL(`\bmilestones\(`),
521+
httpmock.GraphQL(`query RepositoryMilestoneList\b`),
540522
httpmock.StringResponse(`
541523
{ "data": { "repository": { "milestones": {
542524
"nodes": [
@@ -547,7 +529,7 @@ func TestIssueCreate_metadata(t *testing.T) {
547529
} } } }
548530
`))
549531
http.Register(
550-
httpmock.GraphQL(`\brepository\(.+\bprojects\(`),
532+
httpmock.GraphQL(`query RepositoryProjectList\b`),
551533
httpmock.StringResponse(`
552534
{ "data": { "repository": { "projects": {
553535
"nodes": [
@@ -558,7 +540,7 @@ func TestIssueCreate_metadata(t *testing.T) {
558540
} } } }
559541
`))
560542
http.Register(
561-
httpmock.GraphQL(`\borganization\(.+\bprojects\(`),
543+
httpmock.GraphQL(`query OrganizationProjectList\b`),
562544
httpmock.StringResponse(`
563545
{ "data": { "organization": null },
564546
"errors": [{
@@ -569,7 +551,7 @@ func TestIssueCreate_metadata(t *testing.T) {
569551
}
570552
`))
571553
http.Register(
572-
httpmock.GraphQL(`\bcreateIssue\(`),
554+
httpmock.GraphQL(`mutation IssueCreate\b`),
573555
httpmock.GraphQLMutation(`
574556
{ "data": { "createIssue": { "issue": {
575557
"URL": "https://github.com/OWNER/REPO/issues/12"

command/pr_create_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ func TestPRCreate_metadata(t *testing.T) {
7272
defer http.Verify(t)
7373

7474
http.Register(
75-
httpmock.GraphQL(`\bviewerPermission\b`),
75+
httpmock.GraphQL(`query RepositoryNetwork\b`),
7676
httpmock.StringResponse(httpmock.RepoNetworkStubResponse("OWNER", "REPO", "master", "WRITE")))
7777
http.Register(
78-
httpmock.GraphQL(`\bforks\(`),
78+
httpmock.GraphQL(`query RepositoryFindFork\b`),
7979
httpmock.StringResponse(`
8080
{ "data": { "repository": { "forks": { "nodes": [
8181
] } } } }
8282
`))
8383
http.Register(
84-
httpmock.GraphQL(`\bpullRequests\(`),
84+
httpmock.GraphQL(`query PullRequestForBranch\b`),
8585
httpmock.StringResponse(`
8686
{ "data": { "repository": { "pullRequests": { "nodes": [
8787
] } } } }
8888
`))
8989
http.Register(
90-
httpmock.GraphQL(`\bteam\(`),
90+
httpmock.GraphQL(`query RepositoryResolveMetadataIDs\b`),
9191
httpmock.StringResponse(`
9292
{ "data": {
9393
"u000": { "login": "MonaLisa", "id": "MONAID" },
@@ -103,7 +103,7 @@ func TestPRCreate_metadata(t *testing.T) {
103103
} }
104104
`))
105105
http.Register(
106-
httpmock.GraphQL(`\bmilestones\(`),
106+
httpmock.GraphQL(`query RepositoryMilestoneList\b`),
107107
httpmock.StringResponse(`
108108
{ "data": { "repository": { "milestones": {
109109
"nodes": [
@@ -114,7 +114,7 @@ func TestPRCreate_metadata(t *testing.T) {
114114
} } } }
115115
`))
116116
http.Register(
117-
httpmock.GraphQL(`\brepository\(.+\bprojects\(`),
117+
httpmock.GraphQL(`query RepositoryProjectList\b`),
118118
httpmock.StringResponse(`
119119
{ "data": { "repository": { "projects": {
120120
"nodes": [
@@ -125,15 +125,15 @@ func TestPRCreate_metadata(t *testing.T) {
125125
} } } }
126126
`))
127127
http.Register(
128-
httpmock.GraphQL(`\borganization\(.+\bprojects\(`),
128+
httpmock.GraphQL(`query OrganizationProjectList\b`),
129129
httpmock.StringResponse(`
130130
{ "data": { "organization": { "projects": {
131131
"nodes": [],
132132
"pageInfo": { "hasNextPage": false }
133133
} } } }
134134
`))
135135
http.Register(
136-
httpmock.GraphQL(`\bcreatePullRequest\(`),
136+
httpmock.GraphQL(`mutation PullRequestCreate\b`),
137137
httpmock.GraphQLMutation(`
138138
{ "data": { "createPullRequest": { "pullRequest": {
139139
"id": "NEWPULLID",
@@ -150,7 +150,7 @@ func TestPRCreate_metadata(t *testing.T) {
150150
}
151151
}))
152152
http.Register(
153-
httpmock.GraphQL(`\bupdatePullRequest\(`),
153+
httpmock.GraphQL(`mutation PullRequestCreateMetadata\b`),
154154
httpmock.GraphQLMutation(`
155155
{ "data": { "updatePullRequest": {
156156
"clientMutationId": ""
@@ -163,7 +163,7 @@ func TestPRCreate_metadata(t *testing.T) {
163163
eq(t, inputs["milestoneId"], "BIGONEID")
164164
}))
165165
http.Register(
166-
httpmock.GraphQL(`\brequestReviews\(`),
166+
httpmock.GraphQL(`mutation PullRequestCreateRequestReviews\b`),
167167
httpmock.GraphQLMutation(`
168168
{ "data": { "requestReviews": {
169169
"clientMutationId": ""

0 commit comments

Comments
 (0)
X Tutup