From 6f49089a624a13353a07db87aff5fe6f01699728 Mon Sep 17 00:00:00 2001 From: MikeTheReader Date: Mon, 22 May 2017 16:15:48 -0700 Subject: [PATCH 1/4] Added tests for make_response and get_debug_flag to improve coverage of helpers.py --- tests/test_helpers.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index d5706521bd..19c364f535 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -22,6 +22,7 @@ from werkzeug.http import parse_cache_control_header, parse_options_header from werkzeug.http import http_date from flask._compat import StringIO, text_type +from flask.helpers import get_debug_flag, make_response def has_encoding(name): @@ -941,3 +942,52 @@ def test_safe_join_exceptions(self): for args in failing: with pytest.raises(NotFound): print(flask.safe_join(*args)) + +class TestHelpers(object): + + def test_get_debug_flag(self): + original_debug_value = os.environ.get('FLASK_DEBUG') or '' + os.environ['FLASK_DEBUG'] = '' + assert get_debug_flag() == None + assert get_debug_flag(default=True) == True + + os.environ['FLASK_DEBUG'] = '0' + assert get_debug_flag() == False + assert get_debug_flag(default=True) == False + + os.environ['FLASK_DEBUG'] = 'False' + assert get_debug_flag() == False + assert get_debug_flag(default=True) == False + + os.environ['FLASK_DEBUG'] = 'No' + assert get_debug_flag() == False + assert get_debug_flag(default=True) == False + + os.environ['FLASK_DEBUG'] = 'True' + assert get_debug_flag() == True + assert get_debug_flag(default=True) == True + + os.environ['FLASK_DEBUG'] = original_debug_value + + def test_make_response_no_args(self): + app = flask.Flask(__name__) + app.testing = True + @app.route('/') + def index(): + return flask.helpers.make_response() + c = app.test_client() + rv = c.get() + assert rv + + def test_make_response_with_args(self): + app = flask.Flask(__name__) + app.testing = True + @app.route('/') + def index(): + response = flask.helpers.make_response(flask.render_template_string('Hello World')) + response.headers['X-Parachutes'] = 'parachutes are cool' + return response + c = app.test_client() + rv = c.get() + assert rv + assert rv.headers['X-Parachutes'] == 'parachutes are cool' From fd4a363657f3b4f13611e029c52fcb8c806f7229 Mon Sep 17 00:00:00 2001 From: MikeTheReader Date: Mon, 22 May 2017 20:49:37 -0700 Subject: [PATCH 2/4] Modifications based on review --- tests/test_helpers.py | 44 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 19c364f535..a5863da95c 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -945,49 +945,35 @@ def test_safe_join_exceptions(self): class TestHelpers(object): - def test_get_debug_flag(self): - original_debug_value = os.environ.get('FLASK_DEBUG') or '' - os.environ['FLASK_DEBUG'] = '' + def test_get_debug_flag(self, monkeypatch): + monkeypatch.setenv('FLASK_DEBUG', '') assert get_debug_flag() == None assert get_debug_flag(default=True) == True - os.environ['FLASK_DEBUG'] = '0' + monkeypatch.setenv('FLASK_DEBUG', '0') assert get_debug_flag() == False assert get_debug_flag(default=True) == False - os.environ['FLASK_DEBUG'] = 'False' + monkeypatch.setenv('FLASK_DEBUG', 'False') assert get_debug_flag() == False assert get_debug_flag(default=True) == False - os.environ['FLASK_DEBUG'] = 'No' + monkeypatch.setenv('FLASK_DEBUG', 'No') assert get_debug_flag() == False assert get_debug_flag(default=True) == False - os.environ['FLASK_DEBUG'] = 'True' + monkeypatch.setenv('FLASK_DEBUG', 'True') assert get_debug_flag() == True assert get_debug_flag(default=True) == True - os.environ['FLASK_DEBUG'] = original_debug_value - - def test_make_response_no_args(self): + def test_make_response(self): app = flask.Flask(__name__) - app.testing = True - @app.route('/') - def index(): - return flask.helpers.make_response() - c = app.test_client() - rv = c.get() - assert rv + with app.test_request_context(): + rv = flask.helpers.make_response() + assert rv.status_code == 200 + assert rv.mimetype == 'text/html' - def test_make_response_with_args(self): - app = flask.Flask(__name__) - app.testing = True - @app.route('/') - def index(): - response = flask.helpers.make_response(flask.render_template_string('Hello World')) - response.headers['X-Parachutes'] = 'parachutes are cool' - return response - c = app.test_client() - rv = c.get() - assert rv - assert rv.headers['X-Parachutes'] == 'parachutes are cool' + rv = flask.helpers.make_response('Hello') + assert rv.status_code == 200 + assert rv.data == b'Hello' + assert rv.mimetype == 'text/html' From cd412b20dc64f3bfa980c9993e5a4dce39a22b94 Mon Sep 17 00:00:00 2001 From: MikeTheReader Date: Tue, 23 May 2017 07:51:57 -0700 Subject: [PATCH 3/4] Parameterize test_get_debug_flag --- tests/test_helpers.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index a5863da95c..1f623559ce 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -945,26 +945,20 @@ def test_safe_join_exceptions(self): class TestHelpers(object): - def test_get_debug_flag(self, monkeypatch): - monkeypatch.setenv('FLASK_DEBUG', '') - assert get_debug_flag() == None - assert get_debug_flag(default=True) == True - - monkeypatch.setenv('FLASK_DEBUG', '0') - assert get_debug_flag() == False - assert get_debug_flag(default=True) == False - - monkeypatch.setenv('FLASK_DEBUG', 'False') - assert get_debug_flag() == False - assert get_debug_flag(default=True) == False - - monkeypatch.setenv('FLASK_DEBUG', 'No') - assert get_debug_flag() == False - assert get_debug_flag(default=True) == False - - monkeypatch.setenv('FLASK_DEBUG', 'True') - assert get_debug_flag() == True - assert get_debug_flag(default=True) == True + @pytest.mark.parametrize("debug, expected_flag, expected_default_flag", [ + ('', None, True), + ('0', False, False), + ('False', False, False), + ('No', False, False), + ('True', True, True) + ]) + def test_get_debug_flag(self, monkeypatch, debug, expected_flag, expected_default_flag): + monkeypatch.setenv('FLASK_DEBUG', debug) + if expected_flag is None: + assert get_debug_flag() is None + else: + assert get_debug_flag() == expected_flag + assert get_debug_flag(default=True) == expected_default_flag def test_make_response(self): app = flask.Flask(__name__) From 7c882a457b734cd0a38d41c7e930e375a6ce6e4c Mon Sep 17 00:00:00 2001 From: MikeTheReader Date: Tue, 23 May 2017 07:59:53 -0700 Subject: [PATCH 4/4] Replace double quotes with single quotes --- tests/test_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 1f623559ce..5af95f18fd 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -945,7 +945,7 @@ def test_safe_join_exceptions(self): class TestHelpers(object): - @pytest.mark.parametrize("debug, expected_flag, expected_default_flag", [ + @pytest.mark.parametrize('debug, expected_flag, expected_default_flag', [ ('', None, True), ('0', False, False), ('False', False, False),