Skip to content

Commit 3e061df

Browse files
committed
Add test for pickling schemas
Note that this is not yet supported
1 parent 4a63f97 commit 3e061df

File tree

3 files changed

+104
-79
lines changed

3 files changed

+104
-79
lines changed

tests/language/test_schema_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,18 +796,18 @@ def directive_with_incorrect_locations():
796796
def parses_kitchen_sink_schema(kitchen_sink_sdl): # noqa: F811
797797
assert parse(kitchen_sink_sdl)
798798

799-
def can_pickle_and_unpickle_kitchen_sink_schema(kitchen_sink_sdl): # noqa: F811
799+
def can_pickle_and_unpickle_kitchen_sink_schema_ast(kitchen_sink_sdl): # noqa: F811
800800
import pickle
801801

802-
# create a schema from the kitchen sink SDL
802+
# create a schema AST from the kitchen sink SDL
803803
doc = parse(kitchen_sink_sdl)
804-
# check that the schema can be pickled
804+
# check that the schema AST can be pickled
805805
# (particularly, there should be no recursion error)
806806
dumped = pickle.dumps(doc)
807807
# check that the pickle size is reasonable
808808
assert len(dumped) < 50 * len(kitchen_sink_sdl)
809809
loaded = pickle.loads(dumped)
810-
# check that the un-pickled schema is still the same
810+
# check that the un-pickled schema AST is still the same
811811
assert loaded == doc
812812
# check that pickling again creates the same result
813813
dumped_again = pickle.dumps(doc)

tests/utilities/test_build_ast_schema.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import namedtuple
22
from typing import Union
33

4-
from pytest import raises
4+
from pytest import mark, raises
55

66
from graphql import graphql_sync
77
from graphql.language import parse, print_ast, DocumentNode, InterfaceTypeDefinitionNode
@@ -34,6 +34,7 @@
3434
)
3535
from graphql.utilities import build_ast_schema, build_schema, print_schema, print_type
3636

37+
from ..fixtures import big_schema_sdl # noqa: F401
3738
from ..utils import dedent
3839

3940

@@ -42,7 +43,7 @@ def cycle_sdl(sdl: str) -> str:
4243
4344
This function does a full cycle of going from a string with the contents of the SDL,
4445
parsed in a schema AST, materializing that schema AST into an in-memory
45-
GraphQLSchema, and then finally printing that GraphQL into the SDÖ.
46+
GraphQLSchema, and then finally printing that GraphQL into the SDL.
4647
"""
4748
ast = parse(sdl)
4849
schema = build_ast_schema(ast)
@@ -1184,3 +1185,27 @@ def rejects_invalid_ast():
11841185
with raises(TypeError) as exc_info:
11851186
build_ast_schema({}) # type: ignore
11861187
assert str(exc_info.value) == "Must provide valid Document AST."
1188+
1189+
# This currently does not work because of how extend_schema is implemented
1190+
@mark.skip(reason="pickling of schemas is not yet supported")
1191+
def can_pickle_and_unpickle_big_schema(big_schema_sdl): # noqa: F811
1192+
import pickle
1193+
1194+
# create a schema from the kitchen sink SDL
1195+
schema = build_schema(big_schema_sdl, assume_valid_sdl=True)
1196+
# check that the schema can be pickled
1197+
# (particularly, there should be no recursion error,
1198+
# or errors because of trying to pickle lambdas or local functions)
1199+
dumped = pickle.dumps(schema)
1200+
# check that the pickle size is reasonable
1201+
assert len(dumped) < 50 * len(big_schema_sdl)
1202+
loaded = pickle.loads(dumped)
1203+
1204+
# check that the un-pickled schema is still the same
1205+
assert loaded == schema
1206+
# check that pickling again creates the same result
1207+
dumped_again = pickle.dumps(schema)
1208+
assert dumped_again == dumped
1209+
1210+
# check that printing the unpickled schema gives the same SDL
1211+
assert cycle_sdl(print_schema(schema)) == cycle_sdl(big_schema_sdl)

tests/utilities/test_extend_schema.py

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -970,29 +970,29 @@ def extends_different_types_multiple_times():
970970
extended_schema,
971971
dedent(
972972
"""
973-
scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec")
974-
975-
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
976-
oldField: String
977-
newField: String
978-
anotherNewField: String
979-
}
980-
981-
enum SomeEnum {
982-
OLD_VALUE
983-
NEW_VALUE
984-
ANOTHER_NEW_VALUE
985-
}
986-
987-
union SomeUnion = SomeObject | NewObject | AnotherNewObject
988-
989-
input SomeInput {
990-
oldField: String
991-
newField: String
992-
anotherNewField: String
993-
}
994-
995-
""" # noqa: E501
973+
scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec")
974+
975+
type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
976+
oldField: String
977+
newField: String
978+
anotherNewField: String
979+
}
980+
981+
enum SomeEnum {
982+
OLD_VALUE
983+
NEW_VALUE
984+
ANOTHER_NEW_VALUE
985+
}
986+
987+
union SomeUnion = SomeObject | NewObject | AnotherNewObject
988+
989+
input SomeInput {
990+
oldField: String
991+
newField: String
992+
anotherNewField: String
993+
}
994+
995+
""" # noqa: E501
996996
)
997997
+ "\n\n"
998998
+ new_types_sdl,
@@ -1041,21 +1041,21 @@ def extends_interfaces_by_adding_new_fields():
10411041
extended_schema,
10421042
dedent(
10431043
"""
1044-
interface SomeInterface {
1045-
oldField: String
1046-
newField: String
1047-
}
1048-
1049-
interface AnotherInterface implements SomeInterface {
1050-
oldField: String
1051-
newField: String
1052-
}
1053-
1054-
type SomeObject implements SomeInterface & AnotherInterface {
1055-
oldField: String
1056-
newField: String
1057-
}
1058-
"""
1044+
interface SomeInterface {
1045+
oldField: String
1046+
newField: String
1047+
}
1048+
1049+
interface AnotherInterface implements SomeInterface {
1050+
oldField: String
1051+
newField: String
1052+
}
1053+
1054+
type SomeObject implements SomeInterface & AnotherInterface {
1055+
oldField: String
1056+
newField: String
1057+
}
1058+
"""
10591059
),
10601060
)
10611061

@@ -1102,20 +1102,20 @@ def extends_interfaces_by_adding_new_implemented_interfaces():
11021102
extended_schema,
11031103
dedent(
11041104
"""
1105-
interface AnotherInterface implements SomeInterface & NewInterface {
1106-
oldField: String
1107-
newField: String
1108-
}
1109-
1110-
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1111-
oldField: String
1112-
newField: String
1113-
}
1114-
1115-
interface NewInterface {
1116-
newField: String
1117-
}
1118-
"""
1105+
interface AnotherInterface implements SomeInterface & NewInterface {
1106+
oldField: String
1107+
newField: String
1108+
}
1109+
1110+
type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1111+
oldField: String
1112+
newField: String
1113+
}
1114+
1115+
interface NewInterface {
1116+
newField: String
1117+
}
1118+
"""
11191119
),
11201120
)
11211121

@@ -1150,11 +1150,11 @@ def allows_extension_of_interface_with_missing_object_fields():
11501150
extended_schema,
11511151
dedent(
11521152
"""
1153-
interface SomeInterface {
1154-
oldField: SomeInterface
1155-
newField: String
1156-
}
1157-
"""
1153+
interface SomeInterface {
1154+
oldField: SomeInterface
1155+
newField: String
1156+
}
1157+
"""
11581158
),
11591159
)
11601160

@@ -1189,12 +1189,12 @@ def extends_interfaces_multiple_times():
11891189
extended_schema,
11901190
dedent(
11911191
"""
1192-
interface SomeInterface {
1193-
some: SomeInterface
1194-
newFieldA: Int
1195-
newFieldB(test: Boolean): String
1196-
}
1197-
"""
1192+
interface SomeInterface {
1193+
some: SomeInterface
1194+
newFieldA: Int
1195+
newFieldB(test: Boolean): String
1196+
}
1197+
"""
11981198
),
11991199
)
12001200

@@ -1491,15 +1491,15 @@ def schema_extension_ast_are_available_from_schema_object():
14911491
extended_twice_schema,
14921492
dedent(
14931493
"""
1494-
extend schema {
1495-
mutation: Mutation
1496-
}
1497-
1498-
extend schema {
1499-
subscription: Subscription
1500-
}
1501-
1502-
extend schema @foo
1503-
"""
1494+
extend schema {
1495+
mutation: Mutation
1496+
}
1497+
1498+
extend schema {
1499+
subscription: Subscription
1500+
}
1501+
1502+
extend schema @foo
1503+
"""
15041504
),
15051505
)

0 commit comments

Comments
 (0)