mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 19:43:56 +03:00
add test for sqlalchemy and identifier meta attribute
This commit is contained in:
parent
74a4043899
commit
0960b7914d
|
@ -11,6 +11,12 @@ association_table = Table('association', Base.metadata,
|
|||
Column('reporter_id', Integer, ForeignKey('reporters.id')))
|
||||
|
||||
|
||||
class Editor(Base):
|
||||
__tablename__ = 'editors'
|
||||
editor_id = Column(Integer(), primary_key=True)
|
||||
name = Column(String(100))
|
||||
|
||||
|
||||
class Pet(Base):
|
||||
__tablename__ = 'pets'
|
||||
id = Column(Integer(), primary_key=True)
|
||||
|
|
|
@ -7,7 +7,7 @@ from graphene import relay
|
|||
from graphene.contrib.sqlalchemy import (SQLAlchemyConnectionField,
|
||||
SQLAlchemyNode, SQLAlchemyObjectType)
|
||||
|
||||
from .models import Article, Base, Reporter
|
||||
from .models import Article, Base, Reporter, Editor
|
||||
|
||||
db = create_engine('sqlite:///test_sqlalchemy.sqlite3')
|
||||
|
||||
|
@ -37,6 +37,8 @@ def setup_fixtures(session):
|
|||
session.add(reporter2)
|
||||
article = Article(headline='Hi!')
|
||||
session.add(article)
|
||||
editor = Editor(name="John")
|
||||
session.add(editor)
|
||||
session.commit()
|
||||
|
||||
|
||||
|
@ -187,3 +189,51 @@ def test_should_node(session):
|
|||
result = schema.execute(query)
|
||||
assert not result.errors
|
||||
assert result.data == expected
|
||||
|
||||
|
||||
def test_should_custom_identifier(session):
|
||||
setup_fixtures(session)
|
||||
|
||||
class EditorNode(SQLAlchemyNode):
|
||||
|
||||
class Meta:
|
||||
model = Editor
|
||||
identifier = "editor_id"
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
node = relay.NodeField(EditorNode)
|
||||
all_editors = SQLAlchemyConnectionField(EditorNode)
|
||||
|
||||
query = '''
|
||||
query EditorQuery {
|
||||
allEditors {
|
||||
edges {
|
||||
node {
|
||||
id,
|
||||
name
|
||||
}
|
||||
}
|
||||
},
|
||||
node(id: "RWRpdG9yTm9kZTox") {
|
||||
name
|
||||
}
|
||||
}
|
||||
'''
|
||||
expected = {
|
||||
'allEditors': {
|
||||
'edges': [{
|
||||
'node': {
|
||||
'id': 'RWRpdG9yTm9kZTox',
|
||||
'name': 'John'
|
||||
}
|
||||
}]
|
||||
},
|
||||
'node': {
|
||||
'name': 'John'
|
||||
}
|
||||
}
|
||||
|
||||
schema = graphene.Schema(query=Query, session=session)
|
||||
result = schema.execute(query)
|
||||
assert not result.errors
|
||||
assert result.data == expected
|
||||
|
|
Loading…
Reference in New Issue
Block a user