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..e6bf157da 100644 --- a/tests/asgi/test_cythonized_asgi.py +++ b/tests/asgi/test_cythonized_asgi.py @@ -20,6 +20,9 @@ if pyximport: from . import _cythonized # type: ignore + jsonschema = _cythonized.jsonschema + jsonschema_rs = _cythonized.jsonschema_rs + _CYTHON_FUNC_TEST_TYPES = [ _cythonized.nop_method, _cythonized.nop_method_async, @@ -29,6 +32,7 @@ _cythonized.NOPClass().nop_method_async, ] else: + jsonschema = jsonschema_rs = None _CYTHON_FUNC_TEST_TYPES = [] from _util import disable_asgi_non_coroutine_wrapping # NOQA @@ -83,6 +87,7 @@ def test_not_cython_func(func): @pytest.mark.skipif(not pyximport, reason='Cython not installed') +@pytest.mark.skipif(jsonschema is None, reason='jsonschema not installed') def test_jsonchema_validator(client): with disable_asgi_non_coroutine_wrapping(): if CYTHON_COROUTINE_HINT: @@ -98,6 +103,23 @@ def test_jsonchema_validator(client): client.simulate_get() +@pytest.mark.skipif(not pyximport, reason='Cython not installed') +@pytest.mark.skipif(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() diff --git a/tests/test_validators.py b/tests/test_validators.py index 4fed20828..e55587825 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -3,11 +3,11 @@ try: import jsonschema except ImportError: - jsonschema = None + jsonschema = None # type: ignore try: import jsonschema_rs except ImportError: - jsonschema_rs = None + jsonschema_rs = None # type: ignore import pytest import falcon