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