Method Signal Handler Test (#2630)

This commit is contained in:
Zhiwei 2022-12-17 12:38:46 -06:00 committed by GitHub
parent a3ff0c13b7
commit 154863d6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -348,12 +348,14 @@ def test_app_registry_retrieval_from_multiple():
def test_get_app_does_not_exist(): def test_get_app_does_not_exist():
with pytest.raises( with pytest.raises(
SanicException, SanicException,
match="Sanic app name 'does-not-exist' not found.\n" match=(
"App instantiation must occur outside " "Sanic app name 'does-not-exist' not found.\n"
"if __name__ == '__main__' " "App instantiation must occur outside "
"block or by using an AppLoader.\nSee " "if __name__ == '__main__' "
"https://sanic.dev/en/guide/deployment/app-loader.html" "block or by using an AppLoader.\nSee "
" for more details.", "https://sanic.dev/en/guide/deployment/app-loader.html"
" for more details."
),
): ):
Sanic.get_app("does-not-exist") Sanic.get_app("does-not-exist")

View File

@ -7,7 +7,7 @@ import pytest
from sanic_routing.exceptions import NotFound from sanic_routing.exceptions import NotFound
from sanic import Blueprint from sanic import Blueprint, Sanic, empty
from sanic.exceptions import InvalidSignal, SanicException from sanic.exceptions import InvalidSignal, SanicException
@ -20,6 +20,31 @@ def test_add_signal(app):
assert len(app.signal_router.routes) == 1 assert len(app.signal_router.routes) == 1
def test_add_signal_method_handler(app):
counter = 0
class TestSanic(Sanic):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_signal(
self.after_routing_signal_handler, "http.routing.after"
)
def after_routing_signal_handler(self, *args, **kwargs):
nonlocal counter
counter += 1
app = TestSanic("Test")
assert len(app.signal_router.routes) == 1
@app.route("/")
async def handler(_):
return empty()
app.test_client.get("/")
assert counter == 1
def test_add_signal_decorator(app): def test_add_signal_decorator(app):
@app.signal("foo.bar.baz") @app.signal("foo.bar.baz")
def sync_signal(*_): def sync_signal(*_):