Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method Signal Handler Test #2630

Merged
merged 2 commits into from Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions tests/test_app.py
Expand Up @@ -348,12 +348,14 @@ def test_app_registry_retrieval_from_multiple():
def test_get_app_does_not_exist():
with pytest.raises(
SanicException,
match="Sanic app name 'does-not-exist' not found.\n"
"App instantiation must occur outside "
"if __name__ == '__main__' "
"block or by using an AppLoader.\nSee "
"https://sanic.dev/en/guide/deployment/app-loader.html"
" for more details.",
match=(
"Sanic app name 'does-not-exist' not found.\n"
"App instantiation must occur outside "
"if __name__ == '__main__' "
"block or by using an AppLoader.\nSee "
"https://sanic.dev/en/guide/deployment/app-loader.html"
" for more details."
),
):
Sanic.get_app("does-not-exist")

Expand Down
27 changes: 26 additions & 1 deletion tests/test_signals.py
Expand Up @@ -7,7 +7,7 @@

from sanic_routing.exceptions import NotFound

from sanic import Blueprint
from sanic import Blueprint, Sanic, empty
from sanic.exceptions import InvalidSignal, SanicException


Expand All @@ -20,6 +20,31 @@ def sync_signal(*_):
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):
@app.signal("foo.bar.baz")
def sync_signal(*_):
Expand Down