From 3e9ca99ce63c9f50a30a4f6c7d9b66de47fba336 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Fri, 5 Apr 2024 22:05:57 +0200 Subject: [PATCH] test(cython): fix cython tests --- tests/asgi/_cythonized.pyx | 86 +++++++++++++++++++----------- tests/asgi/test_cythonized_asgi.py | 20 +++++++ 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/tests/asgi/_cythonized.pyx b/tests/asgi/_cythonized.pyx index 091e07358..1ea41befc 100644 --- a/tests/asgi/_cythonized.pyx +++ b/tests/asgi/_cythonized.pyx @@ -4,26 +4,33 @@ from collections import Counter import time import falcon -from falcon.media.validators.jsonschema import validate - +from falcon.media import validators +try: + import jsonschema +except ImportError: + jsonschema = None +try: + import jsonschema_rs +except ImportError: + jsonschema_rs = None _MESSAGE_SCHEMA = { - 'definitions': {}, - '$schema': 'http://json-schema.org/draft-07/schema#', - '$id': 'http://example.com/root.json', - 'type': 'object', - 'title': 'The Root Schema', - 'required': ['message'], - 'properties': { - 'message': { - '$id': '#/properties/message', - 'type': 'string', - 'title': 'The Message Schema', - 'default': '', - 'examples': ['hello world'], - 'pattern': '^(.*)$' - } - } + 'definitions': {}, + '$schema': 'http://json-schema.org/draft-07/schema#', + '$id': 'http://example.com/root.json', + 'type': 'object', + 'title': 'The Root Schema', + 'required': ['message'], + 'properties': { + 'message': { + '$id': '#/properties/message', + 'type': 'string', + 'title': 'The Message Schema', + 'default': '', + 'examples': ['hello world'], + 'pattern': '^(.*)$' + } + } } @@ -43,20 +50,37 @@ class NOPClass: pass -class TestResourceWithValidation: - @validate(resp_schema=_MESSAGE_SCHEMA, is_async=True) - async def on_get(self, req, resp): - resp.media = { - 'message': 'hello world' - } +if jsonschema: + class TestResourceWithValidation: + @validators.jsonschema.validate(resp_schema=_MESSAGE_SCHEMA, is_async=True) + async def on_get(self, req, resp): + resp.media = { + 'message': 'hello world' + } -class TestResourceWithValidationNoHint: - @validate(resp_schema=_MESSAGE_SCHEMA) - async def on_get(self, req, resp): - resp.media = { - 'message': 'hello world' - } + class TestResourceWithValidationNoHint: + @validators.jsonschema.validate(resp_schema=_MESSAGE_SCHEMA) + async def on_get(self, req, resp): + resp.media = { + 'message': 'hello world' + } + +if jsonschema_rs: + class TestResourceWithValidationRs: + @validators.jsonschema_rs.validate(resp_schema=_MESSAGE_SCHEMA, is_async=True) + async def on_get(self, req, resp): + resp.media = { + 'message': 'hello world' + } + + + class TestResourceWithValidationNoHintRs: + @validators.jsonschema_rs.validate(resp_schema=_MESSAGE_SCHEMA) + async def on_get(self, req, resp): + resp.media = { + 'message': 'hello world' + } class TestResourceWithScheduledJobs: @@ -85,7 +109,7 @@ class TestResourceWithScheduledJobsAsyncRequired: pass # NOTE(kgriffs): This will fail later since we can't detect - # up front that it isn't a coroutine function. + # up front that it isn't a coroutine function. resp.schedule(background_job_sync) diff --git a/tests/asgi/test_cythonized_asgi.py b/tests/asgi/test_cythonized_asgi.py index 744dcd952..b059b1f6c 100644 --- a/tests/asgi/test_cythonized_asgi.py +++ b/tests/asgi/test_cythonized_asgi.py @@ -83,6 +83,7 @@ def test_not_cython_func(func): @pytest.mark.skipif(not pyximport, reason='Cython not installed') +@pytest.mark.skipif('_cythonized.jsonschema is None', reason='jsonschema not installed') def test_jsonchema_validator(client): with disable_asgi_non_coroutine_wrapping(): if CYTHON_COROUTINE_HINT: @@ -98,6 +99,25 @@ def test_jsonchema_validator(client): client.simulate_get() +@pytest.mark.skipif(not pyximport, reason='Cython not installed') +@pytest.mark.skipif( + '_cythonized.jsonschema_rs is None', reason='jsonschema_rs not installed' +) +def test_jsonchema_rs_validator(client): + with disable_asgi_non_coroutine_wrapping(): + if CYTHON_COROUTINE_HINT: + client.app.add_route('/', _cythonized.TestResourceWithValidationNoHintRs()) + else: + with pytest.raises(TypeError): + client.app.add_route( + '/wowsuchfail', _cythonized.TestResourceWithValidationNoHintRs() + ) + + client.app.add_route('/', _cythonized.TestResourceWithValidationRs()) + + client.simulate_get() + + @pytest.mark.skipif(not pyximport, reason='Cython not installed') def test_scheduled_jobs(client): resource = _cythonized.TestResourceWithScheduledJobs()