Skip to content

Commit

Permalink
test(cython): fix cython tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Apr 5, 2024
1 parent 038a071 commit ce2019f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 33 deletions.
86 changes: 55 additions & 31 deletions tests/asgi/_cythonized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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': '^(.*)$'
}
}
}


Expand All @@ -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:
Expand Down Expand Up @@ -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)


Expand Down
22 changes: 22 additions & 0 deletions tests/asgi/test_cythonized_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce2019f

Please sign in to comment.