[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-06-27 00:05:42 +00:00
parent 5fa27b0209
commit a2dd6b86c5
7 changed files with 61 additions and 46 deletions

View file

@ -14,7 +14,7 @@ Programming language: Python
### Existing tool ### 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``` ```coverage run -m pytest```
@ -73,4 +73,4 @@ The coverage improved by 58%, from 42% to 100%.
## Statement of individual contributions ## 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. 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.

View file

@ -8,4 +8,4 @@
"dispatch_request_success": true, "dispatch_request_success": true,
"dispatch_request_error": true, "dispatch_request_error": true,
"dispatch_request_not_found": true "dispatch_request_not_found": true
} }

View file

@ -6,6 +6,8 @@ from . import json as json
from .app import Flask as Flask from .app import Flask as Flask
from .blueprints import Blueprint as Blueprint from .blueprints import Blueprint as Blueprint
from .config import Config as Config 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 after_this_request as after_this_request
from .ctx import copy_current_request_context as copy_current_request_context from .ctx import copy_current_request_context as copy_current_request_context
from .ctx import has_app_context as has_app_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 .templating import stream_template_string as stream_template_string
from .wrappers import Request as Request from .wrappers import Request as Request
from .wrappers import Response as Response from .wrappers import Response as Response
from .coverage_tracker import track_coverage, branch_coverage
def __getattr__(name: str) -> t.Any: def __getattr__(name: str) -> t.Any:
if name == "__version__": if name == "__version__":

View file

@ -10,7 +10,6 @@ from inspect import iscoroutinefunction
from itertools import chain from itertools import chain
from types import TracebackType from types import TracebackType
from urllib.parse import quote as _url_quote from urllib.parse import quote as _url_quote
from .coverage_tracker import track_coverage, branch_coverage
import click import click
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
@ -28,6 +27,7 @@ from werkzeug.wrappers import Response as BaseResponse
from . import cli from . import cli
from . import typing as ft from . import typing as ft
from .coverage_tracker import track_coverage
from .ctx import AppContext from .ctx import AppContext
from .ctx import RequestContext from .ctx import RequestContext
from .globals import _cv_app from .globals import _cv_app
@ -293,7 +293,7 @@ class Flask(App):
if value is None: if value is None:
track_coverage("get_send_file_max_age_1") track_coverage("get_send_file_max_age_1")
return None return None
if isinstance(value, timedelta): if isinstance(value, timedelta):
track_coverage("get_send_file_max_age_2") track_coverage("get_send_file_max_age_2")
return int(value.total_seconds()) return int(value.total_seconds())
@ -870,7 +870,6 @@ class Flask(App):
track_coverage("dispatch_request_3") track_coverage("dispatch_request_3")
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
def full_dispatch_request(self) -> Response: def full_dispatch_request(self) -> Response:
"""Dispatches the request and on top of that performs request """Dispatches the request and on top of that performs request
pre and postprocessing as well as HTTP exception catching and pre and postprocessing as well as HTTP exception catching and

View file

@ -1,12 +1,13 @@
branch_coverage = { 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_2": False,
"get_send_file_max_age_3": False, "get_send_file_max_age_3": False,
"dispatch_request_1": False, "dispatch_request_1": False,
"dispatch_request_2": False, "dispatch_request_2": False,
"dispatch_request_3": False, "dispatch_request_3": False,
} }
def track_coverage(branch_name): def track_coverage(branch_name):
global branch_coverage global branch_coverage
branch_coverage[branch_name] = True branch_coverage[branch_name] = True

View file

@ -1,59 +1,68 @@
import pytest import pytest
from flask import Flask, json from werkzeug.exceptions import InternalServerError
from werkzeug.exceptions import HTTPException, NotFound, InternalServerError from werkzeug.exceptions import NotFound
from unittest.mock import patch
from flask import branch_coverage, track_coverage from flask import branch_coverage
from flask import Flask
from flask import json
from flask import track_coverage
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
@app.route('/success') @app.route("/success")
def success(): def success():
track_coverage('dispatch_request_success') track_coverage("dispatch_request_success")
return 'Success', 200 return "Success", 200
@app.route('/error') @app.route("/error")
def error(): def error():
track_coverage('dispatch_request_error') track_coverage("dispatch_request_error")
raise InternalServerError(description='Error occurred') raise InternalServerError(description="Error occurred")
@app.route('/not_found') @app.route("/not_found")
def not_found(): def not_found():
track_coverage('dispatch_request_not_found') track_coverage("dispatch_request_not_found")
raise NotFound(description='This is a 404') raise NotFound(description="This is a 404")
return app return app
@pytest.fixture @pytest.fixture
def client(): def client():
app = create_app() app = create_app()
app.config['TESTING'] = True app.config["TESTING"] = True
with app.test_client() as client: with app.test_client() as client:
yield client yield client
def test_success_path(client): def test_success_path(client):
response = client.get('/success') response = client.get("/success")
assert response.status_code == 200 assert response.status_code == 200
assert response.data == b'Success' assert response.data == b"Success"
assert branch_coverage['dispatch_request_success'] assert branch_coverage["dispatch_request_success"]
def test_error_handling(client): def test_error_handling(client):
response = client.get('/error') response = client.get("/error")
assert response.status_code == 500 assert response.status_code == 500
assert 'Error occurred' in response.get_data(as_text=True) assert "Error occurred" in response.get_data(as_text=True)
assert branch_coverage['dispatch_request_error'] assert branch_coverage["dispatch_request_error"]
def test_not_found_handling(client): def test_not_found_handling(client):
response = client.get('/not_found') response = client.get("/not_found")
assert response.status_code == 404 assert response.status_code == 404
assert 'This is a 404' in response.get_data(as_text=True) assert "This is a 404" in response.get_data(as_text=True)
assert branch_coverage['dispatch_request_not_found'] assert branch_coverage["dispatch_request_not_found"]
def test_no_route(client): def test_no_route(client):
response = client.get('/no_route') response = client.get("/no_route")
assert response.status_code == 404 assert response.status_code == 404
def save_coverage_to_json(file_path='coverage_result.json'): def save_coverage_to_json(file_path="coverage_result.json"):
with open(file_path, 'w') as json_file: with open(file_path, "w") as json_file:
json.dump(branch_coverage, json_file, indent=4) json.dump(branch_coverage, json_file, indent=4)

View file

@ -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 import json
from datetime import timedelta
def save_coverage_to_json(file_path='coverage_result.json'): from flask import branch_coverage
with open(file_path, 'w') as json_file: 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) json.dump(branch_coverage, json_file, indent=4)
def create_test_app(config): def create_test_app(config):
app = Flask(__name__) app = Flask(__name__)
app.config.update(config) app.config.update(config)
return app return app
def test_get_send_file_max_age_none(): def test_get_send_file_max_age_none():
app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": None}) app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": None})
with app.app_context(): with app.app_context():
@ -22,6 +23,7 @@ def test_get_send_file_max_age_none():
assert max_age is None assert max_age is None
assert branch_coverage["get_send_file_max_age_1"] == True assert branch_coverage["get_send_file_max_age_1"] == True
def test_get_send_file_max_age_timedelta(): def test_get_send_file_max_age_timedelta():
app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": timedelta(hours=1)}) app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": timedelta(hours=1)})
with app.app_context(): with app.app_context():
@ -29,6 +31,7 @@ def test_get_send_file_max_age_timedelta():
assert max_age == 3600 assert max_age == 3600
assert branch_coverage["get_send_file_max_age_2"] == True assert branch_coverage["get_send_file_max_age_2"] == True
def test_get_send_file_max_age_int(): def test_get_send_file_max_age_int():
app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": 3600}) app = create_test_app({"SEND_FILE_MAX_AGE_DEFAULT": 3600})
with app.app_context(): with app.app_context():
@ -36,8 +39,9 @@ def test_get_send_file_max_age_int():
assert max_age == 3600 assert max_age == 3600
assert branch_coverage["get_send_file_max_age_3"] == True assert branch_coverage["get_send_file_max_age_3"] == True
def test_branch_coverage(): def test_branch_coverage():
save_coverage_to_json() save_coverage_to_json()
assert branch_coverage["get_send_file_max_age_1"] == True 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_2"] == True
assert branch_coverage["get_send_file_max_age_3"] == True assert branch_coverage["get_send_file_max_age_3"] == True