test_config.py
This file
Path
tests/test_config.py
209 lines · 6.0 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
The walk stopped after 200 commits: lines older than 49f3e96 point at it.
Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3 tests.test_config4 ~~~~~~~~~~~~~~~~~5Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
line 6why, one step back6 :copyright: © 2010 by the Pallets team.Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 7–19why, one step back7 :license: BSD, see LICENSE for more details.8"""910from datetime import timedelta11import os12import textwrap1314import flask15from flask._compat import PY216import pytest171819# config keys used for the TestConfigMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 20–21why, one step back20TEST_KEY = "foo"21SECRET_KEY = "config"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 22–24why, one step back222324def common_object_test(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 25–27why, one step back25 assert app.secret_key == "config"26 assert app.config["TEST_KEY"] == "foo"27 assert "TestConfig" not in app.configMerge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 28–31why, one step back282930def test_config_from_file():31 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 32why, one step back32 app.config.from_pyfile(__file__.rsplit(".", 1)[0] + ".py")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 33–44why, one step back33 common_object_test(app)343536def test_config_from_object():37 app = flask.Flask(__name__)38 app.config.from_object(__name__)39 common_object_test(app)404142def test_config_from_json():43 app = flask.Flask(__name__)44 current_dir = os.path.dirname(os.path.abspath(__file__))Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 45why, one step back45 app.config.from_json(os.path.join(current_dir, "static", "config.json"))Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 46–50why, one step back46 common_object_test(app)474849def test_config_from_mapping():50 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 51why, one step back51 app.config.from_mapping({"SECRET_KEY": "config", "TEST_KEY": "foo"})Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 52–54why, one step back52 common_object_test(app)5354 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 55why, one step back55 app.config.from_mapping([("SECRET_KEY", "config"), ("TEST_KEY", "foo")])Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 56–58why, one step back56 common_object_test(app)5758 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 59why, one step back59 app.config.from_mapping(SECRET_KEY="config", TEST_KEY="foo")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 60–63why, one step back60 common_object_test(app)6162 app = flask.Flask(__name__)63 with pytest.raises(TypeError):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 64why, one step back64 app.config.from_mapping({}, {})Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 65–68why, one step back656667def test_config_from_class():68 class Base(object):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 69why, one step back69 TEST_KEY = "foo"Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 70–71why, one step back7071 class Test(Base):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 72why, one step back72 SECRET_KEY = "config"Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d
line 73why, one step back73Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 74–84why, one step back74 app = flask.Flask(__name__)75 app.config.from_object(Test)76 common_object_test(app)777879def test_config_from_envvar():80 env = os.environ81 try:82 os.environ = {}83 app = flask.Flask(__name__)84 with pytest.raises(RuntimeError) as e:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 85why, one step back85 app.config.from_envvar("FOO_SETTINGS")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 86why, one step back86 assert "'FOO_SETTINGS' is not set" in str(e.value)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 87why, one step back87 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 88why, one step back88Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 89–90why, one step back89 os.environ = {"FOO_SETTINGS": __file__.rsplit(".", 1)[0] + ".py"}90 assert app.config.from_envvar("FOO_SETTINGS")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 91–98why, one step back91 common_object_test(app)92 finally:93 os.environ = env949596def test_config_from_envvar_missing():97 env = os.environ98 try:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 99why, one step back99 os.environ = {"FOO_SETTINGS": "missing.cfg"}Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 100–101why, one step back100 with pytest.raises(IOError) as e:101 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 102why, one step back102 app.config.from_envvar("FOO_SETTINGS")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 103why, one step back103 msg = str(e.value)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 104–107why, one step back104 assert msg.startswith(105 "[Errno 2] Unable to load configuration "106 "file (No such file or directory):"107 )Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 108why, one step back108 assert msg.endswith("missing.cfg'")Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 109why, one step back109 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 110–116why, one step back110 finally:111 os.environ = env112113114def test_config_missing():115 app = flask.Flask(__name__)116 with pytest.raises(IOError) as e:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 117why, one step back117 app.config.from_pyfile("missing.cfg")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 118why, one step back118 msg = str(e.value)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 119–121why, one step back119 assert msg.startswith(120 "[Errno 2] Unable to load configuration " "file (No such file or directory):"121 )Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 122why, one step back122 assert msg.endswith("missing.cfg'")Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 123why, one step back123 assert not app.config.from_pyfile("missing.cfg", silent=True)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 124–128why, one step back124125126def test_config_missing_json():127 app = flask.Flask(__name__)128 with pytest.raises(IOError) as e:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 129why, one step back129 app.config.from_json("missing.json")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 130why, one step back130 msg = str(e.value)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 131–133why, one step back131 assert msg.startswith(132 "[Errno 2] Unable to load configuration " "file (No such file or directory):"133 )Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 134why, one step back134 assert msg.endswith("missing.json'")Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 135why, one step back135 assert not app.config.from_json("missing.json", silent=True)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 136–143why, one step back136137138def test_custom_config_class():139 class Config(flask.Config):140 pass141142 class Flask(flask.Flask):143 config_class = ConfigMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 144why, one step back144Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 145–152why, one step back145 app = Flask(__name__)146 assert isinstance(app.config, Config)147 app.config.from_object(__name__)148 common_object_test(app)149150151def test_session_lifetime():152 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 153why, one step back153 app.config["PERMANENT_SESSION_LIFETIME"] = 42Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 154–158why, one step back154 assert app.permanent_session_lifetime.seconds == 42155156157def test_send_file_max_age():158 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 159why, one step back159 app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 160why, one step back160 assert app.send_file_max_age_default.seconds == 3600Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 161why, one step back161 app.config["SEND_FILE_MAX_AGE_DEFAULT"] = timedelta(hours=2)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 162–166why, one step back162 assert app.send_file_max_age_default.seconds == 7200163164165def test_get_namespace():166 app = flask.Flask(__name__)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 167–171why, one step back167 app.config["FOO_OPTION_1"] = "foo option 1"168 app.config["FOO_OPTION_2"] = "foo option 2"169 app.config["BAR_STUFF_1"] = "bar stuff 1"170 app.config["BAR_STUFF_2"] = "bar stuff 2"171 foo_options = app.config.get_namespace("FOO_")Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 172why, one step back172 assert 2 == len(foo_options)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 173–175why, one step back173 assert "foo option 1" == foo_options["option_1"]174 assert "foo option 2" == foo_options["option_2"]175 bar_options = app.config.get_namespace("BAR_", lowercase=False)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 176why, one step back176 assert 2 == len(bar_options)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 177–179why, one step back177 assert "bar stuff 1" == bar_options["STUFF_1"]178 assert "bar stuff 2" == bar_options["STUFF_2"]179 foo_options = app.config.get_namespace("FOO_", trim_namespace=False)Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 180why, one step back180 assert 2 == len(foo_options)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 181–185why, one step back181 assert "foo option 1" == foo_options["foo_option_1"]182 assert "foo option 2" == foo_options["foo_option_2"]183 bar_options = app.config.get_namespace(184 "BAR_", lowercase=False, trim_namespace=False185 )Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 186why, one step back186 assert 2 == len(bar_options)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 187–188why, one step back187 assert "bar stuff 1" == bar_options["BAR_STUFF_1"]188 assert "bar stuff 2" == bar_options["BAR_STUFF_2"]Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 189–190why, one step back189190Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 191why, one step back191@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
line 192why, one step back192def test_from_pyfile_weird_encoding(tmpdir, encoding):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 193–196why, one step back193 f = tmpdir.join("my_config.py")194 f.write_binary(195 textwrap.dedent(196 u"""Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 197–198why, one step back197 # -*- coding: {0} -*-198 TEST_VALUE = "föö"Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 199–203why, one step back199 """.format(200 encoding201 )202 ).encode(encoding)203 )Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 204–205why, one step back204 app = flask.Flask(__name__)205 app.config.from_pyfile(str(f))Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 206why, one step back206 value = app.config["TEST_VALUE"]Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96
lines 207–208why, one step back207 if PY2:208 value = value.decode(encoding)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 209why, one step back209 assert value == u"föö"