Skip to content

Commit 3e9ca99

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

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_not_cython_func(func):
8383

8484

8585
@pytest.mark.skipif(not pyximport, reason='Cython not installed')
86+
@pytest.mark.skipif('_cythonized.jsonschema is None', reason='jsonschema not installed')
8687
def test_jsonchema_validator(client):
8788
with disable_asgi_non_coroutine_wrapping():
8889
if CYTHON_COROUTINE_HINT:
@@ -98,6 +99,25 @@ def test_jsonchema_validator(client):
9899
client.simulate_get()
99100

100101

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

0 commit comments

Comments
 (0)