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