Skip to content

Commit 850eddf

Browse files
committed
definition tests: simplify NonNull and List tests
Replicates graphql/graphql-js@271e23e
1 parent b4da0b2 commit 850eddf

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.1.1. All parts of the API are covered by an extensive test suite of currently 1731
16+
14.1.1. All parts of the API are covered by an extensive test suite of currently 1727
1717
unit tests.
1818

1919

tests/type/test_definition.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
from graphql.error import INVALID
66
from graphql.type import (
77
GraphQLArgument,
8+
GraphQLEnumValue,
9+
GraphQLEnumType,
810
GraphQLField,
911
GraphQLFieldResolver,
12+
GraphQLInputField,
13+
GraphQLInputObjectType,
1014
GraphQLInt,
11-
GraphQLString,
12-
GraphQLObjectType,
15+
GraphQLInterfaceType,
1316
GraphQLList,
17+
GraphQLNonNull,
18+
GraphQLObjectType,
19+
GraphQLOutputType,
1420
GraphQLScalarType,
15-
GraphQLInterfaceType,
16-
GraphQLUnionType,
17-
GraphQLEnumType,
18-
GraphQLEnumValue,
19-
GraphQLInputObjectType,
2021
GraphQLSchema,
21-
GraphQLOutputType,
22-
GraphQLInputField,
23-
GraphQLNonNull,
22+
GraphQLString,
23+
GraphQLUnionType,
2424
)
2525

2626
ObjectType = GraphQLObjectType("Object", {})
@@ -29,6 +29,10 @@
2929
EnumType = GraphQLEnumType("Enum", {"foo": GraphQLEnumValue()})
3030
InputObjectType = GraphQLInputObjectType("InputObject", {})
3131
ScalarType = GraphQLScalarType("Scalar", serialize=lambda: None)
32+
NonNullScalarType = GraphQLNonNull(ScalarType)
33+
ListOfScalarsType = GraphQLList(ScalarType)
34+
NonNullListOfScalars = GraphQLNonNull(ListOfScalarsType)
35+
ListOfNonNullScalarsType = GraphQLList(NonNullScalarType)
3236

3337

3438
def schema_with_field_type(type_: GraphQLOutputType) -> GraphQLSchema:
@@ -100,19 +104,20 @@ def stringifies_simple_types():
100104
assert str(UnionType) == "Union"
101105
assert str(EnumType) == "Enum"
102106
assert str(InputObjectType) == "InputObject"
103-
assert str(GraphQLNonNull(GraphQLInt)) == "Int!"
104-
assert str(GraphQLList(GraphQLInt)) == "[Int]"
105-
assert str(GraphQLNonNull(GraphQLList(GraphQLInt))) == "[Int]!"
106-
assert str(GraphQLList(GraphQLNonNull(GraphQLInt))) == "[Int!]"
107-
assert str(GraphQLList(GraphQLList(GraphQLInt))) == "[[Int]]"
107+
108+
assert str(NonNullScalarType) == "Scalar!"
109+
assert str(ListOfScalarsType) == "[Scalar]"
110+
assert str(NonNullListOfScalars) == "[Scalar]!"
111+
assert str(ListOfNonNullScalarsType) == "[Scalar!]"
112+
assert str(GraphQLList(ListOfScalarsType)) == "[[Scalar]]"
108113

109114
def prohibits_nesting_nonnull_inside_nonnull():
110115
with raises(TypeError) as exc_info:
111116
# noinspection PyTypeChecker
112-
GraphQLNonNull(GraphQLNonNull(GraphQLInt))
117+
GraphQLNonNull(GraphQLNonNull(NonNullScalarType))
113118
msg = str(exc_info.value)
114119
assert msg == (
115-
"Can only create NonNull of a Nullable GraphQLType but got: Int!."
120+
"Can only create NonNull of a Nullable GraphQLType but got: Scalar!."
116121
)
117122

118123
def allows_a_thunk_for_union_member_types():
@@ -622,15 +627,14 @@ def does_not_allow_is_deprecated():
622627
def describe_type_system_list_must_accept_only_types():
623628

624629
types = [
625-
GraphQLString,
626630
ScalarType,
627631
ObjectType,
628632
UnionType,
629633
InterfaceType,
630634
EnumType,
631635
InputObjectType,
632-
GraphQLList(GraphQLString),
633-
GraphQLNonNull(GraphQLString),
636+
ListOfScalarsType,
637+
NonNullScalarType,
634638
]
635639

636640
not_types = [{}, dict, str, object, None]
@@ -642,7 +646,7 @@ def accepts_a_type_as_item_type_of_list(type_):
642646
@mark.parametrize("type_", not_types, ids=lambda type_: type_.__class__.__name__)
643647
def rejects_a_non_type_as_item_type_of_list(type_):
644648
with raises(TypeError) as exc_info:
645-
assert GraphQLList(type_)
649+
GraphQLList(type_)
646650
msg = str(exc_info.value)
647651
assert msg == (
648652
f"Can only create a wrapper for a GraphQLType, but got: {type_}."
@@ -652,18 +656,17 @@ def rejects_a_non_type_as_item_type_of_list(type_):
652656
def describe_type_system_non_null_must_only_accept_non_nullable_types():
653657

654658
nullable_types = [
655-
GraphQLString,
656659
ScalarType,
657660
ObjectType,
658661
UnionType,
659662
InterfaceType,
660663
EnumType,
661664
InputObjectType,
662-
GraphQLList(GraphQLString),
663-
GraphQLList(GraphQLNonNull(GraphQLString)),
665+
ListOfScalarsType,
666+
ListOfNonNullScalarsType,
664667
]
665668

666-
not_nullable_types = [GraphQLNonNull(GraphQLString), {}, dict, str, object, None]
669+
not_nullable_types = [NonNullScalarType, {}, dict, str, object, None]
667670

668671
@mark.parametrize("type_", nullable_types)
669672
def accepts_a_type_as_nullable_type_of_non_null(type_):
@@ -672,7 +675,7 @@ def accepts_a_type_as_nullable_type_of_non_null(type_):
672675
@mark.parametrize("type_", not_nullable_types)
673676
def rejects_a_non_type_as_nullable_type_of_non_null(type_):
674677
with raises(TypeError) as exc_info:
675-
assert GraphQLNonNull(type_)
678+
GraphQLNonNull(type_)
676679
msg = str(exc_info.value)
677680
assert (
678681
msg

0 commit comments

Comments
 (0)