(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func TestIsPullRequest(t *testing.T) { |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | repo ghrepo.Interface |
| 19 | response string |
| 20 | want bool |
| 21 | wantURL string |
| 22 | wantErr string |
| 23 | }{ |
| 24 | { |
| 25 | name: "an issue", |
| 26 | repo: ghrepo.New("monalisa", "monas-cafe"), |
| 27 | response: `{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"__typename":"Issue","number":142}}}}`, |
| 28 | want: false, |
| 29 | wantURL: "https://api.github.com/graphql", |
| 30 | }, |
| 31 | { |
| 32 | name: "a pull request", |
| 33 | repo: ghrepo.New("monalisa", "monas-cafe"), |
| 34 | response: `{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"__typename":"PullRequest","number":142}}}}`, |
| 35 | want: true, |
| 36 | wantURL: "https://api.github.com/graphql", |
| 37 | }, |
| 38 | { |
| 39 | name: "a repository on a ghe.com host", |
| 40 | repo: ghrepo.NewWithHost("monalisa", "monas-cafe", "monas-cafe.ghe.com"), |
| 41 | response: `{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"__typename":"Issue","number":142}}}}`, |
| 42 | want: false, |
| 43 | wantURL: "https://api.monas-cafe.ghe.com/graphql", |
| 44 | }, |
| 45 | { |
| 46 | name: "a number that is neither", |
| 47 | repo: ghrepo.New("monalisa", "monas-cafe"), |
| 48 | response: `{"data":{"repository":{"hasIssuesEnabled":true,"issue":null}},"errors":[{"type":"NOT_FOUND",` + |
| 49 | `"message":"Could not resolve to an issue or pull request with the number of 142.","path":["repository","issue"]}]}`, |
| 50 | wantURL: "https://api.github.com/graphql", |
| 51 | wantErr: "GraphQL: Could not resolve to an issue or pull request with the number of 142. (repository.issue)", |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | for _, tt := range tests { |
| 56 | t.Run(tt.name, func(t *testing.T) { |
| 57 | reg := &httpmock.Registry{} |
| 58 | defer reg.Verify(t) |
| 59 | reg.Register( |
| 60 | httpmock.GraphQL(`query IssueByNumber\b`), |
| 61 | httpmock.GraphQLQuery(tt.response, func(_ string, vars map[string]any) { |
| 62 | assert.Equal(t, "monalisa", vars["owner"]) |
| 63 | assert.Equal(t, "monas-cafe", vars["repo"]) |
| 64 | assert.Equal(t, float64(142), vars["number"]) |
| 65 | }), |
| 66 | ) |
| 67 | |
| 68 | c := NewArtifactClient(&http.Client{Transport: reg}) |
| 69 | got, err := c.IsPullRequest(tt.repo, 142) |
| 70 | |
| 71 | require.Len(t, reg.Requests, 1) |
| 72 | assert.Equal(t, tt.wantURL, reg.Requests[0].URL.String()) |
nothing calls this directly
no test coverage detected