Skip to content

Commit

Permalink
Merge pull request #810 from graingert/fix-tornado-related-deprecatio…
Browse files Browse the repository at this point in the history
…n-warnings

fix tornado related warnings by replacing pytest-tornado with a simple coroutine runner
  • Loading branch information
graingert committed Jan 23, 2024
2 parents 8028420 + bf80673 commit 3a5ff1c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ markers = ["online"]
filterwarnings = [
"error",
'''ignore:datetime\.datetime\.utcfromtimestamp\(\) is deprecated and scheduled for removal in a future version.*:DeprecationWarning''',
'''ignore:There is no current event loop:DeprecationWarning''',
'''ignore:make_current is deprecated; start the event loop first:DeprecationWarning''',
'''ignore:clear_current is deprecated:DeprecationWarning''',
'''ignore:the \(type, exc, tb\) signature of throw\(\) is deprecated, use the single-arg signature instead.:DeprecationWarning''',
]

[tool.ruff]
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def run_tests(self):
"pytest-asyncio",
"pytest-cov",
"pytest-httpbin",
"pytest-tornado",
"pytest",
"requests>=2.22.0",
"tornado",
Expand Down
59 changes: 38 additions & 21 deletions tests/integration/test_tornado.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test requests' interaction with vcr"""

import asyncio
import functools
import inspect
import json

import pytest
Expand All @@ -10,6 +12,7 @@
from ..assertions import assert_cassette_empty, assert_is_json_bytes

tornado = pytest.importorskip("tornado")
gen = pytest.importorskip("tornado.gen")
http = pytest.importorskip("tornado.httpclient")

# whether the current version of Tornado supports the raise_error argument for
Expand All @@ -18,6 +21,20 @@
raise_error_for_response_code_only = tornado.version_info >= (6,)


def gen_test(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
async def coro():
return await gen.coroutine(func)(*args, **kwargs)

return asyncio.run(coro())

# Patch the signature so pytest can inject fixtures
# we can't use wrapt.decorator because it returns a generator function
wrapper.__signature__ = inspect.signature(func)
return wrapper


@pytest.fixture(params=["https", "http"])
def scheme(request):
"""Fixture that returns both http and https."""
Expand Down Expand Up @@ -52,7 +69,7 @@ def post(client, url, data=None, **kwargs):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_status_code(get_client, scheme, tmpdir):
"""Ensure that we can read the status code"""
url = scheme + "://httpbin.org/"
Expand All @@ -65,7 +82,7 @@ def test_status_code(get_client, scheme, tmpdir):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_headers(get_client, scheme, tmpdir):
"""Ensure that we can read the headers back"""
url = scheme + "://httpbin.org/"
Expand All @@ -78,7 +95,7 @@ def test_headers(get_client, scheme, tmpdir):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_body(get_client, tmpdir, scheme):
"""Ensure the responses are all identical enough"""

Expand All @@ -91,7 +108,7 @@ def test_body(get_client, tmpdir, scheme):
assert 1 == cass.play_count


@pytest.mark.gen_test
@gen_test
def test_effective_url(get_client, tmpdir, httpbin):
"""Ensure that the effective_url is captured"""
url = httpbin.url + "/redirect/1"
Expand All @@ -105,7 +122,7 @@ def test_effective_url(get_client, tmpdir, httpbin):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_auth(get_client, tmpdir, scheme):
"""Ensure that we can handle basic auth"""
auth = ("user", "passwd")
Expand All @@ -121,7 +138,7 @@ def test_auth(get_client, tmpdir, scheme):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_auth_failed(get_client, tmpdir, scheme):
"""Ensure that we can save failed auth statuses"""
auth = ("user", "wrongwrongwrong")
Expand All @@ -145,7 +162,7 @@ def test_auth_failed(get_client, tmpdir, scheme):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_post(get_client, tmpdir, scheme):
"""Ensure that we can post and cache the results"""
data = {"key1": "value1", "key2": "value2"}
Expand All @@ -160,7 +177,7 @@ def test_post(get_client, tmpdir, scheme):
assert 1 == cass.play_count


@pytest.mark.gen_test
@gen_test
def test_redirects(get_client, tmpdir, httpbin):
"""Ensure that we can handle redirects"""
url = httpbin + "/redirect-to?url=bytes/1024&status_code=301"
Expand All @@ -173,7 +190,7 @@ def test_redirects(get_client, tmpdir, httpbin):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_cross_scheme(get_client, tmpdir, scheme):
"""Ensure that requests between schemes are treated separately"""
# First fetch a url under http, and then again under https and then
Expand All @@ -193,7 +210,7 @@ def test_cross_scheme(get_client, tmpdir, scheme):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_gzip(get_client, tmpdir, scheme):
"""
Ensure that httpclient is able to automatically decompress the response
Expand All @@ -219,7 +236,7 @@ def test_gzip(get_client, tmpdir, scheme):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_https_with_cert_validation_disabled(get_client, tmpdir):
cass_path = str(tmpdir.join("cert_validation_disabled.yaml"))

Expand All @@ -231,7 +248,7 @@ def test_https_with_cert_validation_disabled(get_client, tmpdir):
assert 1 == cass.play_count


@pytest.mark.gen_test
@gen_test
def test_unsupported_features_raises_in_future(get_client, tmpdir):
"""Ensure that the exception for an AsyncHTTPClient feature not being
supported is raised inside the future."""
Expand All @@ -253,7 +270,7 @@ def callback(chunk):
raise_error_for_response_code_only,
reason="raise_error only ignores HTTPErrors due to response code",
)
@pytest.mark.gen_test
@gen_test
def test_unsupported_features_raise_error_disabled(get_client, tmpdir):
"""Ensure that the exception for an AsyncHTTPClient feature not being
supported is not raised if raise_error=False."""
Expand All @@ -273,7 +290,7 @@ def callback(chunk):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_cannot_overwrite_cassette_raises_in_future(get_client, tmpdir):
"""Ensure that CannotOverwriteExistingCassetteException is raised inside
the future."""
Expand All @@ -293,7 +310,7 @@ def test_cannot_overwrite_cassette_raises_in_future(get_client, tmpdir):
raise_error_for_response_code_only,
reason="raise_error only ignores HTTPErrors due to response code",
)
@pytest.mark.gen_test
@gen_test
def test_cannot_overwrite_cassette_raise_error_disabled(get_client, tmpdir):
"""Ensure that CannotOverwriteExistingCassetteException is not raised if
raise_error=False in the fetch() call."""
Expand All @@ -307,14 +324,14 @@ def test_cannot_overwrite_cassette_raise_error_disabled(get_client, tmpdir):
assert isinstance(response.error, CannotOverwriteExistingCassetteException)


@pytest.mark.gen_test
@gen_test
@vcr.use_cassette(path_transformer=vcr.default_vcr.ensure_suffix(".yaml"))
def test_tornado_with_decorator_use_cassette(get_client):
response = yield get_client().fetch(http.HTTPRequest("http://www.google.com/", method="GET"))
assert response.body.decode("utf-8") == "not actually google"


@pytest.mark.gen_test
@gen_test
@vcr.use_cassette(path_transformer=vcr.default_vcr.ensure_suffix(".yaml"))
def test_tornado_exception_can_be_caught(get_client):
try:
Expand All @@ -329,7 +346,7 @@ def test_tornado_exception_can_be_caught(get_client):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_existing_references_get_patched(tmpdir):
from tornado.httpclient import AsyncHTTPClient

Expand All @@ -343,7 +360,7 @@ def test_existing_references_get_patched(tmpdir):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_existing_instances_get_patched(get_client, tmpdir):
"""Ensure that existing instances of AsyncHTTPClient get patched upon
entering VCR context."""
Expand All @@ -359,7 +376,7 @@ def test_existing_instances_get_patched(get_client, tmpdir):


@pytest.mark.online
@pytest.mark.gen_test
@gen_test
def test_request_time_is_set(get_client, tmpdir):
"""Ensures that the request_time on HTTPResponses is set."""

Expand Down

0 comments on commit 3a5ff1c

Please sign in to comment.