Skip to content

Commit 7c72a38

Browse files
committed
Improve coverage in tests
1 parent 2744051 commit 7c72a38

21 files changed

+84
-87
lines changed

Diff for: .coveragerc

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ omit =
88
[report]
99
exclude_lines =
1010
pragma: no cover
11-
pragma: false coverage
1211
raise NotImplementedError
1312
raise TypeError\(f?"Unexpected
13+
assert False,
1414
if MYPY:
1515
if TYPE_CHECKING:
16-
^\s+...$
16+
^\s+\.\.\.$
1717
^\s+pass$
1818
ignore_errors = True

Diff for: src/graphql/execution/execute.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def collect_fields(
487487
fields,
488488
visited_fragment_names,
489489
)
490-
elif isinstance(selection, FragmentSpreadNode): # pragma: no cover
490+
elif isinstance(selection, FragmentSpreadNode): # pragma: no cover else
491491
frag_name = selection.name.value
492492
if frag_name in visited_fragment_names or not self.should_include_node(
493493
selection

Diff for: src/graphql/execution/values.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def coerce_variable_values(
9696
coerced_values[var_name] = value_from_ast(
9797
var_def_node.default_value, var_type
9898
)
99-
elif is_non_null_type(var_type): # pragma: false coverage
99+
elif is_non_null_type(var_type): # pragma: no cover else
100100
var_type_str = inspect(var_type)
101101
on_error(
102102
GraphQLError(
@@ -163,13 +163,13 @@ def get_argument_values(
163163
if argument_node is None:
164164
if arg_def.default_value is not Undefined:
165165
coerced_values[arg_def.out_name or name] = arg_def.default_value
166-
elif is_non_null_type(arg_type):
166+
elif is_non_null_type(arg_type): # pragma: no cover else
167167
raise GraphQLError(
168168
f"Argument '{name}' of required type '{arg_type}'"
169169
" was not provided.",
170170
node,
171171
)
172-
continue # pragma: false coverage
172+
continue # pragma: no cover
173173

174174
value_node = argument_node.value
175175
is_null = isinstance(argument_node.value, NullValueNode)
@@ -179,14 +179,14 @@ def get_argument_values(
179179
if variable_values is None or variable_name not in variable_values:
180180
if arg_def.default_value is not Undefined:
181181
coerced_values[arg_def.out_name or name] = arg_def.default_value
182-
elif is_non_null_type(arg_type):
182+
elif is_non_null_type(arg_type): # pragma: no cover else
183183
raise GraphQLError(
184184
f"Argument '{name}' of required type '{arg_type}'"
185185
f" was provided the variable '${variable_name}'"
186186
" which was not provided a runtime value.",
187187
value_node,
188188
)
189-
continue # pragma: false coverage
189+
continue # pragma: no cover
190190
is_null = variable_values[variable_name] is None
191191

192192
if is_null and is_non_null_type(arg_type):

Diff for: src/graphql/utilities/build_client_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def get_interface_type(type_ref: Dict) -> GraphQLInterfaceType:
122122
def build_type(type_: Dict) -> GraphQLNamedType:
123123
if type_ and "name" in type_ and "kind" in type_:
124124
builder = type_builders.get(cast(str, type_["kind"]))
125-
if builder:
125+
if builder: # pragma: no cover else
126126
return cast(GraphQLNamedType, builder(type_))
127127
raise TypeError(
128128
"Invalid or incomplete introspection result."

Diff for: src/graphql/utilities/coerce_input_value.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def coerce_input_value(
9898
if field.default_value is not Undefined:
9999
# Use out name as name if it exists (extension of GraphQL.js).
100100
coerced_dict[field.out_name or field_name] = field.default_value
101-
elif is_non_null_type(field.type):
101+
elif is_non_null_type(field.type): # pragma: no cover else
102102
type_str = inspect(field.type)
103103
on_error(
104104
path.as_list() if path else [],

Diff for: src/graphql/utilities/introspection_from_schema.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def introspection_from_schema(
2929
result = execute(schema, document)
3030
if not isinstance(result, ExecutionResult): # pragma: no cover
3131
raise RuntimeError("Introspection cannot be executed")
32-
if result.errors or not result.data:
33-
raise result.errors[0] if result.errors else GraphQLError(
34-
"Introspection did not return a result"
35-
)
32+
if result.errors: # pragma: no cover
33+
raise result.errors[0]
34+
if not result.data: # pragma: no cover
35+
raise GraphQLError("Introspection did not return a result")
3636
return result.data

Diff for: src/graphql/utilities/value_from_ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def value_from_ast(
110110
if field.default_value is not Undefined:
111111
# Use out name as name if it exists (extension of GraphQL.js).
112112
coerced_obj[field.out_name or field_name] = field.default_value
113-
elif is_non_null_type(field.type): # pragma: false coverage
113+
elif is_non_null_type(field.type): # pragma: no cover else
114114
return Undefined
115115
continue
116116
field_value = value_from_ast(field_node.value, field.type, variables)

Diff for: src/graphql/validation/rules/known_directives.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,18 @@ def enter_directive(self, node: DirectiveNode, _key, _parent, _path, ancestors):
9191

9292
def get_directive_location_for_ast_path(ancestors):
9393
applied_to = ancestors[-1]
94-
if isinstance(applied_to, Node):
95-
kind = applied_to.kind
96-
if kind == "operation_definition":
97-
applied_to = cast(OperationDefinitionNode, applied_to)
98-
return _operation_location[applied_to.operation.value]
99-
elif kind == "input_value_definition":
100-
parent_node = ancestors[-3]
101-
return (
102-
DirectiveLocation.INPUT_FIELD_DEFINITION
103-
if parent_node.kind == "input_object_type_definition"
104-
else DirectiveLocation.ARGUMENT_DEFINITION
105-
)
106-
else:
107-
return _directive_location.get(kind)
94+
if not isinstance(applied_to, Node): # pragma: no cover
95+
raise TypeError("Unexpected error in directive.")
96+
kind = applied_to.kind
97+
if kind == "operation_definition":
98+
applied_to = cast(OperationDefinitionNode, applied_to)
99+
return _operation_location[applied_to.operation.value]
100+
elif kind == "input_value_definition":
101+
parent_node = ancestors[-3]
102+
return (
103+
DirectiveLocation.INPUT_FIELD_DEFINITION
104+
if parent_node.kind == "input_object_type_definition"
105+
else DirectiveLocation.ARGUMENT_DEFINITION
106+
)
107+
else:
108+
return _directive_location.get(kind)

Diff for: src/graphql/validation/rules/overlapping_fields_can_be_merged.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def collect_fields_and_fragment_names(
690690
)
691691
elif isinstance(selection, FragmentSpreadNode):
692692
fragment_names[selection.name.value] = True
693-
elif isinstance(selection, InlineFragmentNode):
693+
elif isinstance(selection, InlineFragmentNode): # pragma: no cover else
694694
type_condition = selection.type_condition
695695
inline_fragment_type = (
696696
type_from_ast(context.schema, type_condition)

Diff for: tests/pyutils/test_inspect.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def overly_large_int():
100100
assert inspect(n) == r
101101

102102
def inspect_function():
103-
assert inspect(lambda: 0) == "<function>"
103+
assert inspect(lambda: 0) == "<function>" # pragma: no cover
104104

105105
def test_func():
106-
return None
106+
pass
107107

108108
assert inspect(test_func) == "<function test_func>"
109109

@@ -114,7 +114,7 @@ def inspect_exception():
114114
def inspect_class_and_method():
115115
class TestClass:
116116
def test_method(self):
117-
return None
117+
pass
118118

119119
assert inspect(TestClass) == "<class TestClass>"
120120
assert inspect(TestClass()) == "<TestClass instance>"
@@ -132,15 +132,15 @@ class TestClass(metaclass=MetaClass):
132132

133133
def inspect_generator():
134134
def test_generator():
135-
yield None
135+
yield None # pragma: no cover
136136

137137
assert inspect(test_generator) == "<generator function test_generator>"
138138
assert inspect(test_generator()) == "<generator test_generator>"
139139

140140
@mark.asyncio
141141
async def inspect_coroutine():
142142
async def test_coroutine():
143-
return None
143+
pass
144144

145145
assert inspect(test_coroutine) == "<coroutine function test_coroutine>"
146146
coroutine_object = test_coroutine()
@@ -149,7 +149,7 @@ async def test_coroutine():
149149

150150
def inspect_async_generator():
151151
async def test_async_generator():
152-
yield None
152+
yield None # pragma: no cover
153153

154154
assert inspect(test_async_generator) == (
155155
"<async generator function test_async_generator>"

Diff for: tests/pyutils/test_is_nullish.py

-16
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@
33
from graphql.pyutils import is_nullish, Undefined
44

55

6-
class FakeNumpyArray:
7-
def __eq__(self, other):
8-
# Numpy arrays return an array when compared with another numpy array
9-
# containing the pointwise equality of the two
10-
if isinstance(other, FakeNumpyArray):
11-
return FakeNumpyArray()
12-
else:
13-
return False
14-
15-
def __bool__(self):
16-
raise TypeError(
17-
"The truth value of an array with more than one element is "
18-
"ambiguous. Use a.any() or a.all()"
19-
)
20-
21-
226
def describe_is_nullish():
237
def null_is_nullish():
248
assert is_nullish(None) is True

Diff for: tests/subscription/test_map_async_iterator.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,18 @@ async def __anext__(self):
310310
raise StopAsyncIteration
311311
return self.counter
312312

313+
def double(x):
314+
return x + x
315+
313316
for iterator in source, Source:
314-
doubles = MapAsyncIterator(iterator(), lambda x: x + x)
317+
doubles = MapAsyncIterator(iterator(), double)
315318

316319
await doubles.aclose()
317320

318321
with raises(StopAsyncIteration):
319322
await anext(doubles)
320323

321-
doubles = MapAsyncIterator(iterator(), lambda x: x + x)
324+
doubles = MapAsyncIterator(iterator(), double)
322325

323326
assert await anext(doubles) == 2
324327
assert await anext(doubles) == 4
@@ -327,7 +330,7 @@ async def __anext__(self):
327330
with raises(StopAsyncIteration):
328331
await anext(doubles)
329332

330-
doubles = MapAsyncIterator(iterator(), lambda x: x + x)
333+
doubles = MapAsyncIterator(iterator(), double)
331334

332335
assert await anext(doubles) == 2
333336
assert await anext(doubles) == 4
@@ -350,7 +353,7 @@ async def __anext__(self):
350353

351354
await doubles.aclose()
352355

353-
doubles = MapAsyncIterator(iterator(), lambda x: x + x)
356+
doubles = MapAsyncIterator(iterator(), double)
354357

355358
assert await anext(doubles) == 2
356359
assert await anext(doubles) == 4

Diff for: tests/subscription/test_subscribe.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async def accepts_positional_arguments():
139139

140140
async def empty_async_iterator(_info):
141141
for value in (): # type: ignore
142-
yield value
142+
yield value # pragma: no cover
143143

144144
ai = await subscribe(
145145
email_schema, document, {"importantEmail": empty_async_iterator}
@@ -255,7 +255,7 @@ def subscribe_important(_inbox, _info):
255255
did_resolve["importantEmail"] = True
256256
return EventEmitterAsyncIterator(EventEmitter(), "event")
257257

258-
def subscribe_non_important(_inbox, _info):
258+
def subscribe_non_important(_inbox, _info): # pragma: no cover
259259
did_resolve["nonImportantEmail"] = True
260260
return EventEmitterAsyncIterator(EventEmitter(), "event")
261261

Diff for: tests/test_docs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def get_snippets(source, indent=4):
1212
"""Get all code snippets from a given documentation source file."""
13-
if not source.endswith(".rst"):
13+
if not source.endswith(".rst"): # pragma: no cover
1414
source += ".rst"
1515
source_path = Path(__file__).parents[1] / "docs" / source
1616
lines = open(source_path).readlines()

Diff for: tests/test_version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def version_info_has_correct_fields():
7474
assert version_info.major == int(groups[0])
7575
assert version_info.minor == int(groups[1])
7676
assert version_info.micro == int(groups[2])
77-
if groups[3] is None:
77+
if groups[3] is None: # pragma: no cover
7878
assert groups[4] is None
7979
else:
8080
assert version_info.releaselevel[:1] == groups[3]
@@ -100,7 +100,7 @@ def version_info_js_has_correct_fields():
100100
assert version_info_js.major == int(groups[0])
101101
assert version_info_js.minor == int(groups[1])
102102
assert version_info_js.micro == int(groups[2])
103-
if groups[3] is None:
103+
if groups[3] is None: # pragma: no cover
104104
assert groups[4] is None
105105
else:
106106
assert version_info_js.releaselevel[:1] == groups[3]

0 commit comments

Comments
 (0)