Skip to content

Commit 0657bae

Browse files
committed
added additional tests for CustomSQLAlchemyObjectType
1 parent 7a77680 commit 0657bae

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

graphene_sqlalchemy/tests/test_types.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

22
from graphene import Field, Int, Interface, ObjectType
33
from graphene.relay import Node, is_node
4+
import six
45

56
from ..registry import Registry
6-
from ..types import SQLAlchemyObjectType
7+
from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeMeta
78
from .models import Article, Reporter
89

910
registry = Registry()
@@ -74,3 +75,46 @@ def test_object_type():
7475
assert issubclass(Human, ObjectType)
7576
assert list(Human._meta.fields.keys()) == ['id', 'headline', 'reporter_id', 'reporter', 'pub_date']
7677
assert is_node(Human)
78+
79+
80+
81+
# Test Custom SQLAlchemyObjectType Implementation
82+
class CustomSQLAlchemyObjectType(six.with_metaclass(SQLAlchemyObjectTypeMeta, ObjectType)):
83+
@classmethod
84+
def is_type_of(cls, root, context, info):
85+
return SQLAlchemyObjectType.is_type_of(root, context, info)
86+
87+
@classmethod
88+
def get_query(cls, context):
89+
return SQLAlchemyObjectType.get_query(context)
90+
91+
@classmethod
92+
def get_node(cls, id, context, info):
93+
return SQLAlchemyObjectType.get_node(id, context, info)
94+
95+
@classmethod
96+
def resolve_id(self, args, context, info):
97+
graphene_type = info.parent_type.graphene_type
98+
if is_node(graphene_type):
99+
return self.__mapper__.primary_key_from_instance(self)[0]
100+
return getattr(self, graphene_type._meta.id, None)
101+
102+
103+
class CustomCharacter(CustomSQLAlchemyObjectType):
104+
'''Character description'''
105+
class Meta:
106+
model = Reporter
107+
registry = registry
108+
109+
def test_custom_objecttype_registered():
110+
assert issubclass(CustomCharacter, ObjectType)
111+
assert CustomCharacter._meta.model == Reporter
112+
assert list(
113+
CustomCharacter._meta.fields.keys()) == [
114+
'id',
115+
'first_name',
116+
'last_name',
117+
'email',
118+
'pets',
119+
'articles',
120+
'favorite_article']

0 commit comments

Comments
 (0)