-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoql_test.go
246 lines (196 loc) · 7.13 KB
/
goql_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package goql
import (
"context"
"errors"
"net/http"
"reflect"
"testing"
"time"
"github.com/getoutreach/goql/graphql_test"
)
// TestNewClient tests the NewClient function.
func TestNewClient(t *testing.T) {
// This value is needed in one of the table tests and needs to be replicated
// within the same table test so it needs to be abstracted out into a variable.
specificError := errors.New("this is a specific error")
tt := []struct {
Name string
URL string
Options ClientOptions
SpecificErrorFromMapper error
}{
{
Name: "GoodDefaultOptions",
URL: "http://localhost:3403/graphql",
Options: DefaultClientOptions,
SpecificErrorFromMapper: Errors{
{
Message: "foo",
},
{
Message: "bar",
},
},
},
{
Name: "GoodCustomOptions",
URL: "http://localhost:3403/graphql",
Options: ClientOptions{
HTTPClient: &http.Client{
Timeout: time.Duration(465) * time.Second,
},
ErrorMapper: func(_ int, _ Errors) error {
return specificError
},
},
SpecificErrorFromMapper: specificError,
},
}
for _, test := range tt {
test := test
fn := func(t *testing.T) {
t.Parallel()
client := NewClient(test.URL, test.Options)
if e, a := test.URL, client.url; e != a {
t.Errorf("expected url to be \"%s\", got \"%s\"", e, a)
}
// Default options were provided, so the HTTP client should be http.DefaultClient
// and the errorMapper should return whatever is passed in the second argument to
// the ErrorMapper type.
if reflect.DeepEqual(test.Options, DefaultClientOptions) {
if e, a := http.DefaultClient.Timeout, client.httpClient.Timeout; e != a {
t.Errorf("expected client's http client timeout to be \"%s\", got \"%s\"", e.String(), a.String())
}
if e, a := test.SpecificErrorFromMapper.Error(), client.errorMapper(http.StatusBadRequest,
test.SpecificErrorFromMapper.(Errors)).Error(); e != a { //nolint:errorlint // Why: test code
t.Errorf("expected error returned from error mapper to be \"%s\", got \"%s\"", e, a)
}
return
}
// Else, custom ClientOptions were set on the Client.
if e, a := test.Options.HTTPClient.Timeout, client.httpClient.Timeout; e != a {
t.Errorf("expected client's http client timeout to be \"%s\", got \"%s\"", e.String(), a.String())
}
if e, a := test.SpecificErrorFromMapper.Error(), client.errorMapper(http.StatusBadRequest, Errors{}).Error(); e != a {
t.Errorf("expected error returned from error mapper to be \"%s\", got \"%s\"", e, a)
}
}
t.Run(test.Name, fn)
}
}
// TestQueryWithHeaders tests the QueryWithHeaders pointer receiver function on the Client
// type. Since this is mostly a pass-through function to *Client.doStruct, this test is
// intentionally kept simple.
func TestQueryWithHeaders(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var GetEntity graphql_test.GetEntity
operation := Operation{
OperationType: &GetEntity,
Fields: nil,
Variables: GetEntity.Variables(),
}
headers := http.Header{}
if err := client.QueryWithHeaders(context.Background(), &operation, headers); err != nil {
t.Fatalf("error running query with headers: %v", err)
}
ts.DiffResponse(GetEntity.ExpectedResponse(), GetEntity)
}
// TestQuery tests the Query pointer receiver function on the Client type. Since this is
// mostly a pass-through function to *Client.doStruct, this test is intentionally kept
// simple.
func TestQuery(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var GetEntity graphql_test.GetEntity
operation := Operation{
OperationType: &GetEntity,
Fields: nil,
Variables: GetEntity.Variables(),
}
if err := client.Query(context.Background(), &operation); err != nil {
t.Fatalf("error running query: %v", err)
}
ts.DiffResponse(GetEntity.ExpectedResponse(), GetEntity)
}
// TestMutateWithHeaders tests the MutateWithHeaders pointer receiver function on the Client
// type. Since this is mostly a pass-through function to *Client.doStruct, this test is
// intentionally kept simple.
func TestMutateWithHeaders(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var CreateEntity graphql_test.CreateEntity
operation := Operation{
OperationType: &CreateEntity,
Fields: nil,
Variables: CreateEntity.Variables(),
}
headers := http.Header{}
if err := client.MutateWithHeaders(context.Background(), &operation, headers); err != nil {
t.Fatalf("error running mutate with headers: %v", err)
}
ts.DiffResponse(CreateEntity.ExpectedResponse(), CreateEntity)
}
// TestMutate tests the Mutate pointer receiver function on the Client type. Since this is
// mostly a pass-through function to *Client.doStruct, this test is intentionally kept
// simple.
func TestMutate(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var CreateEntity graphql_test.CreateEntity
operation := Operation{
OperationType: &CreateEntity,
Fields: nil,
Variables: CreateEntity.Variables(),
}
if err := client.Mutate(context.Background(), &operation); err != nil {
t.Fatalf("error running mutate: %v", err)
}
ts.DiffResponse(CreateEntity.ExpectedResponse(), CreateEntity)
}
// TestCustomOperationWithHeaders tests the CustomOperationWithHeaders pointer receiver
// function on the Client type. Since this is mostly a pass-through function to
// *Client.doCustom, this test is intentionally kept simple.
func TestCustomOperationWithHeaders(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var testOperation graphql_test.GetEntity
testQuery, err := MarshalQuery(testOperation, nil)
if err != nil {
t.Fatalf("error marshaling test query: %v", err)
}
headers := http.Header{}
if err := client.CustomOperationWithHeaders(context.Background(), testQuery, testOperation.Variables(),
&testOperation, headers); err != nil {
t.Fatalf("error running custom operation with headers: %v", err)
}
ts.DiffResponse(testOperation.ExpectedResponse(), testOperation)
}
// TestCustomOperation tests the CustomOperation pointer receiver function on the Client
// type. Since this is mostly a pass-through function to *Client.doCustom, this test is
// intentionally kept simple.
func TestCustomOperation(t *testing.T) {
t.Parallel()
ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)
client := NewClient(ts.URL, DefaultClientOptions)
var testOperation graphql_test.GetEntity
testQuery, err := MarshalQuery(testOperation, nil)
if err != nil {
t.Fatalf("error marshaling test query: %v", err)
}
if err := client.CustomOperation(context.Background(), testQuery, testOperation.Variables(), &testOperation); err != nil {
t.Fatalf("error running custom operation: %v", err)
}
ts.DiffResponse(testOperation.ExpectedResponse(), testOperation)
}