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>
This commit is contained in:
Adam Hopkins 2021-06-21 15:10:26 +03:00 committed by GitHub
parent 7c180376d6
commit 108a4a99c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 19 deletions

View File

@ -160,7 +160,9 @@ class RouteMixin:
if apply: if apply:
self._apply_route(route) self._apply_route(route)
return route, handler if static:
return route, handler
return handler
return decorator return decorator

View File

@ -48,7 +48,7 @@ class SignalRouter(BaseRouter):
f".{event}", f".{event}",
self.DEFAULT_METHOD, self.DEFAULT_METHOD,
self, self,
{"__params__": {}}, {"__params__": {}, "__matches__": {}},
extra=extra, extra=extra,
) )
except NotFound: except NotFound:
@ -59,7 +59,13 @@ class SignalRouter(BaseRouter):
terms.append(extra) terms.append(extra)
raise NotFound(message % tuple(terms)) 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 return group, [route.handler for route in group], params
async def _dispatch( async def _dispatch(

View File

@ -83,7 +83,7 @@ ujson = "ujson>=1.35" + env_dependency
uvloop = "uvloop>=0.5.3" + env_dependency uvloop = "uvloop>=0.5.3" + env_dependency
requirements = [ requirements = [
"sanic-routing>=0.6.0", "sanic-routing==0.7.0rc1",
"httptools>=0.0.10", "httptools>=0.0.10",
uvloop, uvloop,
ujson, ujson,

View File

@ -234,7 +234,7 @@ def test_named_dynamic_route():
app.router.routes_all[ app.router.routes_all[
( (
"folder", "folder",
"<name>", "<name:str>",
) )
].name ].name
== "app.route_dynamic" == "app.route_dynamic"
@ -369,7 +369,8 @@ def test_dynamic_add_named_route():
app.add_route(handler, "/folder/<name>", name="route_dynamic") app.add_route(handler, "/folder/<name>", name="route_dynamic")
assert ( assert (
app.router.routes_all[("folder", "<name>")].name == "app.route_dynamic" app.router.routes_all[("folder", "<name:str>")].name
== "app.route_dynamic"
) )
assert app.url_for("route_dynamic", name="test") == "/folder/test" assert app.url_for("route_dynamic", name="test") == "/folder/test"
with pytest.raises(URLBuildError): with pytest.raises(URLBuildError):

View File

@ -2246,9 +2246,7 @@ def test_conflicting_body_methods_overload(app):
def test_handler_overload(app): def test_handler_overload(app):
@app.get( @app.get("/long/sub/route/param_a/<param_a:str>/param_b/<param_b:str>")
"/long/sub/route/param_a/<param_a:string>/param_b/<param_b:string>"
)
@app.post("/long/sub/route/") @app.post("/long/sub/route/")
def handler(request, **kwargs): def handler(request, **kwargs):
return json(kwargs) return json(kwargs)

View File

@ -258,7 +258,7 @@ def test_route_strict_slash(app):
def test_route_invalid_parameter_syntax(app): def test_route_invalid_parameter_syntax(app):
with pytest.raises(ValueError): with pytest.raises(ValueError):
@app.get("/get/<:string>", strict_slashes=True) @app.get("/get/<:str>", strict_slashes=True)
def handler(request): def handler(request):
return text("OK") return text("OK")
@ -478,7 +478,7 @@ def test_dynamic_route(app):
def test_dynamic_route_string(app): def test_dynamic_route_string(app):
results = [] results = []
@app.route("/folder/<name:string>") @app.route("/folder/<name:str>")
async def handler(request, name): async def handler(request, name):
results.append(name) results.append(name)
return text("OK") return text("OK")
@ -513,7 +513,7 @@ def test_dynamic_route_int(app):
def test_dynamic_route_number(app): def test_dynamic_route_number(app):
results = [] results = []
@app.route("/weight/<weight:number>") @app.route("/weight/<weight:float>")
async def handler(request, weight): async def handler(request, weight):
results.append(weight) results.append(weight)
return text("OK") return text("OK")
@ -585,7 +585,6 @@ def test_dynamic_route_path(app):
return text("OK") return text("OK")
app.router.finalize() app.router.finalize()
print(app.router.find_route_src)
request, response = app.test_client.get("/path/1/info") request, response = app.test_client.get("/path/1/info")
assert response.status == 200 assert response.status == 200
@ -824,7 +823,7 @@ def test_dynamic_add_route_string(app):
results.append(name) results.append(name)
return text("OK") return text("OK")
app.add_route(handler, "/folder/<name:string>") app.add_route(handler, "/folder/<name:str>")
request, response = app.test_client.get("/folder/test123") request, response = app.test_client.get("/folder/test123")
assert response.text == "OK" assert response.text == "OK"
@ -860,7 +859,7 @@ def test_dynamic_add_route_number(app):
results.append(weight) results.append(weight)
return text("OK") return text("OK")
app.add_route(handler, "/weight/<weight:number>") app.add_route(handler, "/weight/<weight:float>")
request, response = app.test_client.get("/weight/12345") request, response = app.test_client.get("/weight/12345")
assert response.text == "OK" assert response.text == "OK"
@ -1067,7 +1066,8 @@ def test_uri_with_different_method_and_different_params(app):
return json({"action": action}) return json({"action": action})
request, response = app.test_client.get("/ads/1234") 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") request, response = app.test_client.post("/ads/post")
assert response.status == 200 assert response.status == 200

View File

@ -143,7 +143,7 @@ def test_fails_url_build_if_params_not_passed(app):
COMPLEX_PARAM_URL = ( COMPLEX_PARAM_URL = (
"/<foo:int>/<four_letter_string:[A-z]{4}>/" "/<foo:int>/<four_letter_string:[A-z]{4}>/"
"<two_letter_string:[A-z]{2}>/<normal_string>/<some_number:number>" "<two_letter_string:[A-z]{2}>/<normal_string>/<some_number:float>"
) )
PASSING_KWARGS = { PASSING_KWARGS = {
"foo": 4, "foo": 4,
@ -168,7 +168,7 @@ def test_fails_with_int_message(app):
expected_error = ( expected_error = (
r'Value "not_int" for parameter `foo` ' 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 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]) @pytest.mark.parametrize("number", [3, -3, 13.123, -13.123])
def test_passes_with_negative_number_message(app, number): def test_passes_with_negative_number_message(app, number):
@app.route("path/<possibly_neg:number>/another-word") @app.route("path/<possibly_neg:float>/another-word")
def good(request, possibly_neg): def good(request, possibly_neg):
assert isinstance(possibly_neg, (int, float)) assert isinstance(possibly_neg, (int, float))
return text(f"this should pass with `{possibly_neg}`") return text(f"this should pass with `{possibly_neg}`")