|
| 1 | +from typing import cast |
| 2 | + |
1 | 3 | from graphql.error import GraphQLError, format_error
|
2 |
| -from graphql.language import parse, Source |
| 4 | +from graphql.language import parse, OperationDefinitionNode, Source |
| 5 | +from graphql.pyutils import dedent |
| 6 | + |
| 7 | + |
| 8 | +source = Source( |
| 9 | + dedent( |
| 10 | + """ |
| 11 | + { |
| 12 | + field |
| 13 | + } |
| 14 | + """ |
| 15 | + ) |
| 16 | +) |
| 17 | + |
| 18 | +ast = parse(source) |
| 19 | +operation_node = ast.definitions[0] |
| 20 | +operation_node = cast(OperationDefinitionNode, operation_node) |
| 21 | +assert operation_node and operation_node.kind == "operation_definition" |
| 22 | +field_node = operation_node.selection_set.selections[0] |
| 23 | +assert field_node |
3 | 24 |
|
4 | 25 |
|
5 | 26 | def describe_graphql_error():
|
6 | 27 | def is_a_class_and_is_a_subclass_of_exception():
|
| 28 | + assert type(GraphQLError) is type |
7 | 29 | assert issubclass(GraphQLError, Exception)
|
8 |
| - assert isinstance(GraphQLError("msg"), GraphQLError) |
| 30 | + assert isinstance(GraphQLError("str"), Exception) |
| 31 | + assert isinstance(GraphQLError("str"), GraphQLError) |
9 | 32 |
|
10 | 33 | def has_a_name_message_and_stack_trace():
|
11 | 34 | e = GraphQLError("msg")
|
12 | 35 | assert e.__class__.__name__ == "GraphQLError"
|
13 | 36 | assert e.message == "msg"
|
14 | 37 |
|
15 |
| - def stores_the_original_error(): |
16 |
| - original = Exception("original") |
| 38 | + def uses_the_stack_of_an_original_error(): |
| 39 | + try: |
| 40 | + raise RuntimeError("original") |
| 41 | + except RuntimeError as e: |
| 42 | + original = e |
17 | 43 | e = GraphQLError("msg", original_error=original)
|
18 | 44 | assert e.__class__.__name__ == "GraphQLError"
|
| 45 | + assert e.__traceback__ is original.__traceback__ |
19 | 46 | assert e.message == "msg"
|
20 |
| - assert e.original_error == original |
| 47 | + assert e.original_error is original |
| 48 | + assert str(e.original_error) == "original" |
| 49 | + |
| 50 | + def creates_new_stack_if_original_error_has_no_stack(): |
| 51 | + try: |
| 52 | + raise RuntimeError |
| 53 | + except RuntimeError as original_with_traceback: |
| 54 | + original_traceback = original_with_traceback.__traceback__ |
| 55 | + original = RuntimeError("original") |
| 56 | + e = GraphQLError("msg", original_error=original) |
| 57 | + assert e.__class__.__name__ == "GraphQLError" |
| 58 | + assert original.__traceback__ is None |
| 59 | + assert original_traceback is not None |
| 60 | + assert e.__traceback__ is original_traceback |
| 61 | + assert e.message == "msg" |
| 62 | + assert e.original_error is original |
| 63 | + assert str(e.original_error) == "original" |
21 | 64 |
|
22 | 65 | def converts_nodes_to_positions_and_locations():
|
23 |
| - source = Source("{\n field\n}") |
24 |
| - ast = parse(source) |
25 |
| - # noinspection PyUnresolvedReferences |
26 |
| - field_node = ast.definitions[0].selection_set.selections[0] |
27 | 66 | e = GraphQLError("msg", [field_node])
|
28 | 67 | assert e.nodes == [field_node]
|
29 | 68 | assert e.source is source
|
30 |
| - assert e.positions == [8] |
31 |
| - assert e.locations == [(2, 7)] |
| 69 | + assert e.positions == [4] |
| 70 | + assert e.locations == [(2, 3)] |
32 | 71 |
|
33 | 72 | def converts_single_node_to_positions_and_locations():
|
34 |
| - source = Source("{\n field\n}") |
35 |
| - ast = parse(source) |
36 |
| - # noinspection PyUnresolvedReferences |
37 |
| - field_node = ast.definitions[0].selection_set.selections[0] |
38 | 73 | e = GraphQLError("msg", field_node) # Non-array value.
|
39 | 74 | assert e.nodes == [field_node]
|
40 | 75 | assert e.source is source
|
41 |
| - assert e.positions == [8] |
42 |
| - assert e.locations == [(2, 7)] |
| 76 | + assert e.positions == [4] |
| 77 | + assert e.locations == [(2, 3)] |
43 | 78 |
|
44 | 79 | def converts_node_with_loc_start_zero_to_positions_and_locations():
|
45 |
| - source = Source("{\n field\n}") |
46 |
| - ast = parse(source) |
47 |
| - operations_node = ast.definitions[0] |
48 |
| - e = GraphQLError("msg", [operations_node]) |
49 |
| - assert e.nodes == [operations_node] |
| 80 | + e = GraphQLError("msg", [operation_node]) |
| 81 | + assert e.nodes == [operation_node] |
50 | 82 | assert e.source is source
|
51 | 83 | assert e.positions == [0]
|
52 | 84 | assert e.locations == [(1, 1)]
|
53 | 85 |
|
54 | 86 | def converts_source_and_positions_to_locations():
|
55 |
| - source = Source("{\n field\n}") |
56 |
| - # noinspection PyArgumentEqualDefault |
57 |
| - e = GraphQLError("msg", None, source, [10]) |
| 87 | + e = GraphQLError("msg", None, source, [6]) |
58 | 88 | assert e.nodes is None
|
59 | 89 | assert e.source is source
|
60 |
| - assert e.positions == [10] |
61 |
| - assert e.locations == [(2, 9)] |
| 90 | + assert e.positions == [6] |
| 91 | + assert e.locations == [(2, 5)] |
62 | 92 |
|
63 | 93 | def serializes_to_include_message():
|
64 | 94 | e = GraphQLError("msg")
|
65 | 95 | assert str(e) == "msg"
|
66 | 96 | assert repr(e) == "GraphQLError('msg')"
|
67 | 97 |
|
68 | 98 | def serializes_to_include_message_and_locations():
|
69 |
| - # noinspection PyUnresolvedReferences |
70 |
| - node = parse("{ field }").definitions[0].selection_set.selections[0] |
71 |
| - e = GraphQLError("msg", [node]) |
| 99 | + e = GraphQLError("msg", [field_node]) |
72 | 100 | assert "msg" in str(e)
|
73 |
| - assert "(1:3)" in str(e) |
| 101 | + assert "(2:3)" in str(e) |
74 | 102 | assert repr(e) == (
|
75 |
| - "GraphQLError('msg', locations=[SourceLocation(line=1, column=3)])" |
| 103 | + "GraphQLError('msg', locations=[SourceLocation(line=2, column=3)])" |
76 | 104 | )
|
77 | 105 |
|
78 | 106 | def serializes_to_include_path():
|
79 | 107 | path = ["path", 3, "to", "field"]
|
80 |
| - # noinspection PyArgumentEqualDefault |
81 |
| - e = GraphQLError("msg", None, None, None, path) |
| 108 | + e = GraphQLError("msg", path=path) |
82 | 109 | assert e.path is path
|
83 | 110 | assert repr(e) == "GraphQLError('msg', path=['path', 3, 'to', 'field'])"
|
84 | 111 |
|
85 | 112 | def default_error_formatter_includes_path():
|
86 | 113 | path = ["path", 3, "to", "field"]
|
87 |
| - # noinspection PyArgumentEqualDefault |
88 |
| - e = GraphQLError("msg", None, None, None, path) |
| 114 | + e = GraphQLError("msg", path=path) |
89 | 115 | formatted = format_error(e)
|
90 | 116 | assert formatted == e.formatted
|
91 | 117 | assert formatted == {"message": "msg", "locations": None, "path": path}
|
92 | 118 |
|
93 | 119 | def default_error_formatter_includes_extension_fields():
|
94 |
| - # noinspection PyArgumentEqualDefault |
95 |
| - e = GraphQLError("msg", None, None, None, None, None, {"foo": "bar"}) |
| 120 | + e = GraphQLError("msg", extensions={"foo": "bar"}) |
96 | 121 | formatted = format_error(e)
|
97 | 122 | assert formatted == e.formatted
|
98 | 123 | assert formatted == {
|
|
0 commit comments