@@ -2,6 +2,7 @@ package api
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"net/http"
6
7
"testing"
7
8
"time"
@@ -32,7 +33,7 @@ func TestGQLClient(t *testing.T) {
32
33
assert .Equal (t , "hubot" , res .Viewer .Login )
33
34
}
34
35
35
- func TestGQLClientError (t * testing.T ) {
36
+ func TestGQLClientDoError (t * testing.T ) {
36
37
stubConfig (t , testConfig ())
37
38
t .Cleanup (gock .Off )
38
39
@@ -48,7 +49,56 @@ func TestGQLClientError(t *testing.T) {
48
49
49
50
res := struct { Organization struct { Name string } }{}
50
51
err = client .Do ("QUERY" , nil , & res )
51
- assert .EqualError (t , err , "GraphQL: Could not resolve to an Organization with the login of 'cli'. (organization)" )
52
+ var gqlErr * GQLError
53
+ assert .True (t , errors .As (err , & gqlErr ))
54
+ assert .EqualError (t , gqlErr , "GraphQL: Could not resolve to an Organization with the login of 'cli'. (organization)" )
55
+ assert .True (t , gock .IsDone (), printPendingMocks (gock .Pending ()))
56
+ }
57
+
58
+ func TestGQLClientQueryError (t * testing.T ) {
59
+ stubConfig (t , testConfig ())
60
+ t .Cleanup (gock .Off )
61
+
62
+ gock .New ("https://api.github.com" ).
63
+ Post ("/graphql" ).
64
+ MatchHeader ("Authorization" , "token abc123" ).
65
+ BodyString (`{"query":"query QUERY{organization{name}}"}` ).
66
+ Reply (200 ).
67
+ JSON (`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}` )
68
+
69
+ client , err := DefaultGQLClient ()
70
+ assert .NoError (t , err )
71
+
72
+ var res struct { Organization struct { Name string } }
73
+ err = client .Query ("QUERY" , & res , nil )
74
+ var gqlErr * GQLError
75
+ assert .True (t , errors .As (err , & gqlErr ))
76
+ assert .EqualError (t , gqlErr , "GraphQL: Could not resolve to an Organization with the login of 'cli'. (organization)" )
77
+ assert .True (t , gock .IsDone (), printPendingMocks (gock .Pending ()))
78
+ }
79
+
80
+ func TestGQLClientMutateError (t * testing.T ) {
81
+ stubConfig (t , testConfig ())
82
+ t .Cleanup (gock .Off )
83
+
84
+ gock .New ("https://api.github.com" ).
85
+ Post ("/graphql" ).
86
+ MatchHeader ("Authorization" , "token abc123" ).
87
+ BodyString (`{"query":"mutation MUTATE($input:ID!){updateRepository{repository{name}}}","variables":{"input":"variables"}}` ).
88
+ Reply (200 ).
89
+ JSON (`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}` )
90
+
91
+ client , err := DefaultGQLClient ()
92
+ assert .NoError (t , err )
93
+
94
+ var mutation struct {
95
+ UpdateRepository struct { Repository struct { Name string } }
96
+ }
97
+ variables := map [string ]interface {}{"input" : "variables" }
98
+ err = client .Mutate ("MUTATE" , & mutation , variables )
99
+ var gqlErr * GQLError
100
+ assert .True (t , errors .As (err , & gqlErr ))
101
+ assert .EqualError (t , gqlErr , "GraphQL: Could not resolve to an Organization with the login of 'cli'. (organization)" )
52
102
assert .True (t , gock .IsDone (), printPendingMocks (gock .Pending ()))
53
103
}
54
104
@@ -192,7 +242,6 @@ func TestGQLClientDoWithContext(t *testing.T) {
192
242
193
243
for _ , tt := range tests {
194
244
t .Run (tt .name , func (t * testing.T ) {
195
- // given
196
245
t .Cleanup (gock .Off )
197
246
gock .New ("https://api.github.com" ).
198
247
Post ("/graphql" ).
@@ -209,11 +258,9 @@ func TestGQLClientDoWithContext(t *testing.T) {
209
258
vars := map [string ]interface {}{"var" : "test" }
210
259
res := struct { Viewer struct { Login string } }{}
211
260
212
- // when
213
261
ctx := tt .getCtx ()
214
262
gotErr := client .DoWithContext (ctx , "QUERY" , vars , & res )
215
263
216
- // then
217
264
assert .True (t , gock .IsDone (), printPendingMocks (gock .Pending ()))
218
265
assert .EqualError (t , gotErr , tt .wantErrMsg )
219
266
})
0 commit comments