Skip to content

Commit 6f87720

Browse files
committed
Improved tests and coverage
1 parent b474010 commit 6f87720

File tree

10 files changed

+369
-17
lines changed

10 files changed

+369
-17
lines changed

examples/starwars/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Character(graphene.Interface):
1515
id = graphene.IDField()
1616
name = graphene.StringField()
17-
friends = graphene.ListField('self')
17+
friends = graphene.ListField('Character')
1818
appears_in = graphene.ListField(Episode)
1919

2020
def resolve_friends(self, args, *_):

graphene/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
from graphql.core.type import (
2-
GraphQLEnumType as Enum,
3-
GraphQLArgument as Argument,
4-
GraphQLString as String,
5-
GraphQLInt as Int,
6-
GraphQLID as ID
2+
GraphQLEnumType as Enum
73
)
84

95
from graphene import signals
@@ -16,6 +12,19 @@
1612
ObjectType,
1713
Interface,
1814
Mutation,
15+
BaseType,
16+
LazyType,
17+
OrderedType,
18+
Argument,
19+
Field,
20+
InputField,
21+
String,
22+
Int,
23+
Boolean,
24+
ID,
25+
Float,
26+
List,
27+
NonNull
1928
)
2029

2130
from graphene.core.fields import (

graphene/core/fields.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import warnings
2+
3+
from .types.base import FieldType
14
from .types.field import Field
25
from .types.scalars import String, Int, Boolean, ID, Float
36
from .types.definitions import List, NonNull
47

58

6-
class DeprecatedField(object):
9+
class DeprecatedField(FieldType):
710
def __init__(self, *args, **kwargs):
8-
print("Using {} is not longer supported".format(self.__class__.__name__))
11+
cls = self.__class__
12+
warnings.warn("Using {} is not longer supported".format(cls.__name__)
13+
, FutureWarning)
914
kwargs['resolver'] = kwargs.pop('resolve', None)
15+
self.required = kwargs.pop('required', False)
1016
return super(DeprecatedField, self).__init__(*args, **kwargs)
1117

18+
def as_field(self):
19+
t = self
20+
if self.required:
21+
t = NonNull(t)
22+
return Field(t, _creation_counter=self.creation_counter, *self.args, **self.kwargs)
23+
1224

1325
class StringField(DeprecatedField, String):
1426
pass

graphene/core/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def T(self, object_type):
4444
if name:
4545
self._types_names[name] = object_type
4646
return self._types[object_type]
47+
else:
48+
return object_type
4749

4850
@property
4951
def query(self):
@@ -83,7 +85,7 @@ def register(self, object_type):
8385
return object_type
8486

8587
def get_type(self, type_name):
86-
self.schema._build_type_map()
88+
# self.schema._build_type_map()
8789
if type_name not in self._types_names:
8890
raise Exception('Type %s not found in %r' % (type_name, self))
8991
return self._types_names[type_name]

graphene/core/tests/test_fields.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
from py.test import raises
3+
from pytest import raises
4+
5+
from graphene.core.fields import Field, NonNullField, StringField
6+
from graphene.core.options import Options
7+
from graphene.core.schema import Schema
8+
from graphene.core.types import ObjectType
9+
from graphql.core.type import (GraphQLBoolean, GraphQLField, GraphQLID,
10+
GraphQLInt, GraphQLNonNull, GraphQLString)
11+
12+
13+
class ot(ObjectType):
14+
def resolve_customdoc(self, *args, **kwargs):
15+
'''Resolver documentation'''
16+
return None
17+
18+
def __str__(self):
19+
return "ObjectType"
20+
21+
schema = Schema()
22+
23+
24+
def test_field_no_contributed_raises_error():
25+
f = Field(GraphQLString)
26+
with raises(Exception) as excinfo:
27+
schema.T(f)
28+
29+
30+
def test_field_type():
31+
f = Field(GraphQLString)
32+
f.contribute_to_class(ot, 'field_name')
33+
assert isinstance(schema.T(f), GraphQLField)
34+
assert schema.T(f).type == GraphQLString
35+
36+
37+
def test_field_name_automatic_camelcase():
38+
f = Field(GraphQLString)
39+
f.contribute_to_class(ot, 'field_name')
40+
assert f.name == 'fieldName'
41+
42+
43+
def test_field_name_use_name_if_exists():
44+
f = Field(GraphQLString, name='my_custom_name')
45+
f.contribute_to_class(ot, 'field_name')
46+
assert f.name == 'my_custom_name'
47+
48+
49+
def test_stringfield_type():
50+
f = StringField()
51+
f.contribute_to_class(ot, 'field_name')
52+
assert schema.T(f) == GraphQLString
53+
54+
55+
def test_nonnullfield_type():
56+
f = NonNullField(StringField())
57+
f.contribute_to_class(ot, 'field_name')
58+
assert isinstance(schema.T(f), GraphQLNonNull)
59+
60+
61+
def test_stringfield_type_required():
62+
f = StringField(required=True).as_field()
63+
f.contribute_to_class(ot, 'field_name')
64+
assert isinstance(schema.T(f), GraphQLField)
65+
assert isinstance(schema.T(f).type, GraphQLNonNull)
66+
67+
68+
def test_field_resolve():
69+
f = StringField(required=True, resolve=lambda *args: 'RESOLVED').as_field()
70+
f.contribute_to_class(ot, 'field_name')
71+
field_type = schema.T(f)
72+
assert 'RESOLVED' == field_type.resolver(ot, None, None)
73+
74+
75+
def test_field_resolve_type_custom():
76+
class MyCustomType(ObjectType):
77+
pass
78+
79+
f = Field('MyCustomType')
80+
81+
class OtherType(ObjectType):
82+
field_name = f
83+
84+
s = Schema()
85+
s.query = OtherType
86+
s.register(MyCustomType)
87+
88+
assert s.T(f).type == s.T(MyCustomType)
89+
90+
91+
def test_field_orders():
92+
f1 = Field(None)
93+
f2 = Field(None)
94+
assert f1 < f2
95+
96+
97+
def test_field_orders_wrong_type():
98+
field = Field(None)
99+
try:
100+
assert not field < 1
101+
except TypeError:
102+
# Fix exception raising in Python3+
103+
pass
104+
105+
106+
def test_field_eq():
107+
f1 = Field(None)
108+
f2 = Field(None)
109+
assert f1 != f2
110+
111+
112+
def test_field_eq_wrong_type():
113+
field = Field(None)
114+
assert field != 1
115+
116+
117+
def test_field_hash():
118+
f1 = Field(None)
119+
f2 = Field(None)
120+
assert hash(f1) != hash(f2)
121+
122+
123+
def test_field_none_type_raises_error():
124+
s = Schema()
125+
f = Field(None)
126+
f.contribute_to_class(ot, 'field_name')
127+
with raises(Exception) as excinfo:
128+
s.T(f)
129+
assert str(
130+
excinfo.value) == "Internal type for field ot.field_name is None"
131+
132+
133+
def test_field_str():
134+
f = StringField().as_field()
135+
f.contribute_to_class(ot, 'field_name')
136+
assert str(f) == "ot.field_name"
137+
138+
139+
def test_field_repr():
140+
f = StringField().as_field()
141+
assert repr(f) == "<graphene.core.types.field.Field>"
142+
143+
144+
def test_field_repr_contributed():
145+
f = StringField().as_field()
146+
f.contribute_to_class(ot, 'field_name')
147+
assert repr(f) == "<graphene.core.types.field.Field: field_name>"
148+
149+
150+
def test_field_resolve_objecttype_cos():
151+
f = StringField().as_field()
152+
f.contribute_to_class(ot, 'customdoc')
153+
field = schema.T(f)
154+
assert field.description == 'Resolver documentation'

graphene/core/tests/test_schema.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
from py.test import raises
3+
from pytest import raises
4+
5+
from graphene import Interface, ObjectType, Schema
6+
from graphene.core.fields import Field, ListField, StringField
7+
from graphene.core.types.base import LazyType
8+
from graphql.core import graphql
9+
from graphql.core.type import (GraphQLInterfaceType, GraphQLObjectType,
10+
GraphQLSchema)
11+
from tests.utils import assert_equal_lists
12+
13+
schema = Schema(name='My own schema')
14+
15+
16+
class Character(Interface):
17+
name = StringField()
18+
19+
20+
class Pet(ObjectType):
21+
type = StringField(resolve=lambda *_: 'Dog')
22+
23+
24+
class Human(Character):
25+
friends = ListField(Character)
26+
pet = Field(Pet)
27+
28+
def resolve_name(self, *args):
29+
return 'Peter'
30+
31+
def resolve_friend(self, *args):
32+
return Human(object())
33+
34+
def resolve_pet(self, *args):
35+
return Pet(object())
36+
37+
schema.query = Human
38+
39+
40+
def test_get_registered_type():
41+
assert schema.get_type('Character') == Character
42+
43+
44+
def test_get_unregistered_type():
45+
with raises(Exception) as excinfo:
46+
schema.get_type('NON_EXISTENT_MODEL')
47+
assert 'not found' in str(excinfo.value)
48+
49+
50+
def test_schema_query():
51+
assert schema.query == Human
52+
53+
54+
def test_query_schema_graphql():
55+
object()
56+
query = '''
57+
{
58+
name
59+
pet {
60+
type
61+
}
62+
}
63+
'''
64+
expected = {
65+
'name': 'Peter',
66+
'pet': {
67+
'type': 'Dog'
68+
}
69+
}
70+
result = graphql(schema.schema, query, root=Human(object()))
71+
assert not result.errors
72+
assert result.data == expected
73+
74+
75+
def test_query_schema_execute():
76+
object()
77+
query = '''
78+
{
79+
name
80+
pet {
81+
type
82+
}
83+
}
84+
'''
85+
expected = {
86+
'name': 'Peter',
87+
'pet': {
88+
'type': 'Dog'
89+
}
90+
}
91+
result = schema.execute(query, root=object())
92+
assert not result.errors
93+
assert result.data == expected
94+
95+
96+
def test_schema_get_type_map():
97+
assert_equal_lists(
98+
schema.schema.get_type_map().keys(),
99+
['__Field', 'String', 'Pet', 'Character', '__InputValue', '__Directive',
100+
'__TypeKind', '__Schema', '__Type', 'Human', '__EnumValue', 'Boolean']
101+
)
102+
103+
104+
def test_schema_no_query():
105+
schema = Schema(name='My own schema')
106+
with raises(Exception) as excinfo:
107+
schema.schema
108+
assert 'define a base query type' in str(excinfo)
109+
110+
111+
def test_schema_register():
112+
schema = Schema(name='My own schema')
113+
114+
@schema.register
115+
class MyType(ObjectType):
116+
type = StringField(resolve=lambda *_: 'Dog')
117+
118+
schema.query = MyType
119+
120+
assert schema.get_type('MyType') == MyType
121+
122+
123+
def test_schema_register():
124+
schema = Schema(name='My own schema')
125+
126+
@schema.register
127+
class MyType(ObjectType):
128+
type = StringField(resolve=lambda *_: 'Dog')
129+
130+
with raises(Exception) as excinfo:
131+
schema.get_type('MyType')
132+
assert 'base query type' in str(excinfo.value)
133+
134+
135+
def test_schema_introspect():
136+
schema = Schema(name='My own schema')
137+
138+
class MyType(ObjectType):
139+
type = StringField(resolve=lambda *_: 'Dog')
140+
141+
schema.query = MyType
142+
143+
introspection = schema.introspect()
144+
assert '__schema' in introspection
145+
146+
147+
def test_lazytype():
148+
schema = Schema(name='My own schema')
149+
150+
t = LazyType('MyType')
151+
152+
class MyType(ObjectType):
153+
type = StringField(resolve=lambda *_: 'Dog')
154+
155+
schema.query = MyType
156+
157+
assert schema.T(t) == schema.T(MyType)

0 commit comments

Comments
 (0)