Skip to content

Commit 0960b79

Browse files
author
evalette
committed
add test for sqlalchemy and identifier meta attribute
1 parent 74a4043 commit 0960b79

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

graphene/contrib/sqlalchemy/tests/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
Column('reporter_id', Integer, ForeignKey('reporters.id')))
1212

1313

14+
class Editor(Base):
15+
__tablename__ = 'editors'
16+
editor_id = Column(Integer(), primary_key=True)
17+
name = Column(String(100))
18+
19+
1420
class Pet(Base):
1521
__tablename__ = 'pets'
1622
id = Column(Integer(), primary_key=True)

graphene/contrib/sqlalchemy/tests/test_query.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from graphene.contrib.sqlalchemy import (SQLAlchemyConnectionField,
88
SQLAlchemyNode, SQLAlchemyObjectType)
99

10-
from .models import Article, Base, Reporter
10+
from .models import Article, Base, Reporter, Editor
1111

1212
db = create_engine('sqlite:///test_sqlalchemy.sqlite3')
1313

@@ -37,6 +37,8 @@ def setup_fixtures(session):
3737
session.add(reporter2)
3838
article = Article(headline='Hi!')
3939
session.add(article)
40+
editor = Editor(name="John")
41+
session.add(editor)
4042
session.commit()
4143

4244

@@ -187,3 +189,51 @@ def resolve_article(self, *args, **kwargs):
187189
result = schema.execute(query)
188190
assert not result.errors
189191
assert result.data == expected
192+
193+
194+
def test_should_custom_identifier(session):
195+
setup_fixtures(session)
196+
197+
class EditorNode(SQLAlchemyNode):
198+
199+
class Meta:
200+
model = Editor
201+
identifier = "editor_id"
202+
203+
class Query(graphene.ObjectType):
204+
node = relay.NodeField(EditorNode)
205+
all_editors = SQLAlchemyConnectionField(EditorNode)
206+
207+
query = '''
208+
query EditorQuery {
209+
allEditors {
210+
edges {
211+
node {
212+
id,
213+
name
214+
}
215+
}
216+
},
217+
node(id: "RWRpdG9yTm9kZTox") {
218+
name
219+
}
220+
}
221+
'''
222+
expected = {
223+
'allEditors': {
224+
'edges': [{
225+
'node': {
226+
'id': 'RWRpdG9yTm9kZTox',
227+
'name': 'John'
228+
}
229+
}]
230+
},
231+
'node': {
232+
'name': 'John'
233+
}
234+
}
235+
236+
schema = graphene.Schema(query=Query, session=session)
237+
result = schema.execute(query)
238+
assert not result.errors
239+
assert result.data == expected

0 commit comments

Comments
 (0)