Skip to content

Commit

Permalink
chore(tests): adapt ASGI tests to Cython 3.0 (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
vytas7 committed Jul 18, 2023
1 parent 69cdcd6 commit 0e96f88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
9 changes: 2 additions & 7 deletions tests/asgi/_cythonized.pyx
Expand Up @@ -105,13 +105,8 @@ class TestResourceWithHooks:
pass


class TestResourceWithHooksNoHintBefore:
class TestResourceWithHooksNoHint:
@falcon.before(my_before_hook)
async def on_get(self, req, resp):
pass


class TestResourceWithHooksNoHintBefore:
@falcon.after(my_before_hook)
@falcon.after(my_after_hook)
async def on_get(self, req, resp):
pass
36 changes: 20 additions & 16 deletions tests/asgi/test_cythonized_asgi.py
Expand Up @@ -34,6 +34,12 @@
from _util import disable_asgi_non_coroutine_wrapping # NOQA


# NOTE(vytas): Cython 3.0+ now correctly marks cythonized coroutines as such,
# however, the relevant protocol is only available in Python 3.10+.
# See also: https://github.com/cython/cython/pull/3427
CYTHON_COROUTINE_HINT = sys.version_info >= (3, 10)


@pytest.fixture
def client():
return testing.TestClient(falcon.asgi.App())
Expand Down Expand Up @@ -79,12 +85,15 @@ def test_not_cython_func(func):
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
def test_jsonchema_validator(client):
with disable_asgi_non_coroutine_wrapping():
client.app.add_route('/', _cythonized.TestResourceWithValidation())
if CYTHON_COROUTINE_HINT:
client.app.add_route('/', _cythonized.TestResourceWithValidationNoHint())
else:
with pytest.raises(TypeError):
client.app.add_route(
'/wowsuchfail', _cythonized.TestResourceWithValidationNoHint()
)

with pytest.raises(TypeError):
client.app.add_route(
'/wowsuchfail', _cythonized.TestResourceWithValidationNoHint()
)
client.app.add_route('/', _cythonized.TestResourceWithValidation())

client.simulate_get()

Expand All @@ -101,13 +110,6 @@ def test_scheduled_jobs(client):


@pytest.mark.skipif(not pyximport, reason='Cython not installed')
@pytest.mark.skipif(
sys.version_info < (3, 7),
reason=(
'CPython 3.6 does not complain when you try to call loop.create_task() '
'with the wrong type.'
),
)
def test_scheduled_jobs_type_error(client):
client.app.add_route(
'/wowsuchfail', _cythonized.TestResourceWithScheduledJobsAsyncRequired()
Expand All @@ -126,11 +128,13 @@ def test_scheduled_jobs_type_error(client):
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
def test_hooks(client):
with disable_asgi_non_coroutine_wrapping():
with pytest.raises(TypeError):
client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintBefore())
client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintAfter())
if CYTHON_COROUTINE_HINT:
client.app.add_route('/', _cythonized.TestResourceWithHooksNoHint())
else:
with pytest.raises(TypeError):
client.app.add_route('/', _cythonized.TestResourceWithHooksNoHint())

client.app.add_route('/', _cythonized.TestResourceWithHooks())
client.app.add_route('/', _cythonized.TestResourceWithHooks())

result = client.simulate_get()
assert result.headers['x-answer'] == '42'
Expand Down

0 comments on commit 0e96f88

Please sign in to comment.