From cb1c233f6c2a652e617d90e73194a5e48a18525b Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Fri, 16 Dec 2022 22:25:33 -0600 Subject: [PATCH 1/2] Add test_add_signal_method_handler test case --- tests/test_signals.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_signals.py b/tests/test_signals.py index 9835430967..af31fbcd9c 100644 --- a/tests/test_signals.py +++ b/tests/test_signals.py @@ -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 @@ -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(*_): From 61bb2637cdc85ebbaea4c34ba0bfb29f764ef28e Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Fri, 16 Dec 2022 22:26:19 -0600 Subject: [PATCH 2/2] Better format of str --- tests/test_app.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index f5eec8e2ca..471c5689b7 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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")