Skip to content

Commit

Permalink
refactor(tests): remove _util import in favour of fixture (#2281)
Browse files Browse the repository at this point in the history
* refactor(tests): migrate some usage of _util to util fixture

* style: remove unused imports

* refactor(tests): move more stuff out from _util

* refactor(tests): remove usage of _util.create_resp etc

* refactor(tests): significantly reduce the usage of _util

* refactor(tests): move has_cython to util

* refactor: use util in tests_middleware.py

* refactor(tests): finally remove the thing

* chore(tests): remove redundant old parametrization
  • Loading branch information
vytas7 authored Aug 21, 2024
1 parent a65d807 commit edcd5f6
Show file tree
Hide file tree
Showing 38 changed files with 354 additions and 439 deletions.
79 changes: 0 additions & 79 deletions tests/_util.py

This file was deleted.

10 changes: 4 additions & 6 deletions tests/asgi/test_cythonized_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
else:
_CYTHON_FUNC_TEST_TYPES = []

from _util import disable_asgi_non_coroutine_wrapping # NOQA


# NOTE(vytas): Cython 3.0+ now correctly marks cythonized coroutines as such,
# however, the relevant protocol is only available in Python 3.10+.
Expand Down Expand Up @@ -83,8 +81,8 @@ def test_not_cython_func(func):


@pytest.mark.skipif(not pyximport, reason='Cython not installed')
def test_jsonchema_validator(client):
with disable_asgi_non_coroutine_wrapping():
def test_jsonchema_validator(client, util):
with util.disable_asgi_non_coroutine_wrapping():
if CYTHON_COROUTINE_HINT:
client.app.add_route('/', _cythonized.TestResourceWithValidationNoHint())
else:
Expand Down Expand Up @@ -126,8 +124,8 @@ def test_scheduled_jobs_type_error(client):


@pytest.mark.skipif(not pyximport, reason='Cython not installed')
def test_hooks(client):
with disable_asgi_non_coroutine_wrapping():
def test_hooks(client, util):
with util.disable_asgi_non_coroutine_wrapping():
if CYTHON_COROUTINE_HINT:
client.app.add_route('/', _cythonized.TestResourceWithHooksNoHint())
else:
Expand Down
5 changes: 2 additions & 3 deletions tests/asgi/test_hello_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import tempfile

from _util import disable_asgi_non_coroutine_wrapping # NOQA
import pytest

import falcon
Expand Down Expand Up @@ -376,8 +375,8 @@ def test_status_not_set(self, client):
assert not result.content
assert result.status_code == 200

def test_coroutine_required(self, client):
with disable_asgi_non_coroutine_wrapping():
def test_coroutine_required(self, client, util):
with util.disable_asgi_non_coroutine_wrapping():
with pytest.raises(TypeError) as exinfo:
client.app.add_route('/', PartialCoroutineResource())

Expand Down
26 changes: 15 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

import falcon
import falcon.asgi
import falcon.testing

try:
import cython # noqa

has_cython = True
except ImportError:
try:
import falcon.cyutil.reader # noqa

has_cython = True
except ImportError:
has_cython = False

HERE = pathlib.Path(__file__).resolve().parent
FALCON_ROOT = HERE.parent
Expand All @@ -34,6 +47,8 @@ def app_kind(asgi):
class _SuiteUtils:
"""Assorted utilities that previously resided in the _util.py module."""

HAS_CYTHON = has_cython

@staticmethod
def create_app(asgi, **app_kwargs):
App = falcon.asgi.App if asgi else falcon.App
Expand Down Expand Up @@ -75,17 +90,6 @@ def disable_asgi_non_coroutine_wrapping():
if should_wrap:
os.environ['FALCON_ASGI_WRAP_NON_COROUTINES'] = 'Y'

@staticmethod
def as_params(*values, prefix=None):
if not prefix:
prefix = ''
# NOTE(caselit): each value must be a tuple/list even when using one
# single argument
return [
pytest.param(*value, id=f'{prefix}_{i}' if prefix else f'{i}')
for i, value in enumerate(values, 1)
]

@staticmethod
def load_module(filename, parent_dir=None, suffix=None):
if parent_dir:
Expand Down
12 changes: 5 additions & 7 deletions tests/test_after_hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import functools
import json

from _util import create_app # NOQA
from _util import create_resp # NOQA
import pytest

import falcon
Expand All @@ -19,8 +17,8 @@ def wrapped_resource_aware():


@pytest.fixture
def client(asgi):
app = create_app(asgi)
def client(asgi, util):
app = util.create_app(asgi)

resource = WrappedRespondersResourceAsync() if asgi else WrappedRespondersResource()
app.add_route('/', resource)
Expand Down Expand Up @@ -259,8 +257,8 @@ def test_resource_with_uri_fields(client, resource):
assert resource.fields == ('82074', '58927')


def test_resource_with_uri_fields_async():
app = create_app(asgi=True)
def test_resource_with_uri_fields_async(util):
app = util.create_app(asgi=True)

resource = ClassResourceWithURIFieldsAsync()
app.add_route('/{field1}/{field2}', resource)
Expand All @@ -275,7 +273,7 @@ async def test_direct():
resource = ClassResourceWithURIFieldsAsync()

req = testing.create_asgi_req()
resp = create_resp(True)
resp = util.create_resp(True)

await resource.on_get(req, resp, '1', '2')
assert resource.fields == ('1', '2')
Expand Down
23 changes: 10 additions & 13 deletions tests/test_before_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import io
import json

from _util import create_app # NOQA
from _util import create_resp # NOQA
from _util import disable_asgi_non_coroutine_wrapping # NOQA
import pytest

import falcon
Expand Down Expand Up @@ -246,8 +243,8 @@ def resource():


@pytest.fixture
def client(asgi, request, resource):
app = create_app(asgi)
def client(asgi, util, request, resource):
app = util.create_app(asgi)
app.add_route('/', resource)
return testing.TestClient(app)

Expand Down Expand Up @@ -337,8 +334,8 @@ def test_parser_sync(body, doc):
(None, None),
],
)
def test_parser_async(body, doc):
with disable_asgi_non_coroutine_wrapping():
def test_parser_async(body, doc, util):
with util.disable_asgi_non_coroutine_wrapping():

class WrappedRespondersBodyParserAsyncResource:
@falcon.before(validate_param_async, 'limit', 100, is_async=True)
Expand All @@ -350,7 +347,7 @@ async def on_get(self, req, resp, doc=None):
async def on_put(self, req, resp, doc=None):
self.doc = doc

app = create_app(asgi=True)
app = util.create_app(asgi=True)

resource = WrappedRespondersBodyParserAsyncResource()
app.add_route('/', resource)
Expand All @@ -365,7 +362,7 @@ async def test_direct():
resource = WrappedRespondersBodyParserAsyncResource()

req = testing.create_asgi_req()
resp = create_resp(True)
resp = util.create_resp(True)

await resource.on_get(req, resp, doc)
assert resource.doc == doc
Expand Down Expand Up @@ -479,11 +476,11 @@ async def on_post_collection(self, req, resp):
resp.status = falcon.HTTP_CREATED


@pytest.fixture(params=[True, False])
def app_client(request):
items = PiggybackingCollectionAsync() if request.param else PiggybackingCollection()
@pytest.fixture()
def app_client(asgi, util):
items = PiggybackingCollectionAsync() if asgi else PiggybackingCollection()

app = create_app(asgi=request.param)
app = util.create_app(asgi)
app.add_route('/items', items, suffix='collection')
app.add_route('/items/{itemid:int}', items)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_cmd_inspect_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import io
import sys

from _util import create_app # NOQA
import pytest

from falcon import App
from falcon import inspect
import falcon.asgi
from falcon.cmd import inspect_app
from falcon.testing import redirected

Expand All @@ -30,6 +30,11 @@ async def on_get(self, req, resp):
resp.status = '200 OK'


def create_app(asgi):
app_cls = falcon.asgi.App if asgi else App
return app_cls()


def make_app(asgi=False):
app = create_app(asgi)
app.add_route('/test', DummyResourceAsync() if asgi else DummyResource())
Expand Down
5 changes: 2 additions & 3 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from http import cookies as http_cookies
import re

from _util import create_app # NOQA
import pytest

import falcon
Expand Down Expand Up @@ -107,8 +106,8 @@ def on_get(self, req, resp):


@pytest.fixture
def client(asgi):
app = create_app(asgi)
def client(asgi, util):
app = util.create_app(asgi)
app.add_route('/', CookieResource())
app.add_route('/test-convert', CookieResourceMaxAgeFloatString())
app.add_route('/same-site', CookieResourceSameSite())
Expand Down
16 changes: 7 additions & 9 deletions tests/test_cors_middleware.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
from _util import create_app # NOQA
from _util import disable_asgi_non_coroutine_wrapping # NOQA
import pytest

import falcon
from falcon import testing


@pytest.fixture
def client(asgi):
app = create_app(asgi)
def client(asgi, util):
app = util.create_app(asgi)
return testing.TestClient(app)


@pytest.fixture(scope='function')
def cors_client(asgi):
def cors_client(asgi, util):
# NOTE(kgriffs): Disable wrapping to test that built-in middleware does
# not require it (since this will be the case for non-test apps).
with disable_asgi_non_coroutine_wrapping():
app = create_app(asgi, cors_enable=True)
with util.disable_asgi_non_coroutine_wrapping():
app = util.create_app(asgi, cors_enable=True)
return testing.TestClient(app)


Expand Down Expand Up @@ -98,9 +96,9 @@ def test_enabled_cors_handles_preflighting_no_headers_in_req(self, cors_client):


@pytest.fixture(scope='function')
def make_cors_client(asgi):
def make_cors_client(asgi, util):
def make(middleware):
app = create_app(asgi, middleware=middleware)
app = util.create_app(asgi, middleware=middleware)
return testing.TestClient(app)

return make
Expand Down
Loading

0 comments on commit edcd5f6

Please sign in to comment.