Skip to content

Commit e670ed4

Browse files
committed
Use match parameter in pytest.raises where appropriate
1 parent 9c50094 commit e670ed4

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

tests/subscription/test_subscribe.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,10 @@ async def throws_an_error_if_schema_is_missing():
306306

307307
assert str(exc_info.value) == "Expected None to be a GraphQL schema."
308308

309-
with raises(TypeError) as exc_info:
309+
with raises(TypeError, match="missing .* positional argument: 'schema'"):
310310
# noinspection PyTypeChecker
311311
await subscribe(document=document)
312312

313-
msg = str(exc_info.value)
314-
assert "missing" in msg and "argument: 'schema'" in msg
315-
316313
# noinspection PyArgumentList
317314
@mark.asyncio
318315
async def throws_an_error_if_document_is_missing():
@@ -322,13 +319,10 @@ async def throws_an_error_if_document_is_missing():
322319

323320
assert str(exc_info.value) == "Must provide document"
324321

325-
with raises(TypeError) as exc_info:
322+
with raises(TypeError, match="missing .* positional argument: 'document'"):
326323
# noinspection PyTypeChecker
327324
await subscribe(schema=email_schema)
328325

329-
msg = str(exc_info.value)
330-
assert "missing" in msg and "argument: 'document'" in msg
331-
332326
@mark.asyncio
333327
async def resolves_to_an_error_for_unknown_subscription_field():
334328
ast = parse(

tests/type/test_definition.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ def rejects_an_object_type_with_incorrectly_typed_field_args():
403403

404404
def does_not_accept_is_deprecated_instead_of_deprecation_reason_on_field():
405405
kwargs = dict(is_deprecated=True)
406-
with raises(TypeError) as exc_info:
406+
with raises(
407+
TypeError, match="got an unexpected keyword argument 'is_deprecated'"
408+
):
407409
GraphQLObjectType(
408410
"OldObject", {"field": GraphQLField(GraphQLString, **kwargs)}
409411
)
410-
msg = str(exc_info.value)
411-
assert "got an unexpected keyword argument 'is_deprecated'" in msg
412412

413413

414414
def describe_object_interfaces_must_be_a_sequence():
@@ -558,11 +558,11 @@ def accepts_a_scalar_type_defining_serialize():
558558
schema_with_field_type(GraphQLScalarType("SomeScalar", lambda: None))
559559

560560
def rejects_a_scalar_type_not_defining_serialize():
561-
with raises(TypeError) as exc_info:
561+
with raises(
562+
TypeError, match="missing 1 required positional argument: 'serialize'"
563+
):
562564
# noinspection PyArgumentList
563565
schema_with_field_type(GraphQLScalarType("SomeScalar"))
564-
msg = str(exc_info.value)
565-
assert "missing 1 required positional argument: 'serialize'" in msg
566566
with raises(TypeError) as exc_info:
567567
# noinspection PyTypeChecker
568568
schema_with_field_type(GraphQLScalarType("SomeScalar", None))
@@ -663,11 +663,9 @@ def accepts_a_union_type_with_function_returning_a_list_of_types():
663663
schema_with_field_type(GraphQLUnionType("SomeUnion", lambda: [ObjectType]))
664664

665665
def accepts_a_union_type_without_types():
666-
with raises(TypeError) as exc_info:
666+
with raises(TypeError, match="missing 1 required positional argument: 'types'"):
667667
# noinspection PyArgumentList
668668
schema_with_field_type(GraphQLUnionType("SomeUnion"))
669-
msg = str(exc_info.value)
670-
assert "missing 1 required positional argument: 'types'" in msg
671669
schema_with_field_type(GraphQLUnionType("SomeUnion", None))
672670
schema_with_field_type(GraphQLUnionType("SomeUnion", []))
673671

@@ -726,14 +724,12 @@ def rejects_an_input_object_type_with_incorrect_fields_function():
726724

727725
def describe_type_system_input_objects_fields_must_not_have_resolvers():
728726
def rejects_an_input_object_type_with_resolvers():
729-
with raises(TypeError) as exc_info:
727+
with raises(TypeError, match="got an unexpected keyword argument 'resolve'"):
730728
# noinspection PyArgumentList
731729
GraphQLInputObjectType(
732730
"SomeInputObject",
733731
{"f": GraphQLInputField(GraphQLString, resolve=lambda: 0)},
734732
)
735-
msg = str(exc_info.value)
736-
assert "got an unexpected keyword argument 'resolve'" in msg
737733
input_obj_type = GraphQLInputObjectType(
738734
"SomeInputObject", {"f": GraphQLField(GraphQLString, resolve=lambda: 0)}
739735
)
@@ -746,13 +742,11 @@ def rejects_an_input_object_type_with_resolvers():
746742
)
747743

748744
def rejects_an_input_object_type_with_resolver_constant():
749-
with raises(TypeError) as exc_info:
745+
with raises(TypeError, match="got an unexpected keyword argument 'resolve'"):
750746
# noinspection PyArgumentList
751747
GraphQLInputObjectType(
752748
"SomeInputObject", {"f": GraphQLInputField(GraphQLString, resolve={})}
753749
)
754-
msg = str(exc_info.value)
755-
assert "got an unexpected keyword argument 'resolve'" in msg
756750

757751

758752
def describe_type_system_enum_types_must_be_well_defined():
@@ -781,11 +775,11 @@ def rejects_an_enum_type_with_incorrectly_typed_values():
781775
)
782776

783777
def does_not_allow_is_deprecated():
784-
with raises(TypeError) as exc_info:
778+
with raises(
779+
TypeError, match="got an unexpected keyword argument 'is_deprecated'"
780+
):
785781
# noinspection PyArgumentList
786782
GraphQLEnumType("SomeEnum", {"FOO": GraphQLEnumValue(is_deprecated=True)})
787-
msg = str(exc_info.value)
788-
assert "got an unexpected keyword argument 'is_deprecated'" in msg
789783

790784

791785
def describe_type_system_list_must_accept_only_types():

tests/utilities/test_assert_valid_name.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,5 @@ def throws_for_non_strings():
2222
assert msg == "Expected string"
2323

2424
def throws_for_names_with_invalid_characters():
25-
with raises(GraphQLError) as exc_info:
25+
with raises(GraphQLError, match="Names must match"):
2626
assert_valid_name(">--()-->")
27-
msg = exc_info.value.message
28-
assert "Names must match" in msg

0 commit comments

Comments
 (0)