|
| 1 | +import graphene |
| 2 | +import pytest |
| 3 | +from sqlalchemy.orm import scoped_session, sessionmaker |
| 4 | + |
| 5 | +from graphene_sqlalchemy import SQLAlchemyObjectType |
| 6 | +from graphene_sqlalchemy.mutations import create |
| 7 | +from graphene_sqlalchemy.registry import reset_global_registry |
| 8 | +from graphene_sqlalchemy.tests.models import Base, Reporter |
| 9 | + |
| 10 | + |
| 11 | +@pytest.yield_fixture(scope='function') |
| 12 | +def session(db): |
| 13 | + reset_global_registry() |
| 14 | + connection = db.engine.connect() |
| 15 | + transaction = connection.begin() |
| 16 | + Base.metadata.create_all(connection) |
| 17 | + |
| 18 | + # options = dict(bind=connection, binds={}) |
| 19 | + session_factory = sessionmaker(bind=connection) |
| 20 | + session = scoped_session(session_factory) |
| 21 | + |
| 22 | + yield session |
| 23 | + |
| 24 | + # Finalize test here |
| 25 | + transaction.rollback() |
| 26 | + connection.close() |
| 27 | + session.remove() |
| 28 | + |
| 29 | + |
| 30 | +def test_should_create_with_create_fields(session): |
| 31 | + class ReporterType(SQLAlchemyObjectType): |
| 32 | + class Meta: |
| 33 | + model = Reporter |
| 34 | + |
| 35 | + class Query(graphene.ObjectType): |
| 36 | + reporters = graphene.List(ReporterType) |
| 37 | + |
| 38 | + def resolve_reporters(self, *args, **kwargs): |
| 39 | + return session.query(Reporter) |
| 40 | + |
| 41 | + class Mutation(graphene.ObjectType): |
| 42 | + createReporter = create(ReporterType) |
| 43 | + |
| 44 | + query = ''' |
| 45 | + query ReporterQuery { |
| 46 | + reporters { |
| 47 | + firstName, |
| 48 | + lastName, |
| 49 | + email |
| 50 | + } |
| 51 | + } |
| 52 | + ''' |
| 53 | + expected = { |
| 54 | + 'reporters': [] |
| 55 | + } |
| 56 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 57 | + result = schema.execute(query) |
| 58 | + assert not result.errors |
| 59 | + assert result.data == expected |
| 60 | + |
| 61 | + query = ''' |
| 62 | + mutation createReporter{ |
| 63 | + createReporter (firstName: "ABC", lastName: "def") { |
| 64 | + firstName, |
| 65 | + lastName, |
| 66 | + email |
| 67 | + } |
| 68 | + } |
| 69 | + ''' |
| 70 | + expected = { |
| 71 | + 'createReporter': { |
| 72 | + 'firstName': 'ABC', |
| 73 | + 'lastName': 'def', |
| 74 | + 'email': None, |
| 75 | + } |
| 76 | + } |
| 77 | + schema = graphene.Schema(query=Query, mutation=Mutation) |
| 78 | + result = schema.execute(query, context_value={'session': session}) |
| 79 | + assert not result.errors |
| 80 | + assert result.data == expected |
0 commit comments