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

Test python 3.10 #140

Merged
merged 1 commit into from
Mar 31, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
docs_requirements = ("mkdocs", "mkdocs-material", "mkautodoc>=0.1.0")


@nox.session(python=["3.6", "3.7", "3.8", "3.9"])
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
def test(session):
session.install("--upgrade", "pytest", "pytest-asyncio", "pytest-cov", "trio")
session.install("-e", ".")
Expand Down
201 changes: 103 additions & 98 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import asyncio
import re

import httpx
import pytest
import trio
from httpcore._backends.asyncio import AsyncioBackend
from httpcore._backends.trio import TrioBackend

import respx
from respx.router import MockRouter
Expand All @@ -21,97 +17,106 @@ async def test_named_route():
assert respx_mock["foobar"] is request


@pytest.mark.parametrize("Backend", [AsyncioBackend, TrioBackend])
def test_stats(Backend):
@respx.mock
async def test(backend):
url = "https://foo.bar/1/"
respx.get(re.compile("https://some.thing"))
respx.delete("https://some.thing")

foobar1 = respx.get(url, name="get_foobar") % dict(status_code=202, text="get")
foobar2 = respx.delete(url, name="del_foobar") % dict(text="del")

assert foobar1.called is False
assert foobar1.call_count == len(foobar1.calls)
assert foobar1.call_count == 0
assert foobar1.calls.last is None
assert respx.calls.call_count == len(respx.calls)
assert respx.calls.call_count == 0

async with httpx.AsyncClient() as client:
get_response = await client.get(url)
del_response = await client.delete(url)

assert foobar1.called is True
assert foobar2.called is True
assert foobar1.call_count == 1
assert foobar2.call_count == 1
assert foobar1.calls.call_count == 1

_request, _response = foobar1.calls[-1]
assert isinstance(_request, httpx.Request)
assert isinstance(_response, httpx.Response)
assert foobar1.calls.last.request is _request
assert foobar1.calls.last.response is _response
assert _request.method == "GET"
assert _request.url == url
assert _response.status_code == get_response.status_code == 202
assert _response.content == get_response.content == b"get"
assert {
_response.status_code,
tuple(_response.headers.raw),
_response.stream,
tuple(_response.ext.items()),
} == {
get_response.status_code,
tuple(get_response.headers.raw),
get_response.stream,
tuple(get_response.ext.items()),
}
assert id(_response) != id(get_response)

_request, _response = foobar2.calls[-1]
assert isinstance(_request, httpx.Request)
assert isinstance(_response, httpx.Response)
assert _request.method == "DELETE"
assert _request.url == url
assert _response.status_code == del_response.status_code == 200
assert _response.content == del_response.content == b"del"
assert {
_response.status_code,
tuple(_response.headers.raw),
_response.stream,
tuple(_response.ext.items()),
} == {
del_response.status_code,
tuple(del_response.headers.raw),
del_response.stream,
tuple(del_response.ext.items()),
}
assert id(_response) != id(del_response)

assert respx.calls.call_count == 2
assert respx.calls[0] == foobar1.calls[-1]
assert respx.calls[1] == foobar2.calls[-1]

assert respx.mock.calls.call_count == 2
assert respx.calls.call_count == 2

route = respx.routes["get_foobar"]
assert route == foobar1
assert route.name == foobar1.name

route = respx.routes["del_foobar"]
assert route == foobar2
assert route.name == foobar2.name

backend = Backend()
if isinstance(backend, TrioBackend):
trio.run(test, backend)
else:
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(test(backend))
finally:
loop.close()
@respx.mock
async def backend_test(backend):
url = "https://foo.bar/1/"
respx.get(re.compile("https://some.thing"))
respx.delete("https://some.thing")

foobar1 = respx.get(url, name="get_foobar") % dict(status_code=202, text="get")
foobar2 = respx.delete(url, name="del_foobar") % dict(text="del")

assert foobar1.called is False
assert foobar1.call_count == len(foobar1.calls)
assert foobar1.call_count == 0
assert foobar1.calls.last is None
assert respx.calls.call_count == len(respx.calls)
assert respx.calls.call_count == 0

async with httpx.AsyncClient() as client:
get_response = await client.get(url)
del_response = await client.delete(url)

assert foobar1.called is True
assert foobar2.called is True
assert foobar1.call_count == 1
assert foobar2.call_count == 1
assert foobar1.calls.call_count == 1

_request, _response = foobar1.calls[-1]
assert isinstance(_request, httpx.Request)
assert isinstance(_response, httpx.Response)
assert foobar1.calls.last.request is _request
assert foobar1.calls.last.response is _response
assert _request.method == "GET"
assert _request.url == url
assert _response.status_code == get_response.status_code == 202
assert _response.content == get_response.content == b"get"
assert {
_response.status_code,
tuple(_response.headers.raw),
_response.stream,
tuple(_response.ext.items()),
} == {
get_response.status_code,
tuple(get_response.headers.raw),
get_response.stream,
tuple(get_response.ext.items()),
}
assert id(_response) != id(get_response)

_request, _response = foobar2.calls[-1]
assert isinstance(_request, httpx.Request)
assert isinstance(_response, httpx.Response)
assert _request.method == "DELETE"
assert _request.url == url
assert _response.status_code == del_response.status_code == 200
assert _response.content == del_response.content == b"del"
assert {
_response.status_code,
tuple(_response.headers.raw),
_response.stream,
tuple(_response.ext.items()),
} == {
del_response.status_code,
tuple(del_response.headers.raw),
del_response.stream,
tuple(del_response.ext.items()),
}
assert id(_response) != id(del_response)

assert respx.calls.call_count == 2
assert respx.calls[0] == foobar1.calls[-1]
assert respx.calls[1] == foobar2.calls[-1]

assert respx.mock.calls.call_count == 2
assert respx.calls.call_count == 2

route = respx.routes["get_foobar"]
assert route == foobar1
assert route.name == foobar1.name

route = respx.routes["del_foobar"]
assert route == foobar2
assert route.name == foobar2.name


def test_asyncio():
import asyncio

from httpcore._backends.asyncio import AsyncioBackend

backend = AsyncioBackend()
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(backend_test(backend))
finally:
loop.close()


def test_trio(): # pragma: nocover
import trio
from httpcore._backends.trio import TrioBackend

backend = TrioBackend()
trio.run(backend_test, backend)