[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
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.
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_error": true,
"dispatch_request_not_found": true
}
}

View file

@ -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__":

View file

@ -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

View file

@ -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
branch_coverage[branch_name] = True

View file

@ -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)
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)

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
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