Fix assert no warning for pytest>=8

This commit is contained in:
Kien Dang 2024-04-16 12:00:48 +08:00
parent fede977f8a
commit d03822ed83

View File

@ -1,3 +1,4 @@
import warnings
from collections import OrderedDict, defaultdict
from textwrap import dedent
from unittest.mock import patch
@ -399,7 +400,7 @@ def test_django_objecttype_fields_exist_on_model():
with pytest.warns(
UserWarning,
match=r"Field name .* matches an attribute on Django model .* but it's not a model field",
) as record:
):
class Reporter2(DjangoObjectType):
class Meta:
@ -407,7 +408,8 @@ def test_django_objecttype_fields_exist_on_model():
fields = ["first_name", "some_method", "email"]
# Don't warn if selecting a custom field
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
class Reporter3(DjangoObjectType):
custom_field = String()
@ -416,8 +418,6 @@ def test_django_objecttype_fields_exist_on_model():
model = ReporterModel
fields = ["first_name", "custom_field", "email"]
assert len(record) == 0
@with_local_registry
def test_django_objecttype_exclude_fields_exist_on_model():
@ -445,15 +445,14 @@ def test_django_objecttype_exclude_fields_exist_on_model():
exclude = ["custom_field"]
# Don't warn on exclude fields
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
class Reporter4(DjangoObjectType):
class Meta:
model = ReporterModel
exclude = ["email", "first_name"]
assert len(record) == 0
@with_local_registry
def test_django_objecttype_neither_fields_nor_exclude():
@ -467,24 +466,22 @@ def test_django_objecttype_neither_fields_nor_exclude():
class Meta:
model = ReporterModel
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
class Reporter2(DjangoObjectType):
class Meta:
model = ReporterModel
fields = ["email"]
assert len(record) == 0
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
class Reporter3(DjangoObjectType):
class Meta:
model = ReporterModel
exclude = ["email"]
assert len(record) == 0
def custom_enum_name(field):
return f"CustomEnum{field.name.title()}"