Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use recwarn rather than with pytest.warns(None) #449

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
107 changes: 54 additions & 53 deletions tests/test_etag.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,57 +196,57 @@ def test_etag_check_etag(self, app, schemas, method, etag_disabled):

@pytest.mark.parametrize("method", HTTP_METHODS)
@pytest.mark.parametrize("etag_disabled", (True, False))
def test_etag_check_etag_wrong_method_warning(self, app, method, etag_disabled):
def test_etag_check_etag_wrong_method_warning(
self, app, method, etag_disabled, recwarn
):
app.config["ETAG_DISABLED"] = etag_disabled
blp = Blueprint("test", __name__)

with pytest.warns(None) as record:
with app.test_request_context(
"/",
method=method,
headers={"If-Match": ""},
):
# Ignore ETag check fail. Just testing the warning.
try:
blp.check_etag(None)
except PreconditionFailed:
pass
if method in ["PUT", "PATCH", "DELETE"]:
assert not record
else:
assert len(record) == 1
assert record[0].category == UserWarning
assert str(record[0].message) == (
f"ETag cannot be checked on {method} request."
)
with app.test_request_context(
"/",
method=method,
headers={"If-Match": ""},
):
# Ignore ETag check fail. Just testing the warning.
try:
blp.check_etag(None)
except PreconditionFailed:
pass
if method in ["PUT", "PATCH", "DELETE"]:
assert not recwarn
else:
assert len(recwarn) == 1
assert recwarn[0].category == UserWarning
assert str(recwarn[0].message) == (
f"ETag cannot be checked on {method} request."
)

@pytest.mark.parametrize("method", HTTP_METHODS)
def test_etag_verify_check_etag_warning(self, app, method):
def test_etag_verify_check_etag_warning(self, app, method, recwarn):
blp = Blueprint("test", __name__)
old_item = {"item_id": 1, "db_field": 0}
old_etag = blp._generate_etag(old_item)

with pytest.warns(None) as record:
with app.test_request_context(
"/",
method=method,
headers={"If-Match": old_etag},
):
blp._verify_check_etag()
if method in ["PUT", "PATCH", "DELETE"]:
assert len(record) == 1
assert record[0].category == UserWarning
assert str(record[0].message) == (
"ETag not checked in endpoint {} on {} request.".format(
f_request.endpoint, method
)
with app.test_request_context(
"/",
method=method,
headers={"If-Match": old_etag},
):
blp._verify_check_etag()
if method in ["PUT", "PATCH", "DELETE"]:
assert len(recwarn) == 1
assert recwarn[0].category == UserWarning
assert str(recwarn[0].message) == (
"ETag not checked in endpoint {} on {} request.".format(
f_request.endpoint, method
)
else:
assert not record
blp.check_etag(old_item)
record.clear()
blp._verify_check_etag()
assert not record
)
else:
assert not recwarn
blp.check_etag(old_item)
recwarn.clear()
blp._verify_check_etag()
assert not recwarn

@pytest.mark.parametrize("method", HTTP_METHODS_ALLOWING_SET_ETAG)
@pytest.mark.parametrize("etag_disabled", (True, False))
Expand Down Expand Up @@ -309,21 +309,22 @@ def test_etag_set_etag(self, app, schemas, method, etag_disabled):

@pytest.mark.parametrize("etag_disabled", (True, False))
@pytest.mark.parametrize("method", HTTP_METHODS)
def test_etag_set_etag_method_not_allowed_warning(self, app, method, etag_disabled):
def test_etag_set_etag_method_not_allowed_warning(
self, app, method, etag_disabled, recwarn
):
app.config["ETAG_DISABLED"] = etag_disabled
blp = Blueprint("test", __name__)

with pytest.warns(None) as record:
with app.test_request_context("/", method=method):
blp.set_etag(None)
if method in HTTP_METHODS_ALLOWING_SET_ETAG:
assert not record
else:
assert len(record) == 1
assert record[0].category == UserWarning
assert str(record[0].message) == (
f"ETag cannot be set on {method} request."
)
with app.test_request_context("/", method=method):
blp.set_etag(None)
if method in HTTP_METHODS_ALLOWING_SET_ETAG:
assert not recwarn
else:
assert len(recwarn) == 1
assert recwarn[0].category == UserWarning
assert str(recwarn[0].message) == (
f"ETag cannot be set on {method} request."
)

@pytest.mark.parametrize("paginate", (True, False))
def test_etag_set_etag_in_response(self, app, schemas, paginate):
Expand Down
13 changes: 6 additions & 7 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def func(pagination_parameters):
}

@pytest.mark.parametrize("header_name", ("X-Pagination", None))
def test_pagination_item_count_missing(self, app, header_name):
def test_pagination_item_count_missing(self, app, header_name, recwarn):
"""If item_count was not set, pass and warn"""
api = Api(app)

Expand All @@ -215,14 +215,13 @@ def func(pagination_parameters):
api.register_blueprint(blp)
client = app.test_client()

with pytest.warns(None) as record:
response = client.get("/test/")
response = client.get("/test/")
if header_name is None:
assert not record
assert not recwarn
else:
assert len(record) == 1
assert record[0].category == UserWarning
assert str(record[0].message) == (
assert len(recwarn) == 1
assert recwarn[0].category == UserWarning
assert str(recwarn[0].message) == (
"item_count not set in endpoint test.func."
)
assert response.status_code == 200
Expand Down