|
| 1 | +from collections import OrderedDict |
| 2 | +import json |
| 3 | + |
| 4 | +from graphql import graphql |
| 5 | +from graphql.type import GraphQLSchema, GraphQLString, GraphQLObjectType, GraphQLInt, GraphQLField, GraphQLArgument |
| 6 | + |
| 7 | + |
| 8 | +def _test_schema(test_field): |
| 9 | + return GraphQLSchema( |
| 10 | + query=GraphQLObjectType( |
| 11 | + name='Query', |
| 12 | + fields={ |
| 13 | + 'test': test_field |
| 14 | + } |
| 15 | + ) |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def test_default_function_accesses_properties(): |
| 20 | + schema = _test_schema(GraphQLField(GraphQLString)) |
| 21 | + |
| 22 | + class source: |
| 23 | + test = 'testValue' |
| 24 | + |
| 25 | + result = graphql(schema, '{ test }', source) |
| 26 | + assert not result.errors |
| 27 | + assert result.data == {'test': 'testValue'} |
| 28 | + |
| 29 | + |
| 30 | +def test_default_function_calls_methods(): |
| 31 | + schema = _test_schema(GraphQLField(GraphQLString)) |
| 32 | + |
| 33 | + class source: |
| 34 | + _secret = 'testValue' |
| 35 | + |
| 36 | + def test(self): |
| 37 | + return self._secret |
| 38 | + |
| 39 | + result = graphql(schema, '{ test }', source()) |
| 40 | + assert not result.errors |
| 41 | + assert result.data == {'test': 'testValue'} |
| 42 | + |
| 43 | + |
| 44 | +def test_uses_provided_resolve_function(): |
| 45 | + def resolver(source, args, *_): |
| 46 | + return json.dumps([source, args], separators=(',', ':')) |
| 47 | + |
| 48 | + schema = _test_schema(GraphQLField( |
| 49 | + GraphQLString, |
| 50 | + args=OrderedDict([ |
| 51 | + ('aStr', GraphQLArgument(GraphQLString)), |
| 52 | + ('aInt', GraphQLArgument(GraphQLInt)), |
| 53 | + ]), |
| 54 | + resolver=resolver |
| 55 | + )) |
| 56 | + |
| 57 | + result = graphql(schema, '{ test }', None) |
| 58 | + assert not result.errors |
| 59 | + assert result.data == {'test': '[null,{}]'} |
| 60 | + |
| 61 | + result = graphql(schema, '{ test(aStr: "String!") }', 'Source!') |
| 62 | + assert not result.errors |
| 63 | + assert result.data == {'test': '["Source!",{"aStr":"String!"}]'} |
| 64 | + |
| 65 | + result = graphql(schema, '{ test(aInt: -123, aStr: "String!",) }', 'Source!') |
| 66 | + assert not result.errors |
| 67 | + assert result.data in [ |
| 68 | + {'test': '["Source!",{"aStr":"String!","aInt":-123}]'}, |
| 69 | + {'test': '["Source!",{"aInt":-123,"aStr":"String!"}]'} |
| 70 | + ] |
0 commit comments