|
1 | | -from graphql.core.execution import execute |
| 1 | +from collections import namedtuple |
| 2 | +from graphql.core.error import format_error |
| 3 | +from graphql.core.execution import execute, Executor |
2 | 4 | from graphql.core.language.parser import parse |
| 5 | +from graphql.core.pyutils.defer import succeed, fail |
3 | 6 | from graphql.core.type import ( |
4 | 7 | GraphQLSchema, |
5 | 8 | GraphQLObjectType, |
|
9 | 12 | GraphQLNonNull, |
10 | 13 | ) |
11 | 14 |
|
12 | | -def run(test_type, test_data): |
13 | | - class Data(object): |
14 | | - test = test_data |
15 | | - |
16 | | - DataType = GraphQLObjectType('DataType', lambda: { |
17 | | - 'test': GraphQLField(test_type), |
18 | | - 'nest': GraphQLField(DataType, resolver=lambda *_: Data()) |
19 | | - }) |
20 | | - |
21 | | - schema = GraphQLSchema(DataType) |
22 | | - ast = parse('{ nest { test } }') |
23 | | - return execute(schema, Data(), ast) |
24 | | - |
25 | | - |
26 | | -def check(test_type, test_data, expected_data): |
27 | | - result = run(test_type, test_data) |
28 | | - assert not result.errors |
29 | | - assert result.data == expected_data |
30 | | - |
31 | | - |
32 | | -# Execute: Handles list nullability |
33 | | - |
34 | | -def test_nullable_list_of_nullables(): |
35 | | - type = GraphQLList(GraphQLInt) # [T] |
36 | | - # Contains values |
37 | | - check(type, [1, 2], {'nest': {'test': [1, 2]}}) |
38 | | - # Contains null |
39 | | - check(type, [1, None, 2], {'nest': {'test': [1, None, 2]}}) |
40 | | - # Returns null |
41 | | - check(type, None, {'nest': {'test': None}}) |
42 | | - |
43 | | - |
44 | | -def test_non_nullable_list_of_nullables(): |
45 | | - type = GraphQLNonNull(GraphQLList(GraphQLInt)) # [T]! |
46 | | - # Contains values |
47 | | - check(type, [1, 2], {'nest': {'test': [1, 2]}}) |
48 | | - # Contains null |
49 | | - check(type, [1, None, 2], {'nest': {'test': [1, None, 2]}}) |
50 | | - # Returns null |
51 | | - result = run(type, None) |
52 | | - assert len(result.errors) == 1 |
53 | | - assert result.errors[0].message == 'Cannot return null for non-nullable field DataType.test.' |
54 | | - # TODO: check error location |
55 | | - assert result.data == {'nest': None} |
56 | | - |
57 | | - |
58 | | -def test_nullable_list_of_non_nullables(): |
59 | | - type = GraphQLList(GraphQLNonNull(GraphQLInt)) # [T!] |
60 | | - # Contains values |
61 | | - check(type, [1, 2], {'nest': {'test': [1, 2]}}) |
62 | | - # Contains null |
63 | | - result = run(type, [1, None, 2]) |
64 | | - assert len(result.errors) == 1 |
65 | | - assert result.errors[0].message == 'Cannot return null for non-nullable field DataType.test.' |
66 | | - # TODO: check error location |
67 | | - assert result.data == {'nest': {'test': None}} |
68 | | - # Returns null |
69 | | - check(type, None, {'nest': {'test': None}}) |
70 | | - |
71 | | - |
72 | | -def test_non_nullable_list_of_non_nullables(): |
73 | | - type = GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLInt))) # [T!]! |
74 | | - # Contains values |
75 | | - check(type, [1, 2], {'nest': {'test': [1, 2]}}) |
76 | | - # Contains null |
77 | | - result = run(type, [1, None, 2]) |
78 | | - assert len(result.errors) == 1 |
79 | | - assert result.errors[0].message == 'Cannot return null for non-nullable field DataType.test.' |
80 | | - # TODO: check error location |
81 | | - assert result.data == {'nest': None} |
82 | | - # Returns null |
83 | | - result = run(type, None) |
84 | | - assert len(result.errors) == 1 |
85 | | - assert result.errors[0].message == 'Cannot return null for non-nullable field DataType.test.' |
86 | | - # TODO: check error location |
87 | | - assert result.data == {'nest': None} |
| 15 | +Data = namedtuple('Data', 'test') |
| 16 | +ast = parse('{ nest { test } }') |
| 17 | +executor = Executor() |
| 18 | + |
| 19 | + |
| 20 | +def check(test_data, expected): |
| 21 | + def run_check(self): |
| 22 | + test_type = self.type |
| 23 | + |
| 24 | + data = Data(test=test_data) |
| 25 | + DataType = GraphQLObjectType( |
| 26 | + name='DataType', |
| 27 | + fields=lambda: { |
| 28 | + 'test': GraphQLField(test_type), |
| 29 | + 'nest': GraphQLField(DataType, resolver=lambda *_: data) |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | + schema = GraphQLSchema(query=DataType) |
| 34 | + response = executor.execute(schema, ast, data) |
| 35 | + assert response.called |
| 36 | + response = response.result |
| 37 | + |
| 38 | + if response.errors: |
| 39 | + result = { |
| 40 | + 'data': response.data, |
| 41 | + 'errors': [format_error(e) for e in response.errors] |
| 42 | + } |
| 43 | + else: |
| 44 | + result = { |
| 45 | + 'data': response.data |
| 46 | + } |
| 47 | + |
| 48 | + assert result == expected |
| 49 | + |
| 50 | + return run_check |
| 51 | + |
| 52 | + |
| 53 | +class Test_ListOfT_Array_T: # [T] Array<T> |
| 54 | + type = GraphQLList(GraphQLInt) |
| 55 | + |
| 56 | + test_contains_values = check([1, 2], {'data': {'nest': {'test': [1, 2]}}}) |
| 57 | + test_contains_null = check([1, None, 2], {'data': {'nest': {'test': [1, None, 2]}}}) |
| 58 | + test_returns_null = check(None, {'data': {'nest': {'test': None}}}) |
| 59 | + |
| 60 | + |
| 61 | +class Test_ListOfT_Promise_Array_T: # [T] Promise<Array<T>> |
| 62 | + type = GraphQLList(GraphQLInt) |
| 63 | + |
| 64 | + test_contains_values = check(succeed([1, 2]), {'data': {'nest': {'test': [1, 2]}}}) |
| 65 | + test_contains_null = check(succeed([1, None, 2]), {'data': {'nest': {'test': [1, None, 2]}}}) |
| 66 | + test_returns_null = check(succeed(None), {'data': {'nest': {'test': None}}}) |
| 67 | + test_rejected = check(lambda: fail(Exception('bad')), { |
| 68 | + 'data': {'nest': {'test': None}}, |
| 69 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 70 | + }) |
| 71 | + |
| 72 | + |
| 73 | +class Test_ListOfT_Array_Promise_T: # [T] Array<Promise<T>> |
| 74 | + type = GraphQLList(GraphQLInt) |
| 75 | + |
| 76 | + test_contains_values = check([succeed(1), succeed(2)], {'data': {'nest': {'test': [1, 2]}}}) |
| 77 | + test_contains_null = check([succeed(1), succeed(None), succeed(2)], {'data': {'nest': {'test': [1, None, 2]}}}) |
| 78 | + test_contains_reject = check(lambda: [succeed(1), fail(Exception('bad')), succeed(2)], { |
| 79 | + 'data': {'nest': {'test': [1, None, 2]}}, |
| 80 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 81 | + }) |
| 82 | + |
| 83 | + |
| 84 | +class Test_NotNullListOfT_Array_T: # [T]! Array<T> |
| 85 | + type = GraphQLNonNull(GraphQLList(GraphQLInt)) |
| 86 | + |
| 87 | + test_contains_values = check(succeed([1, 2]), {'data': {'nest': {'test': [1, 2]}}}) |
| 88 | + test_contains_null = check(succeed([1, None, 2]), {'data': {'nest': {'test': [1, None, 2]}}}) |
| 89 | + test_returns_null = check(succeed(None), { |
| 90 | + 'data': {'nest': None}, |
| 91 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 92 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 93 | + }) |
| 94 | + |
| 95 | + |
| 96 | +class Test_NotNullListOfT_Promise_Array_T: # [T]! Promise<Array<T>>> |
| 97 | + type = GraphQLNonNull(GraphQLList(GraphQLInt)) |
| 98 | + |
| 99 | + test_contains_values = check(succeed([1, 2]), {'data': {'nest': {'test': [1, 2]}}}) |
| 100 | + test_contains_null = check(succeed([1, None, 2]), {'data': {'nest': {'test': [1, None, 2]}}}) |
| 101 | + test_returns_null = check(succeed(None), { |
| 102 | + 'data': {'nest': None}, |
| 103 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 104 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 105 | + }) |
| 106 | + |
| 107 | + test_rejected = check(lambda: fail(Exception('bad')), { |
| 108 | + 'data': {'nest': None}, |
| 109 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 110 | + }) |
| 111 | + |
| 112 | + |
| 113 | +class Test_NotNullListOfT_Array_Promise_T: # [T]! Promise<Array<T>>> |
| 114 | + type = GraphQLNonNull(GraphQLList(GraphQLInt)) |
| 115 | + test_contains_values = check([succeed(1), succeed(2)], {'data': {'nest': {'test': [1, 2]}}}) |
| 116 | + test_contains_null = check([succeed(1), succeed(None), succeed(2)], {'data': {'nest': {'test': [1, None, 2]}}}) |
| 117 | + test_contains_reject = check(lambda: [succeed(1), fail(Exception('bad')), succeed(2)], { |
| 118 | + 'data': {'nest': {'test': [1, None, 2]}}, |
| 119 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 120 | + }) |
| 121 | + |
| 122 | + |
| 123 | +class TestListOfNotNullT_Array_T: # [T!] Array<T> |
| 124 | + type = GraphQLList(GraphQLNonNull(GraphQLInt)) |
| 125 | + |
| 126 | + test_contains_values = check([1, 2], {'data': {'nest': {'test': [1, 2]}}}) |
| 127 | + test_contains_null = check([1, None, 2], { |
| 128 | + 'data': {'nest': {'test': None}}, |
| 129 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 130 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 131 | + }) |
| 132 | + test_returns_null = check(None, {'data': {'nest': {'test': None}}}) |
| 133 | + |
| 134 | + |
| 135 | +class TestListOfNotNullT_Promise_Array_T: # [T!] Promise<Array<T>> |
| 136 | + type = GraphQLList(GraphQLNonNull(GraphQLInt)) |
| 137 | + |
| 138 | + test_contains_value = check(succeed([1, 2]), {'data': {'nest': {'test': [1, 2]}}}) |
| 139 | + test_contains_null = check(succeed([1, None, 2]), { |
| 140 | + 'data': {'nest': {'test': None}}, |
| 141 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 142 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 143 | + }) |
| 144 | + |
| 145 | + test_returns_null = check(succeed(None), {'data': {'nest': {'test': None}}}) |
| 146 | + |
| 147 | + test_rejected = check(lambda: fail(Exception('bad')), { |
| 148 | + 'data': {'nest': {'test': None}}, |
| 149 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 150 | + }) |
| 151 | + |
| 152 | + |
| 153 | +class TestListOfNotNullT_Array_Promise_T: # [T!] Array<Promise<T>> |
| 154 | + type = GraphQLList(GraphQLNonNull(GraphQLInt)) |
| 155 | + |
| 156 | + test_contains_values = check([succeed(1), succeed(2)], {'data': {'nest': {'test': [1, 2]}}}) |
| 157 | + test_contains_null = check([succeed(1), succeed(None), succeed(2)], { |
| 158 | + 'data': {'nest': {'test': None}}, |
| 159 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 160 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 161 | + }) |
| 162 | + test_contains_reject = check(lambda: [succeed(1), fail(Exception('bad')), succeed(2)], { |
| 163 | + 'data': {'nest': {'test': None}}, |
| 164 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 165 | + }) |
| 166 | + |
| 167 | + |
| 168 | +class TestNotNullListOfNotNullT_Array_T: # [T!]! Array<T> |
| 169 | + type = GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLInt))) |
| 170 | + |
| 171 | + test_contains_values = check([1, 2], {'data': {'nest': {'test': [1, 2]}}}) |
| 172 | + test_contains_null = check([1, None, 2], { |
| 173 | + 'data': {'nest': None}, |
| 174 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 175 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 176 | + }) |
| 177 | + test_returns_null = check(None, { |
| 178 | + 'data': {'nest': None}, |
| 179 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 180 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 181 | + }) |
| 182 | + |
| 183 | + |
| 184 | +class TestNotNullListOfNotNullT_Promise_Array_T: # [T!]! Promise<Array<T>> |
| 185 | + type = GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLInt))) |
| 186 | + |
| 187 | + test_contains_value = check(succeed([1, 2]), {'data': {'nest': {'test': [1, 2]}}}) |
| 188 | + test_contains_null = check(succeed([1, None, 2]), { |
| 189 | + 'data': {'nest': None}, |
| 190 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 191 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 192 | + }) |
| 193 | + |
| 194 | + test_returns_null = check(succeed(None), { |
| 195 | + 'data': {'nest': None}, |
| 196 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 197 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 198 | + }) |
| 199 | + |
| 200 | + test_rejected = check(lambda: fail(Exception('bad')), { |
| 201 | + 'data': {'nest': None}, |
| 202 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 203 | + }) |
| 204 | + |
| 205 | + |
| 206 | +class TestNotNullListOfNotNullT_Array_Promise_T: # [T!]! Array<Promise<T>> |
| 207 | + type = GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLInt))) |
| 208 | + |
| 209 | + test_contains_values = check([succeed(1), succeed(2)], {'data': {'nest': {'test': [1, 2]}}}) |
| 210 | + test_contains_null = check([succeed(1), succeed(None), succeed(2)], { |
| 211 | + 'data': {'nest': None}, |
| 212 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], |
| 213 | + 'message': 'Cannot return null for non-nullable field DataType.test.'}] |
| 214 | + }) |
| 215 | + test_contains_reject = check(lambda: [succeed(1), fail(Exception('bad')), succeed(2)], { |
| 216 | + 'data': {'nest': None}, |
| 217 | + 'errors': [{'locations': [{'column': 10, 'line': 1}], 'message': 'bad'}] |
| 218 | + }) |
0 commit comments