From 108a4a99c7e12561b35b62e26ecfc33c77db802d Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 21 Jun 2021 15:10:26 +0300 Subject: [PATCH] v2 AST router (#2133) * Update some tests * Update some tests * Resolve #2122 route decorator returning tuple * Use rc sanic-routing version * Update unit tests to <:str> --- sanic/mixins/routes.py | 4 +++- sanic/signals.py | 10 ++++++++-- setup.py | 2 +- tests/test_named_routes.py | 5 +++-- tests/test_requests.py | 4 +--- tests/test_routes.py | 14 +++++++------- tests/test_url_building.py | 6 +++--- 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/sanic/mixins/routes.py b/sanic/mixins/routes.py index e468be69..5af1610d 100644 --- a/sanic/mixins/routes.py +++ b/sanic/mixins/routes.py @@ -160,7 +160,9 @@ class RouteMixin: if apply: self._apply_route(route) - return route, handler + if static: + return route, handler + return handler return decorator diff --git a/sanic/signals.py b/sanic/signals.py index 87263f1f..eec2a438 100644 --- a/sanic/signals.py +++ b/sanic/signals.py @@ -48,7 +48,7 @@ class SignalRouter(BaseRouter): f".{event}", self.DEFAULT_METHOD, self, - {"__params__": {}}, + {"__params__": {}, "__matches__": {}}, extra=extra, ) except NotFound: @@ -59,7 +59,13 @@ class SignalRouter(BaseRouter): terms.append(extra) raise NotFound(message % tuple(terms)) - params = param_basket.pop("__params__") + params = param_basket["__params__"] + if not params: + params = { + param.name: param_basket["__matches__"][idx] + for idx, param in group.params.items() + } + return group, [route.handler for route in group], params async def _dispatch( diff --git a/setup.py b/setup.py index 0a9ddbfb..58604683 100644 --- a/setup.py +++ b/setup.py @@ -83,7 +83,7 @@ ujson = "ujson>=1.35" + env_dependency uvloop = "uvloop>=0.5.3" + env_dependency requirements = [ - "sanic-routing>=0.6.0", + "sanic-routing==0.7.0rc1", "httptools>=0.0.10", uvloop, ujson, diff --git a/tests/test_named_routes.py b/tests/test_named_routes.py index 39108594..b4ab249b 100644 --- a/tests/test_named_routes.py +++ b/tests/test_named_routes.py @@ -234,7 +234,7 @@ def test_named_dynamic_route(): app.router.routes_all[ ( "folder", - "", + "", ) ].name == "app.route_dynamic" @@ -369,7 +369,8 @@ def test_dynamic_add_named_route(): app.add_route(handler, "/folder/", name="route_dynamic") assert ( - app.router.routes_all[("folder", "")].name == "app.route_dynamic" + app.router.routes_all[("folder", "")].name + == "app.route_dynamic" ) assert app.url_for("route_dynamic", name="test") == "/folder/test" with pytest.raises(URLBuildError): diff --git a/tests/test_requests.py b/tests/test_requests.py index b459a605..35b2c900 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2246,9 +2246,7 @@ def test_conflicting_body_methods_overload(app): def test_handler_overload(app): - @app.get( - "/long/sub/route/param_a//param_b/" - ) + @app.get("/long/sub/route/param_a//param_b/") @app.post("/long/sub/route/") def handler(request, **kwargs): return json(kwargs) diff --git a/tests/test_routes.py b/tests/test_routes.py index 06b4d799..0f4980f6 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -258,7 +258,7 @@ def test_route_strict_slash(app): def test_route_invalid_parameter_syntax(app): with pytest.raises(ValueError): - @app.get("/get/<:string>", strict_slashes=True) + @app.get("/get/<:str>", strict_slashes=True) def handler(request): return text("OK") @@ -478,7 +478,7 @@ def test_dynamic_route(app): def test_dynamic_route_string(app): results = [] - @app.route("/folder/") + @app.route("/folder/") async def handler(request, name): results.append(name) return text("OK") @@ -513,7 +513,7 @@ def test_dynamic_route_int(app): def test_dynamic_route_number(app): results = [] - @app.route("/weight/") + @app.route("/weight/") async def handler(request, weight): results.append(weight) return text("OK") @@ -585,7 +585,6 @@ def test_dynamic_route_path(app): return text("OK") app.router.finalize() - print(app.router.find_route_src) request, response = app.test_client.get("/path/1/info") assert response.status == 200 @@ -824,7 +823,7 @@ def test_dynamic_add_route_string(app): results.append(name) return text("OK") - app.add_route(handler, "/folder/") + app.add_route(handler, "/folder/") request, response = app.test_client.get("/folder/test123") assert response.text == "OK" @@ -860,7 +859,7 @@ def test_dynamic_add_route_number(app): results.append(weight) return text("OK") - app.add_route(handler, "/weight/") + app.add_route(handler, "/weight/") request, response = app.test_client.get("/weight/12345") assert response.text == "OK" @@ -1067,7 +1066,8 @@ def test_uri_with_different_method_and_different_params(app): return json({"action": action}) request, response = app.test_client.get("/ads/1234") - assert response.status == 405 + assert response.status == 200 + assert response.json == {"ad_id": "1234"} request, response = app.test_client.post("/ads/post") assert response.status == 200 diff --git a/tests/test_url_building.py b/tests/test_url_building.py index cccfe543..f8abcb62 100644 --- a/tests/test_url_building.py +++ b/tests/test_url_building.py @@ -143,7 +143,7 @@ def test_fails_url_build_if_params_not_passed(app): COMPLEX_PARAM_URL = ( "///" - "//" + "//" ) PASSING_KWARGS = { "foo": 4, @@ -168,7 +168,7 @@ def test_fails_with_int_message(app): expected_error = ( r'Value "not_int" for parameter `foo` ' - r"does not match pattern for type `int`: ^-?\d+" + r"does not match pattern for type `int`: ^-?\d+$" ) assert str(e.value) == expected_error @@ -223,7 +223,7 @@ def test_fails_with_number_message(app): @pytest.mark.parametrize("number", [3, -3, 13.123, -13.123]) def test_passes_with_negative_number_message(app, number): - @app.route("path//another-word") + @app.route("path//another-word") def good(request, possibly_neg): assert isinstance(possibly_neg, (int, float)) return text(f"this should pass with `{possibly_neg}`")