From b850e49cb3ab6cc225b4b7573a880294f254759b Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Tue, 9 Feb 2021 16:17:53 +0200 Subject: [PATCH] test coverage with param change --- sanic/app.py | 66 +++++++++++++++++++--------------------- tests/test_blueprints.py | 14 ++++----- tests/test_routes.py | 3 ++ 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index ca8c6936..1ff5a079 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -405,46 +405,42 @@ class Sanic(BaseSanic): # find all the parameters we will need to build in the URL # matched_params = re.findall(self.router.parameter_pattern, uri) route.finalize() - for params in route.params.values(): + for param_info in route.params.values(): # name, _type, pattern = self.router.parse_parameter_string(match) # we only want to match against each individual parameter - for idx, param_info in enumerate(params): - try: - supplied_param = str(kwargs.pop(param_info.name)) - except KeyError: - raise URLBuildError( - f"Required parameter `{param_info.name}` was not " - "passed to url_for" - ) + try: + supplied_param = str(kwargs.pop(param_info.name)) + except KeyError: + raise URLBuildError( + f"Required parameter `{param_info.name}` was not " + "passed to url_for" + ) - # determine if the parameter supplied by the caller - # passes the test in the URL - if param_info.pattern: - passes_pattern = param_info.pattern.match(supplied_param) - if not passes_pattern: - if idx + 1 == len(params): - if param_info.cast != str: - msg = ( - f'Value "{supplied_param}" ' - f"for parameter `{param_info.name}` does " - "not match pattern for type " - f"`{param_info.cast.__name__}`: " - f"{param_info.pattern.pattern}" - ) - else: - msg = ( - f'Value "{supplied_param}" for parameter ' - f"`{param_info.name}` does not satisfy " - f"pattern {param_info.pattern.pattern}" - ) - raise URLBuildError(msg) - else: - continue + # determine if the parameter supplied by the caller + # passes the test in the URL + if param_info.pattern: + passes_pattern = param_info.pattern.match(supplied_param) + if not passes_pattern: + if param_info.cast != str: + msg = ( + f'Value "{supplied_param}" ' + f"for parameter `{param_info.name}` does " + "not match pattern for type " + f"`{param_info.cast.__name__}`: " + f"{param_info.pattern.pattern}" + ) + else: + msg = ( + f'Value "{supplied_param}" for parameter ' + f"`{param_info.name}` does not satisfy " + f"pattern {param_info.pattern.pattern}" + ) + raise URLBuildError(msg) - # replace the parameter in the URL with the supplied value - replacement_regex = f"(<{param_info.name}.*?>)" - out = re.sub(replacement_regex, supplied_param, out) + # replace the parameter in the URL with the supplied value + replacement_regex = f"(<{param_info.name}.*?>)" + out = re.sub(replacement_regex, supplied_param, out) # parse the remainder of the keyword arguments into a querystring query_string = urlencode(kwargs, doseq=True) if kwargs else "" diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 4933dd22..88055b57 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -88,18 +88,18 @@ def test_bp_strict_slash(app): app.blueprint(bp) - # request, response = app.test_client.get("/get") - # assert response.text == "OK" - # assert response.json is None + request, response = app.test_client.get("/get") + assert response.text == "OK" + assert response.json is None - # request, response = app.test_client.get("/get/") - # assert response.status == 404 + request, response = app.test_client.get("/get/") + assert response.status == 404 request, response = app.test_client.post("/post/") assert response.text == "OK" - # request, response = app.test_client.post("/post") - # assert response.status == 404 + request, response = app.test_client.post("/post") + assert response.status == 404 def test_bp_strict_slash_default_value(app): diff --git a/tests/test_routes.py b/tests/test_routes.py index 7c0fb816..cd3e11e7 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -538,6 +538,9 @@ def test_dynamic_route_regex(app): async def handler(request, folder_id): return text("OK") + app.router.finalize() + print(app.router.find_route_src) + request, response = app.test_client.get("/folder/test") assert response.status == 200