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 3e9ca99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 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
20 changes: 20 additions & 0 deletions tests/asgi/test_cythonized_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit 3e9ca99

Please sign in to comment.