Skip to content

Commit 3ac30aa

Browse files
committed
tests: replace 'print_node' with more high-level 'print_ast_node'
Replicates graphql/graphql-js@172b141
1 parent 6ddb125 commit 3ac30aa

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

tests/utilities/test_build_ast_schema.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
from collections import namedtuple
2+
from typing import Union
23

34
from pytest import raises
45

56
from graphql import graphql_sync
6-
from graphql.language import parse, print_ast, DocumentNode, Node
7+
from graphql.language import parse, print_ast, DocumentNode
78
from graphql.type import (
89
GraphQLDeprecatedDirective,
910
GraphQLIncludeDirective,
1011
GraphQLSkipDirective,
12+
GraphQLBoolean,
13+
GraphQLFloat,
1114
GraphQLID,
1215
GraphQLInt,
13-
GraphQLFloat,
1416
GraphQLString,
15-
GraphQLBoolean,
17+
GraphQLArgument,
18+
GraphQLEnumValue,
19+
GraphQLField,
20+
GraphQLInputField,
21+
GraphQLNamedType,
1622
assert_directive,
1723
assert_enum_type,
1824
assert_input_object_type,
@@ -38,9 +44,14 @@ def cycle_sdl(sdl: str) -> str:
3844
return print_schema(schema)
3945

4046

41-
def print_node(node: Node) -> str:
42-
assert node
43-
return print_ast(node)
47+
TypeWithAstNode = Union[
48+
GraphQLArgument, GraphQLEnumValue, GraphQLField, GraphQLInputField, GraphQLNamedType
49+
]
50+
51+
52+
def print_ast_node(obj: TypeWithAstNode) -> str:
53+
assert obj is not None and obj.ast_node is not None
54+
return print_ast(obj.ast_node)
4455

4556

4657
def describe_schema_builder():
@@ -807,20 +818,20 @@ def correctly_assign_ast_nodes():
807818
assert restored_schema_ast == ast
808819

809820
test_field = query.fields["testField"]
810-
assert print_node(test_field.ast_node) == (
821+
assert print_ast_node(test_field) == (
811822
"testField(testArg: TestInput): TestUnion"
812823
)
813-
assert print_node(test_field.args["testArg"].ast_node) == "testArg: TestInput"
814-
assert print_node(test_input.fields["testInputField"].ast_node) == (
824+
assert print_ast_node(test_field.args["testArg"]) == "testArg: TestInput"
825+
assert print_ast_node(test_input.fields["testInputField"]) == (
815826
"testInputField: TestEnum"
816827
)
817828
test_enum_value = test_enum.values["TEST_VALUE"]
818829
assert test_enum_value
819-
assert print_node(test_enum_value.ast_node) == "TEST_VALUE"
820-
assert print_node(test_interface.fields["interfaceField"].ast_node) == (
830+
assert print_ast_node(test_enum_value) == "TEST_VALUE"
831+
assert print_ast_node(test_interface.fields["interfaceField"]) == (
821832
"interfaceField: String"
822833
)
823-
assert print_ast(test_directive.args["arg"].ast_node) == "arg: TestScalar"
834+
assert print_ast_node(test_directive.args["arg"]) == "arg: TestScalar"
824835

825836
def root_operation_type_with_custom_names():
826837
schema = build_schema(

tests/utilities/test_extend_schema.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Union
2+
13
from pytest import raises
24

35
from graphql import graphql_sync
4-
from graphql.language import parse, print_ast, DirectiveLocation, DocumentNode, Node
6+
from graphql.language import parse, print_ast, DirectiveLocation, DocumentNode
57
from graphql.pyutils import dedent
68
from graphql.type import (
79
GraphQLArgument,
@@ -17,6 +19,7 @@
1719
GraphQLInt,
1820
GraphQLInterfaceType,
1921
GraphQLList,
22+
GraphQLNamedType,
2023
GraphQLNonNull,
2124
GraphQLObjectType,
2225
GraphQLScalarType,
@@ -143,9 +146,14 @@ def print_test_schema_changes(extended_schema):
143146
return print_ast(ast)
144147

145148

146-
def print_node(node: Node) -> str:
147-
assert node
148-
return print_ast(node)
149+
TypeWithAstNode = Union[
150+
GraphQLArgument, GraphQLEnumValue, GraphQLField, GraphQLInputField, GraphQLNamedType
151+
]
152+
153+
154+
def print_ast_node(obj: TypeWithAstNode) -> str:
155+
assert obj is not None and obj.ast_node is not None
156+
return print_ast(obj.ast_node)
149157

150158

151159
def describe_extend_schema():
@@ -479,53 +487,49 @@ def correctly_assigns_ast_nodes_to_new_and_extended_types():
479487
) == print_schema(extended_twice_schema)
480488

481489
new_field = query.fields["newField"]
490+
assert print_ast_node(new_field) == "newField(testArg: TestInput): TestEnum"
491+
assert print_ast_node(new_field.args["testArg"]) == "testArg: TestInput"
482492
assert (
483-
print_node(new_field.ast_node) == "newField(testArg: TestInput): TestEnum"
484-
)
485-
assert print_node(new_field.args["testArg"].ast_node) == "testArg: TestInput"
486-
assert (
487-
print_node(query.fields["oneMoreNewField"].ast_node)
493+
print_ast_node(query.fields["oneMoreNewField"])
488494
== "oneMoreNewField: TestUnion"
489495
)
490496

491497
new_value = some_enum.values["NEW_VALUE"]
492498
assert some_enum
493-
assert print_node(new_value.ast_node) == "NEW_VALUE"
499+
assert print_ast_node(new_value) == "NEW_VALUE"
494500

495501
one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"]
496502
assert one_more_new_value
497-
assert print_node(one_more_new_value.ast_node) == "ONE_MORE_NEW_VALUE"
498-
assert print_node(some_input.fields["newField"].ast_node) == "newField: String"
503+
assert print_ast_node(one_more_new_value) == "ONE_MORE_NEW_VALUE"
504+
assert print_ast_node(some_input.fields["newField"]) == "newField: String"
499505
assert (
500-
print_node(some_input.fields["oneMoreNewField"].ast_node)
506+
print_ast_node(some_input.fields["oneMoreNewField"])
501507
== "oneMoreNewField: String"
502508
)
509+
assert print_ast_node(some_interface.fields["newField"]) == "newField: String"
503510
assert (
504-
print_node(some_interface.fields["newField"].ast_node) == "newField: String"
505-
)
506-
assert (
507-
print_node(some_interface.fields["oneMoreNewField"].ast_node)
511+
print_ast_node(some_interface.fields["oneMoreNewField"])
508512
== "oneMoreNewField: String"
509513
)
510514

511515
assert (
512-
print_node(test_input.fields["testInputField"].ast_node)
516+
print_ast_node(test_input.fields["testInputField"])
513517
== "testInputField: TestEnum"
514518
)
515519

516520
test_value = test_enum.values["TEST_VALUE"]
517521
assert test_value
518-
assert print_node(test_value.ast_node) == "TEST_VALUE"
522+
assert print_ast_node(test_value) == "TEST_VALUE"
519523

520524
assert (
521-
print_node(test_interface.fields["interfaceField"].ast_node)
525+
print_ast_node(test_interface.fields["interfaceField"])
522526
== "interfaceField: String"
523527
)
524528
assert (
525-
print_node(test_type.fields["interfaceField"].ast_node)
529+
print_ast_node(test_type.fields["interfaceField"])
526530
== "interfaceField: String"
527531
)
528-
assert print_node(test_directive.args["arg"].ast_node) == "arg: Int"
532+
assert print_ast_node(test_directive.args["arg"]) == "arg: Int"
529533

530534
def builds_types_with_deprecated_fields_and_values():
531535
extended_schema = extend_test_schema(
@@ -1150,7 +1154,7 @@ def adds_schema_definition_missing_in_the_original_schema():
11501154
schema = extend_schema(schema, parse(extension_sdl))
11511155
query_type = schema.query_type
11521156
assert query_type.name == "Foo"
1153-
assert print_node(schema.ast_node) == extension_sdl.rstrip()
1157+
assert print_ast_node(schema) == extension_sdl.rstrip()
11541158

11551159
def adds_new_root_types_via_schema_extension():
11561160
schema = extend_test_schema(

0 commit comments

Comments
 (0)