Skip to content

Commit 65f23c9

Browse files
committed
Make Source objects comparable
1 parent a0d1afb commit 65f23c9

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/graphql/language/source.py

+8
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ def get_location(self, position: int) -> SourceLocation:
5656

5757
def __repr__(self):
5858
return f"<{self.__class__.__name__} name={self.name!r}>"
59+
60+
def __eq__(self, other):
61+
return (isinstance(other, Source) and other.body == self.body) or (
62+
isinstance(other, str) and other == self.body
63+
)
64+
65+
def __ne__(self, other):
66+
return not self == other

tests/execution/test_sync.py

+6-16
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
from graphql.execution import execute
88
from graphql.language import parse
99
from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
10+
from graphql.validation import validate
1011

1112

1213
def describe_execute_synchronously_when_possible():
13-
def _resolve_sync(root_value, info_):
14+
def _resolve_sync(root_value, _info):
1415
return root_value
1516

16-
async def _resolve_async(root_value, info_):
17+
async def _resolve_async(root_value, _info):
1718
return root_value
1819

1920
schema = GraphQLSchema(
@@ -77,20 +78,9 @@ def does_not_return_a_promise_for_syntax_errors():
7778

7879
def does_not_return_a_promise_for_validation_errors():
7980
doc = "fragment Example on Query { unknownField }"
80-
assert graphql_sync(schema, doc) == (
81-
None,
82-
[
83-
{
84-
"message": "Cannot query field 'unknownField' on type 'Query'."
85-
" Did you mean 'asyncField' or 'syncField'?",
86-
"locations": [(1, 29)],
87-
},
88-
{
89-
"message": "Fragment 'Example' is never used.",
90-
"locations": [(1, 1)],
91-
},
92-
],
93-
)
81+
validation_errors = validate(schema, parse(doc))
82+
result = graphql_sync(schema, doc)
83+
assert result == (None, validation_errors)
9484

9585
def raises_a_type_error_when_no_query_is_passed():
9686
with raises(TypeError) as exc_info:

tests/language/test_source.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ def can_be_stringified():
1111
source = Source("", "Custom source name")
1212
assert str(source) == "<Source name='Custom source name'>"
1313

14+
def can_be_compared():
15+
source = Source("foo")
16+
assert source == source
17+
assert not source != source
18+
assert source == "foo"
19+
assert not source != "foo"
20+
same_source = Source("foo")
21+
assert source == same_source
22+
assert not source != same_source
23+
different_source = Source("bar")
24+
assert not source == different_source
25+
assert source != different_source
26+
assert not source == "bar"
27+
assert source != "bar"
28+
1429
def rejects_invalid_location_offset():
1530
def create_source(location_offset):
1631
return Source("", "", location_offset)

0 commit comments

Comments
 (0)