Skip to content

Commit

Permalink
replace pytest-asyncio and pytest-trio with anyio (#2512)
Browse files Browse the repository at this point in the history
* replace pytest-asyncio with anyio

* remove pytest-trio also

* Update setup.cfg

* use anyio.Lock in test_auth

Co-authored-by: Tom Christie <[email protected]>
  • Loading branch information
graingert and tomchristie authored Jan 2, 2023
1 parent e4438a3 commit e27d1b8
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 146 deletions.
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ isort==5.11.4
mypy==0.982
types-certifi==2021.10.8.2
pytest==7.2.0
pytest-asyncio==0.20.3
pytest-trio==0.7.0
trio==0.21.0
trio-typing==0.7.0
trustme==0.9.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ combine_as_imports = True
addopts = -rxXs
filterwarnings =
error
asyncio_mode = strict
ignore: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.:RuntimeWarning
markers =
copied_from(source, changes=None): mark test as copied from somewhere else, along with a description of changes made to accodomate e.g. our test setup
network: marks tests which require network connection. Used in 3rd-party build environments that have network disabled.
Expand Down
48 changes: 24 additions & 24 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import httpx


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_get(server):
url = server.url
async with httpx.AsyncClient(http2=True) as client:
Expand All @@ -27,14 +27,14 @@ async def test_get(server):
pytest.param("http://", id="no-host"),
],
)
@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_get_invalid_url(server, url):
async with httpx.AsyncClient() as client:
with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):
await client.get(url)


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_build_request(server):
url = server.url.copy_with(path="/echo_headers")
headers = {"Custom-header": "value"}
Expand All @@ -49,23 +49,23 @@ async def test_build_request(server):
assert response.json()["Custom-header"] == "value"


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_post(server):
url = server.url
async with httpx.AsyncClient() as client:
response = await client.post(url, content=b"Hello, world!")
assert response.status_code == 200


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_post_json(server):
url = server.url
async with httpx.AsyncClient() as client:
response = await client.post(url, json={"text": "Hello, world!"})
assert response.status_code == 200


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_stream_response(server):
async with httpx.AsyncClient() as client:
async with client.stream("GET", server.url) as response:
Expand All @@ -76,7 +76,7 @@ async def test_stream_response(server):
assert response.content == b"Hello, world!"


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_access_content_stream_response(server):
async with httpx.AsyncClient() as client:
async with client.stream("GET", server.url) as response:
Expand All @@ -87,7 +87,7 @@ async def test_access_content_stream_response(server):
response.content


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_stream_request(server):
async def hello_world() -> typing.AsyncIterator[bytes]:
yield b"Hello, "
Expand All @@ -98,7 +98,7 @@ async def hello_world() -> typing.AsyncIterator[bytes]:
assert response.status_code == 200


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_cannot_stream_sync_request(server):
def hello_world() -> typing.Iterator[bytes]: # pragma: no cover
yield b"Hello, "
Expand All @@ -109,7 +109,7 @@ def hello_world() -> typing.Iterator[bytes]: # pragma: no cover
await client.post(server.url, content=hello_world())


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_raise_for_status(server):
async with httpx.AsyncClient() as client:
for status_code in (200, 400, 404, 500, 505):
Expand All @@ -125,45 +125,45 @@ async def test_raise_for_status(server):
assert response.raise_for_status() is None # type: ignore


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_options(server):
async with httpx.AsyncClient() as client:
response = await client.options(server.url)
assert response.status_code == 200
assert response.text == "Hello, world!"


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_head(server):
async with httpx.AsyncClient() as client:
response = await client.head(server.url)
assert response.status_code == 200
assert response.text == ""


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_put(server):
async with httpx.AsyncClient() as client:
response = await client.put(server.url, content=b"Hello, world!")
assert response.status_code == 200


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_patch(server):
async with httpx.AsyncClient() as client:
response = await client.patch(server.url, content=b"Hello, world!")
assert response.status_code == 200


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_delete(server):
async with httpx.AsyncClient() as client:
response = await client.delete(server.url)
assert response.status_code == 200
assert response.text == "Hello, world!"


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_100_continue(server):
headers = {"Expect": "100-continue"}
content = b"Echo request body"
Expand All @@ -177,7 +177,7 @@ async def test_100_continue(server):
assert response.content == content


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_context_managed_transport():
class Transport(httpx.AsyncBaseTransport):
def __init__(self) -> None:
Expand Down Expand Up @@ -209,7 +209,7 @@ async def __aexit__(self, *args):
]


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_context_managed_transport_and_mount():
class Transport(httpx.AsyncBaseTransport):
def __init__(self, name: str):
Expand Down Expand Up @@ -254,7 +254,7 @@ def hello_world(request):
return httpx.Response(200, text="Hello, world!")


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_client_closed_state_using_implicit_open():
client = httpx.AsyncClient(transport=httpx.MockTransport(hello_world))

Expand All @@ -275,7 +275,7 @@ async def test_client_closed_state_using_implicit_open():
pass # pragma: no cover


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_client_closed_state_using_with_block():
async with httpx.AsyncClient(transport=httpx.MockTransport(hello_world)) as client:
assert not client.is_closed
Expand All @@ -296,7 +296,7 @@ def mounted(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json=data)


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_mounted_transport():
transport = httpx.MockTransport(unmounted)
mounts = {"custom://": httpx.MockTransport(mounted)}
Expand All @@ -311,7 +311,7 @@ async def test_mounted_transport():
assert response.json() == {"app": "mounted"}


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_async_mock_transport():
async def hello_world(request):
return httpx.Response(200, text="Hello, world!")
Expand All @@ -324,7 +324,7 @@ async def hello_world(request):
assert response.text == "Hello, world!"


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_cancellation_during_stream():
"""
If any BaseException is raised during streaming the response, then the
Expand Down Expand Up @@ -364,7 +364,7 @@ async def aclose(self) -> None:
assert stream_was_closed


@pytest.mark.usefixtures("async_environment")
@pytest.mark.anyio
async def test_server_extensions(server):
url = server.url
async with httpx.AsyncClient(http2=True) as client:
Expand Down
Loading

0 comments on commit e27d1b8

Please sign in to comment.