From 0960b7914d02ff6d0916ded2dda214f0a9f3b98f Mon Sep 17 00:00:00 2001 From: evalette Date: Wed, 13 Apr 2016 14:39:11 +0200 Subject: [PATCH] add test for sqlalchemy and identifier meta attribute --- graphene/contrib/sqlalchemy/tests/models.py | 6 +++ .../contrib/sqlalchemy/tests/test_query.py | 52 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/graphene/contrib/sqlalchemy/tests/models.py b/graphene/contrib/sqlalchemy/tests/models.py index ee021054..40f95e59 100644 --- a/graphene/contrib/sqlalchemy/tests/models.py +++ b/graphene/contrib/sqlalchemy/tests/models.py @@ -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) diff --git a/graphene/contrib/sqlalchemy/tests/test_query.py b/graphene/contrib/sqlalchemy/tests/test_query.py index 5f970488..ce66d196 100644 --- a/graphene/contrib/sqlalchemy/tests/test_query.py +++ b/graphene/contrib/sqlalchemy/tests/test_query.py @@ -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