Skip to content

Commit ce2019f

Browse files
committed
test(cython): fix cython tests
1 parent 038a071 commit ce2019f

File tree

3 files changed

+79
-33
lines changed

3 files changed

+79
-33
lines changed

tests/asgi/_cythonized.pyx

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@ from collections import Counter
44
import time
55

66
import falcon
7-
from falcon.media.validators.jsonschema import validate
8-
7+
from falcon.media import validators
8+
try:
9+
import jsonschema
10+
except ImportError:
11+
jsonschema = None
12+
try:
13+
import jsonschema_rs
14+
except ImportError:
15+
jsonschema_rs = None
916

1017
_MESSAGE_SCHEMA = {
11-
'definitions': {},
12-
'$schema': 'http://json-schema.org/draft-07/schema#',
13-
'$id': 'http://example.com/root.json',
14-
'type': 'object',
15-
'title': 'The Root Schema',
16-
'required': ['message'],
17-
'properties': {
18-
'message': {
19-
'$id': '#/properties/message',
20-
'type': 'string',
21-
'title': 'The Message Schema',
22-
'default': '',
23-
'examples': ['hello world'],
24-
'pattern': '^(.*)$'
25-
}
26-
}
18+
'definitions': {},
19+
'$schema': 'http://json-schema.org/draft-07/schema#',
20+
'$id': 'http://example.com/root.json',
21+
'type': 'object',
22+
'title': 'The Root Schema',
23+
'required': ['message'],
24+
'properties': {
25+
'message': {
26+
'$id': '#/properties/message',
27+
'type': 'string',
28+
'title': 'The Message Schema',
29+
'default': '',
30+
'examples': ['hello world'],
31+
'pattern': '^(.*)$'
32+
}
33+
}
2734
}
2835

2936

@@ -43,20 +50,37 @@ class NOPClass:
4350
pass
4451

4552

46-
class TestResourceWithValidation:
47-
@validate(resp_schema=_MESSAGE_SCHEMA, is_async=True)
48-
async def on_get(self, req, resp):
49-
resp.media = {
50-
'message': 'hello world'
51-
}
53+
if jsonschema:
54+
class TestResourceWithValidation:
55+
@validators.jsonschema.validate(resp_schema=_MESSAGE_SCHEMA, is_async=True)
56+
async def on_get(self, req, resp):
57+
resp.media = {
58+
'message': 'hello world'
59+
}
5260

5361

54-
class TestResourceWithValidationNoHint:
55-
@validate(resp_schema=_MESSAGE_SCHEMA)
56-
async def on_get(self, req, resp):
57-
resp.media = {
58-
'message': 'hello world'
59-
}
62+
class TestResourceWithValidationNoHint:
63+
@validators.jsonschema.validate(resp_schema=_MESSAGE_SCHEMA)
64+
async def on_get(self, req, resp):
65+
resp.media = {
66+
'message': 'hello world'
67+
}
68+
69+
if jsonschema_rs:
70+
class TestResourceWithValidationRs:
71+
@validators.jsonschema_rs.validate(resp_schema=_MESSAGE_SCHEMA, is_async=True)
72+
async def on_get(self, req, resp):
73+
resp.media = {
74+
'message': 'hello world'
75+
}
76+
77+
78+
class TestResourceWithValidationNoHintRs:
79+
@validators.jsonschema_rs.validate(resp_schema=_MESSAGE_SCHEMA)
80+
async def on_get(self, req, resp):
81+
resp.media = {
82+
'message': 'hello world'
83+
}
6084

6185

6286
class TestResourceWithScheduledJobs:
@@ -85,7 +109,7 @@ class TestResourceWithScheduledJobsAsyncRequired:
85109
pass
86110

87111
# NOTE(kgriffs): This will fail later since we can't detect
88-
# up front that it isn't a coroutine function.
112+
# up front that it isn't a coroutine function.
89113
resp.schedule(background_job_sync)
90114

91115

tests/asgi/test_cythonized_asgi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
if pyximport:
2121
from . import _cythonized # type: ignore
2222

23+
jsonschema = _cythonized.jsonschema
24+
jsonschema_rs = _cythonized.jsonschema_rs
25+
2326
_CYTHON_FUNC_TEST_TYPES = [
2427
_cythonized.nop_method,
2528
_cythonized.nop_method_async,
@@ -29,6 +32,7 @@
2932
_cythonized.NOPClass().nop_method_async,
3033
]
3134
else:
35+
jsonschema = jsonschema_rs = None
3236
_CYTHON_FUNC_TEST_TYPES = []
3337

3438
from _util import disable_asgi_non_coroutine_wrapping # NOQA
@@ -83,6 +87,7 @@ def test_not_cython_func(func):
8387

8488

8589
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
90+
@pytest.mark.skipif(jsonschema is None, reason='jsonschema not installed')
8691
def test_jsonchema_validator(client):
8792
with disable_asgi_non_coroutine_wrapping():
8893
if CYTHON_COROUTINE_HINT:
@@ -98,6 +103,23 @@ def test_jsonchema_validator(client):
98103
client.simulate_get()
99104

100105

106+
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
107+
@pytest.mark.skipif(jsonschema_rs is None, reason='jsonschema_rs not installed')
108+
def test_jsonchema_rs_validator(client):
109+
with disable_asgi_non_coroutine_wrapping():
110+
if CYTHON_COROUTINE_HINT:
111+
client.app.add_route('/', _cythonized.TestResourceWithValidationNoHintRs())
112+
else:
113+
with pytest.raises(TypeError):
114+
client.app.add_route(
115+
'/wowsuchfail', _cythonized.TestResourceWithValidationNoHintRs()
116+
)
117+
118+
client.app.add_route('/', _cythonized.TestResourceWithValidationRs())
119+
120+
client.simulate_get()
121+
122+
101123
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
102124
def test_scheduled_jobs(client):
103125
resource = _cythonized.TestResourceWithScheduledJobs()

tests/test_validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
try:
44
import jsonschema
55
except ImportError:
6-
jsonschema = None
6+
jsonschema = None # type: ignore
77
try:
88
import jsonschema_rs
99
except ImportError:
10-
jsonschema_rs = None
10+
jsonschema_rs = None # type: ignore
1111
import pytest
1212

1313
import falcon

0 commit comments

Comments
 (0)