@@ -101,6 +101,76 @@ def _parse_literal_raises(s, message):
101
101
"$var" , "Int cannot represent non-integer value: $var"
102
102
)
103
103
104
+ def serializes ():
105
+ serialize = GraphQLInt .serialize
106
+
107
+ assert serialize (1 ) == 1
108
+ assert serialize ("123" ) == 123
109
+ assert serialize (0 ) == 0
110
+ assert serialize (- 1 ) == - 1
111
+ assert serialize (1e5 ) == 100000
112
+ assert serialize (False ) == 0
113
+ assert serialize (True ) == 1
114
+ assert serialize (type ("Int" , (int ,), {})(5 )) == 5
115
+
116
+ # The GraphQL specification does not allow serializing non-integer
117
+ # values as Int to avoid accidental data loss.
118
+ with raises (GraphQLError ) as exc_info :
119
+ serialize (0.1 )
120
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: 0.1"
121
+ with raises (GraphQLError ) as exc_info :
122
+ serialize (1.1 )
123
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: 1.1"
124
+ with raises (GraphQLError ) as exc_info :
125
+ serialize (- 1.1 )
126
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: -1.1"
127
+ with raises (GraphQLError ) as exc_info :
128
+ serialize ("-1.1" )
129
+ assert (
130
+ str (exc_info .value ) == "Int cannot represent non-integer value: '-1.1'"
131
+ )
132
+ # Maybe a safe JavaScript int, but bigger than 2^32, so not
133
+ # representable as a GraphQL Int
134
+ with raises (GraphQLError ) as exc_info :
135
+ serialize (9876504321 )
136
+ assert str (exc_info .value ) == (
137
+ "Int cannot represent non 32-bit signed integer value: 9876504321"
138
+ )
139
+ with raises (GraphQLError ) as exc_info :
140
+ serialize (- 9876504321 )
141
+ assert str (exc_info .value ) == (
142
+ "Int cannot represent non 32-bit signed integer value: -9876504321"
143
+ )
144
+ # Too big to represent as an Int in JavaScript or GraphQL
145
+ with raises (GraphQLError ) as exc_info :
146
+ serialize (1e100 )
147
+ assert str (exc_info .value ) == (
148
+ "Int cannot represent non 32-bit signed integer value: 1e+100"
149
+ )
150
+ with raises (GraphQLError ) as exc_info :
151
+ serialize (- 1e100 )
152
+ assert str (exc_info .value ) == (
153
+ "Int cannot represent non 32-bit signed integer value: -1e+100"
154
+ )
155
+ with raises (GraphQLError ) as exc_info :
156
+ serialize ("one" )
157
+ assert (
158
+ str (exc_info .value ) == "Int cannot represent non-integer value: 'one'"
159
+ )
160
+ # Doesn't represent number
161
+ with raises (GraphQLError ) as exc_info :
162
+ serialize ("" )
163
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: ''"
164
+ with raises (GraphQLError ) as exc_info :
165
+ serialize (nan )
166
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: nan"
167
+ with raises (GraphQLError ) as exc_info :
168
+ serialize (inf )
169
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: inf"
170
+ with raises (GraphQLError ) as exc_info :
171
+ serialize ([5 ])
172
+ assert str (exc_info .value ) == "Int cannot represent non-integer value: [5]"
173
+
104
174
def describe_graphql_float ():
105
175
def parse_value ():
106
176
_parse_value = GraphQLFloat .parse_value
@@ -184,6 +254,45 @@ def _parse_literal_raises(s, message):
184
254
"$var" , "Float cannot represent non numeric value: $var"
185
255
)
186
256
257
+ def serializes ():
258
+ serialize = GraphQLFloat .serialize
259
+
260
+ assert serialize (1 ) == 1.0
261
+ assert serialize (0 ) == 0.0
262
+ assert serialize ("123.5" ) == 123.5
263
+ assert serialize (- 1 ) == - 1.0
264
+ assert serialize (0.1 ) == 0.1
265
+ assert serialize (1.1 ) == 1.1
266
+ assert serialize (- 1.1 ) == - 1.1
267
+ assert serialize ("-1.1" ) == - 1.1
268
+ assert serialize (False ) == 0
269
+ assert serialize (True ) == 1
270
+ assert serialize (type ("Float" , (float ,), {})(5.5 )) == 5.5
271
+
272
+ with raises (GraphQLError ) as exc_info :
273
+ serialize (nan )
274
+ assert (
275
+ str (exc_info .value ) == "Float cannot represent non numeric value: nan"
276
+ )
277
+ with raises (GraphQLError ) as exc_info :
278
+ serialize (inf )
279
+ assert (
280
+ str (exc_info .value ) == "Float cannot represent non numeric value: inf"
281
+ )
282
+ with raises (GraphQLError ) as exc_info :
283
+ serialize ("one" )
284
+ assert str (exc_info .value ) == (
285
+ "Float cannot represent non numeric value: 'one'"
286
+ )
287
+ with raises (GraphQLError ) as exc_info :
288
+ serialize ("" )
289
+ assert str (exc_info .value ) == "Float cannot represent non numeric value: ''"
290
+ with raises (GraphQLError ) as exc_info :
291
+ serialize ([5 ])
292
+ assert (
293
+ str (exc_info .value ) == "Float cannot represent non numeric value: [5]"
294
+ )
295
+
187
296
def describe_graphql_string ():
188
297
def parse_value ():
189
298
_parse_value = GraphQLString .parse_value
@@ -256,6 +365,40 @@ def _parse_literal_raises(s, message):
256
365
"$var" , "String cannot represent a non string value: $var"
257
366
)
258
367
368
+ def serializes ():
369
+ serialize = GraphQLString .serialize
370
+
371
+ assert serialize ("string" ) == "string"
372
+ assert serialize (1 ) == "1"
373
+ assert serialize (- 1.1 ) == "-1.1"
374
+ assert serialize (True ) == "true"
375
+ assert serialize (False ) == "false"
376
+
377
+ class StringableObjValue :
378
+ def __str__ (self ):
379
+ return "something useful"
380
+
381
+ assert serialize (StringableObjValue ()) == "something useful"
382
+
383
+ with raises (GraphQLError ) as exc_info :
384
+ serialize (nan )
385
+ assert str (exc_info .value ) == "String cannot represent value: nan"
386
+
387
+ with raises (GraphQLError ) as exc_info :
388
+ serialize ([1 ])
389
+ assert str (exc_info .value ) == "String cannot represent value: [1]"
390
+
391
+ with raises (GraphQLError ) as exc_info :
392
+ serialize ({})
393
+ assert str (exc_info .value ) == "String cannot represent value: {}"
394
+
395
+ with raises (GraphQLError ) as exc_info :
396
+ serialize ({"value_of" : "value_of string" })
397
+ assert (
398
+ str (exc_info .value ) == "String cannot represent value:"
399
+ " {'value_of': 'value_of string'}"
400
+ )
401
+
259
402
def describe_graphql_boolean ():
260
403
def parse_value ():
261
404
_parse_value = GraphQLBoolean .parse_value
@@ -357,6 +500,47 @@ def _parse_literal_raises(s, message):
357
500
"$var" , "Boolean cannot represent a non boolean value: $var"
358
501
)
359
502
503
+ def serializes ():
504
+ serialize = GraphQLBoolean .serialize
505
+
506
+ assert serialize (1 ) is True
507
+ assert serialize (0 ) is False
508
+ assert serialize (True ) is True
509
+ assert serialize (False ) is False
510
+ with raises (TypeError , match = "not an acceptable base type" ):
511
+ # you can't subclass bool in Python
512
+ assert serialize (type ("Boolean" , (bool ,), {})(True )) is True
513
+
514
+ with raises (GraphQLError ) as exc_info :
515
+ serialize (nan )
516
+ assert str (exc_info .value ) == (
517
+ "Boolean cannot represent a non boolean value: nan"
518
+ )
519
+
520
+ with raises (GraphQLError ) as exc_info :
521
+ serialize ("" )
522
+ assert str (exc_info .value ) == (
523
+ "Boolean cannot represent a non boolean value: ''"
524
+ )
525
+
526
+ with raises (GraphQLError ) as exc_info :
527
+ serialize ("True" )
528
+ assert str (exc_info .value ) == (
529
+ "Boolean cannot represent a non boolean value: 'True'"
530
+ )
531
+
532
+ with raises (GraphQLError ) as exc_info :
533
+ serialize ([False ])
534
+ assert str (exc_info .value ) == (
535
+ "Boolean cannot represent a non boolean value: [False]"
536
+ )
537
+
538
+ with raises (GraphQLError ) as exc_info :
539
+ serialize ({})
540
+ assert str (exc_info .value ) == (
541
+ "Boolean cannot represent a non boolean value: {}"
542
+ )
543
+
360
544
def describe_graphql_id ():
361
545
def parse_value ():
362
546
_parse_value = GraphQLID .parse_value
@@ -439,3 +623,39 @@ def _parse_literal_raises(s, message):
439
623
_parse_literal_raises (
440
624
"$var" , "ID cannot represent a non-string and non-integer value: $var"
441
625
)
626
+
627
+ def serializes ():
628
+ serialize = GraphQLID .serialize
629
+
630
+ assert serialize ("string" ) == "string"
631
+ assert serialize ("false" ) == "false"
632
+ assert serialize ("" ) == ""
633
+ assert serialize (123 ) == "123"
634
+ assert serialize (0 ) == "0"
635
+ assert serialize (- 1 ) == "-1"
636
+
637
+ class ObjValue :
638
+ def __init__ (self , value ):
639
+ self ._id = value
640
+
641
+ def __str__ (self ):
642
+ return str (self ._id )
643
+
644
+ obj_value = ObjValue (123 )
645
+ assert serialize (obj_value ) == "123"
646
+
647
+ with raises (GraphQLError ) as exc_info :
648
+ serialize (True )
649
+ assert str (exc_info .value ) == "ID cannot represent value: True"
650
+
651
+ with raises (GraphQLError ) as exc_info :
652
+ serialize (3.14 )
653
+ assert str (exc_info .value ) == "ID cannot represent value: 3.14"
654
+
655
+ with raises (GraphQLError ) as exc_info :
656
+ serialize ({})
657
+ assert str (exc_info .value ) == "ID cannot represent value: {}"
658
+
659
+ with raises (GraphQLError ) as exc_info :
660
+ serialize (["abc" ])
661
+ assert str (exc_info .value ) == "ID cannot represent value: ['abc']"
0 commit comments