From a2dd6b86c5b6cc09f9193041066dbed0489c938f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Jun 2024 00:05:42 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 4 +- coverage_result.json | 2 +- src/flask/__init__.py | 4 +- src/flask/app.py | 5 +-- src/flask/coverage_tracker.py | 7 ++-- tests/test_dispatch_request.py | 63 ++++++++++++++++------------- tests/test_get_send_file_max_age.py | 22 +++++----- 7 files changed, 61 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 5dc4a23b..ce8fb079 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Programming language: Python ### Existing tool -The existing tool used for measuring coverage is coverage.py. It was executed using the following command: +The existing tool used for measuring coverage is coverage.py. It was executed using the following command: ```coverage run -m pytest``` @@ -73,4 +73,4 @@ The coverage improved by 58%, from 42% to 100%. ## Statement of individual contributions -Jannes van den Bogert: I was responsible for designing and implementing two tests for the Flask application. The tests targeted two specific functions: get_send_file_max_age and dispatch_request. My contributions were helping in enhancing the test coverage from partial to complete for these functions, achieving a significant increase in overall coverage. \ No newline at end of file +Jannes van den Bogert: I was responsible for designing and implementing two tests for the Flask application. The tests targeted two specific functions: get_send_file_max_age and dispatch_request. My contributions were helping in enhancing the test coverage from partial to complete for these functions, achieving a significant increase in overall coverage. diff --git a/coverage_result.json b/coverage_result.json index 8b276bed..beeee2a2 100644 --- a/coverage_result.json +++ b/coverage_result.json @@ -8,4 +8,4 @@ "dispatch_request_success": true, "dispatch_request_error": true, "dispatch_request_not_found": true -} \ No newline at end of file +} diff --git a/src/flask/__init__.py b/src/flask/__init__.py index 06c906b6..6f395fe7 100644 --- a/src/flask/__init__.py +++ b/src/flask/__init__.py @@ -6,6 +6,8 @@ from . import json as json from .app import Flask as Flask from .blueprints import Blueprint as Blueprint from .config import Config as Config +from .coverage_tracker import branch_coverage +from .coverage_tracker import track_coverage from .ctx import after_this_request as after_this_request from .ctx import copy_current_request_context as copy_current_request_context from .ctx import has_app_context as has_app_context @@ -41,7 +43,7 @@ from .templating import stream_template as stream_template from .templating import stream_template_string as stream_template_string from .wrappers import Request as Request from .wrappers import Response as Response -from .coverage_tracker import track_coverage, branch_coverage + def __getattr__(name: str) -> t.Any: if name == "__version__": diff --git a/src/flask/app.py b/src/flask/app.py index 1a77455f..21f27f38 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -10,7 +10,6 @@ from inspect import iscoroutinefunction from itertools import chain from types import TracebackType from urllib.parse import quote as _url_quote -from .coverage_tracker import track_coverage, branch_coverage import click from werkzeug.datastructures import Headers @@ -28,6 +27,7 @@ from werkzeug.wrappers import Response as BaseResponse from . import cli from . import typing as ft +from .coverage_tracker import track_coverage from .ctx import AppContext from .ctx import RequestContext from .globals import _cv_app @@ -293,7 +293,7 @@ class Flask(App): if value is None: track_coverage("get_send_file_max_age_1") return None - + if isinstance(value, timedelta): track_coverage("get_send_file_max_age_2") return int(value.total_seconds()) @@ -870,7 +870,6 @@ class Flask(App): track_coverage("dispatch_request_3") return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] - def full_dispatch_request(self) -> Response: """Dispatches the request and on top of that performs request pre and postprocessing as well as HTTP exception catching and diff --git a/src/flask/coverage_tracker.py b/src/flask/coverage_tracker.py index beaf85f7..d2c793a7 100644 --- a/src/flask/coverage_tracker.py +++ b/src/flask/coverage_tracker.py @@ -1,12 +1,13 @@ branch_coverage = { - "get_send_file_max_age_1": False, + "get_send_file_max_age_1": False, "get_send_file_max_age_2": False, - "get_send_file_max_age_3": False, + "get_send_file_max_age_3": False, "dispatch_request_1": False, "dispatch_request_2": False, "dispatch_request_3": False, } + def track_coverage(branch_name): global branch_coverage - branch_coverage[branch_name] = True \ No newline at end of file + branch_coverage[branch_name] = True diff --git a/tests/test_dispatch_request.py b/tests/test_dispatch_request.py index f94f67f4..4a907a17 100644 --- a/tests/test_dispatch_request.py +++ b/tests/test_dispatch_request.py @@ -1,59 +1,68 @@ import pytest -from flask import Flask, json -from werkzeug.exceptions import HTTPException, NotFound, InternalServerError -from unittest.mock import patch -from flask import branch_coverage, track_coverage +from werkzeug.exceptions import InternalServerError +from werkzeug.exceptions import NotFound + +from flask import branch_coverage +from flask import Flask +from flask import json +from flask import track_coverage + def create_app(): app = Flask(__name__) - @app.route('/success') + @app.route("/success") def success(): - track_coverage('dispatch_request_success') - return 'Success', 200 + track_coverage("dispatch_request_success") + return "Success", 200 - @app.route('/error') + @app.route("/error") def error(): - track_coverage('dispatch_request_error') - raise InternalServerError(description='Error occurred') + track_coverage("dispatch_request_error") + raise InternalServerError(description="Error occurred") - @app.route('/not_found') + @app.route("/not_found") def not_found(): - track_coverage('dispatch_request_not_found') - raise NotFound(description='This is a 404') + track_coverage("dispatch_request_not_found") + raise NotFound(description="This is a 404") return app + @pytest.fixture def client(): app = create_app() - app.config['TESTING'] = True + app.config["TESTING"] = True with app.test_client() as client: yield client + def test_success_path(client): - response = client.get('/success') + response = client.get("/success") assert response.status_code == 200 - assert response.data == b'Success' - assert branch_coverage['dispatch_request_success'] + assert response.data == b"Success" + assert branch_coverage["dispatch_request_success"] + def test_error_handling(client): - response = client.get('/error') + response = client.get("/error") assert response.status_code == 500 - assert 'Error occurred' in response.get_data(as_text=True) - assert branch_coverage['dispatch_request_error'] + assert "Error occurred" in response.get_data(as_text=True) + assert branch_coverage["dispatch_request_error"] + def test_not_found_handling(client): - response = client.get('/not_found') + response = client.get("/not_found") assert response.status_code == 404 - assert 'This is a 404' in response.get_data(as_text=True) - assert branch_coverage['dispatch_request_not_found'] + assert "This is a 404" in response.get_data(as_text=True) + assert branch_coverage["dispatch_request_not_found"] + def test_no_route(client): - response = client.get('/no_route') + response = client.get("/no_route") assert response.status_code == 404 -def save_coverage_to_json(file_path='coverage_result.json'): - with open(file_path, 'w') as json_file: - json.dump(branch_coverage, json_file, indent=4) \ No newline at end of file +def save_coverage_to_json(file_path="coverage_result.json"): + with open(file_path, "w") as json_file: + json.dump(branch_coverage, json_file, indent=4) diff --git a/tests/test_get_send_file_max_age.py b/tests/test_get_send_file_max_age.py index 3f2fea0a..4e287e3b 100644 --- a/tests/test_get_send_file_max_age.py +++ b/tests/test_get_send_file_max_age.py @@ -1,20 +1,21 @@ -import pytest -from unittest.mock import MagicMock, patch -from datetime import timedelta -from flask import Flask, current_app -from flask.app import Flask as FlaskApp -from flask import branch_coverage, track_coverage import json +from datetime import timedelta -def save_coverage_to_json(file_path='coverage_result.json'): - with open(file_path, 'w') as json_file: +from flask import branch_coverage +from flask import Flask + + +def save_coverage_to_json(file_path="coverage_result.json"): + with open(file_path, "w") as json_file: json.dump(branch_coverage, json_file, indent=4) + def create_test_app(config): app = Flask(__name__) app.config.update(config) return app + def test_get_send_file_max_age_none(): app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": None}) with app.app_context(): @@ -22,6 +23,7 @@ def test_get_send_file_max_age_none(): assert max_age is None assert branch_coverage["get_send_file_max_age_1"] == True + def test_get_send_file_max_age_timedelta(): app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": timedelta(hours=1)}) with app.app_context(): @@ -29,6 +31,7 @@ def test_get_send_file_max_age_timedelta(): assert max_age == 3600 assert branch_coverage["get_send_file_max_age_2"] == True + def test_get_send_file_max_age_int(): app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": 3600}) with app.app_context(): @@ -36,8 +39,9 @@ def test_get_send_file_max_age_int(): assert max_age == 3600 assert branch_coverage["get_send_file_max_age_3"] == True + def test_branch_coverage(): save_coverage_to_json() assert branch_coverage["get_send_file_max_age_1"] == True assert branch_coverage["get_send_file_max_age_2"] == True - assert branch_coverage["get_send_file_max_age_3"] == True + assert branch_coverage["get_send_file_max_age_3"] == True