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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
package jsonutil_test
import (
"reflect"
"testing"
"time"
"github.com/shurcooL/graphql"
"github.com/shurcooL/graphql/internal/jsonutil"
)
func TestUnmarshalGraphQL(t *testing.T) {
/*
query {
me {
name
height
}
}
*/
type query struct {
Me struct {
Name graphql.String
Height graphql.Float
}
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"me": {
"name": "Luke Skywalker",
"height": 1.72
}
}`), &got)
if err != nil {
t.Fatal(err)
}
var want query
want.Me.Name = "Luke Skywalker"
want.Me.Height = 1.72
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_graphqlTag(t *testing.T) {
type query struct {
Foo graphql.String `graphql:"baz"`
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"baz": "bar"
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: "bar",
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_jsonTag(t *testing.T) {
type query struct {
Foo graphql.String `json:"baz"`
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"foo": "bar"
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: "bar",
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_array(t *testing.T) {
type query struct {
Foo []graphql.String
Bar []graphql.String
Baz []graphql.String
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"foo": [
"bar",
"baz"
],
"bar": [],
"baz": null
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: []graphql.String{"bar", "baz"},
Bar: []graphql.String{},
Baz: []graphql.String(nil),
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
// When unmarshaling into an array, its initial value should be overwritten
// (rather than appended to).
func TestUnmarshalGraphQL_arrayReset(t *testing.T) {
var got = []string{"initial"}
err := jsonutil.UnmarshalGraphQL([]byte(`["bar", "baz"]`), &got)
if err != nil {
t.Fatal(err)
}
want := []string{"bar", "baz"}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_objectArray(t *testing.T) {
type query struct {
Foo []struct {
Name graphql.String
}
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"foo": [
{"name": "bar"},
{"name": "baz"}
]
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: []struct{ Name graphql.String }{
{"bar"},
{"baz"},
},
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_pointer(t *testing.T) {
type query struct {
Foo *graphql.String
Bar *graphql.String
}
var got query
got.Bar = new(graphql.String) // Test that got.Bar gets set to nil.
err := jsonutil.UnmarshalGraphQL([]byte(`{
"foo": "foo",
"bar": null
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: graphql.NewString("foo"),
Bar: nil,
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_objectPointerArray(t *testing.T) {
type query struct {
Foo []*struct {
Name graphql.String
}
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"foo": [
{"name": "bar"},
null,
{"name": "baz"}
]
}`), &got)
if err != nil {
t.Fatal(err)
}
want := query{
Foo: []*struct{ Name graphql.String }{
{"bar"},
nil,
{"baz"},
},
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_pointerWithInlineFragment(t *testing.T) {
type actor struct {
User struct {
DatabaseID uint64
} `graphql:"... on User"`
Login string
}
type query struct {
Author actor
Editor *actor
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"author": {
"databaseId": 1,
"login": "test1"
},
"editor": {
"databaseId": 2,
"login": "test2"
}
}`), &got)
if err != nil {
t.Fatal(err)
}
var want query
want.Author = actor{
User: struct{ DatabaseID uint64 }{1},
Login: "test1",
}
want.Editor = &actor{
User: struct{ DatabaseID uint64 }{2},
Login: "test2",
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_unexportedField(t *testing.T) {
type query struct {
foo graphql.String
}
err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}`), new(query))
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := err.Error(), "struct field for \"foo\" doesn't exist in any of 1 places to unmarshal"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}
}
func TestUnmarshalGraphQL_multipleValues(t *testing.T) {
type query struct {
Foo graphql.String
}
err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}{"foo": "baz"}`), new(query))
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := err.Error(), "invalid token '{' after top-level value"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}
}
func TestUnmarshalGraphQL_directives(t *testing.T) {
/*
query {
me {
name @include(if: true)
height @skip(if: false)
}
}
*/
type query struct {
Me struct {
Name graphql.String `graphql:"name @include(if: true)"`
Height graphql.Float `graphql:"height @skip(if: false)"`
}
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"me": {
"name": "Luke Skywalker",
"height": 1.72
}
}`), &got)
if err != nil {
t.Fatal(err)
}
var want query
want.Me.Name = "Luke Skywalker"
want.Me.Height = 1.72
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
func TestUnmarshalGraphQL_union(t *testing.T) {
/*
{
__typename
... on ClosedEvent {
createdAt
actor {login}
}
... on ReopenedEvent {
createdAt
actor {login}
}
}
*/
type actor struct{ Login graphql.String }
type closedEvent struct {
Actor actor
CreatedAt time.Time
}
type reopenedEvent struct {
Actor actor
CreatedAt time.Time
}
type issueTimelineItem struct {
Typename string `graphql:"__typename"`
ClosedEvent closedEvent `graphql:"... on ClosedEvent"`
ReopenedEvent reopenedEvent `graphql:"... on ReopenedEvent"`
}
var got issueTimelineItem
err := jsonutil.UnmarshalGraphQL([]byte(`{
"__typename": "ClosedEvent",
"createdAt": "2017-06-29T04:12:01Z",
"actor": {
"login": "shurcooL-test"
}
}`), &got)
if err != nil {
t.Fatal(err)
}
want := issueTimelineItem{
Typename: "ClosedEvent",
ClosedEvent: closedEvent{
Actor: actor{
Login: "shurcooL-test",
},
CreatedAt: time.Unix(1498709521, 0).UTC(),
},
ReopenedEvent: reopenedEvent{
Actor: actor{
Login: "shurcooL-test",
},
CreatedAt: time.Unix(1498709521, 0).UTC(),
},
}
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
// Issue https://github.com/shurcooL/githubv4/issues/18.
func TestUnmarshalGraphQL_arrayInsideInlineFragment(t *testing.T) {
/*
query {
search(type: ISSUE, first: 1, query: "type:pr repo:owner/name") {
nodes {
... on PullRequest {
commits(last: 1) {
nodes {
url
}
}
}
}
}
}
*/
type query struct {
Search struct {
Nodes []struct {
PullRequest struct {
Commits struct {
Nodes []struct {
URL string `graphql:"url"`
}
} `graphql:"commits(last: 1)"`
} `graphql:"... on PullRequest"`
}
} `graphql:"search(type: ISSUE, first: 1, query: \"type:pr repo:owner/name\")"`
}
var got query
err := jsonutil.UnmarshalGraphQL([]byte(`{
"search": {
"nodes": [
{
"commits": {
"nodes": [
{
"url": "https://example.org/commit/49e1"
}
]
}
}
]
}
}`), &got)
if err != nil {
t.Fatal(err)
}
var want query
want.Search.Nodes = make([]struct {
PullRequest struct {
Commits struct {
Nodes []struct {
URL string `graphql:"url"`
}
} `graphql:"commits(last: 1)"`
} `graphql:"... on PullRequest"`
}, 1)
want.Search.Nodes[0].PullRequest.Commits.Nodes = make([]struct {
URL string `graphql:"url"`
}, 1)
want.Search.Nodes[0].PullRequest.Commits.Nodes[0].URL = "https://example.org/commit/49e1"
if !reflect.DeepEqual(got, want) {
t.Error("not equal")
}
}
|