Skip to content
Myr

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 file

Take 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 back
1# -*- coding: utf-8 -*-
2"""3 tests.test_config4 ~~~~~~~~~~~~~~~~~5

Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c

line 6why, one step back
6 :copyright: © 2010 by the Pallets team.

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 7–19why, one step back
7 :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 TestConfig

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 20–21why, one step back
20TEST_KEY = "foo"
21SECRET_KEY = "config"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 22–24why, one step back
22
2324def common_object_test(app):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 25–27why, one step back
25 assert app.secret_key == "config"
26 assert app.config["TEST_KEY"] == "foo"27 assert "TestConfig" not in app.config

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 28–31why, one step back
28
2930def test_config_from_file():31 app = flask.Flask(__name__)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 32why, one step back
32 app.config.from_pyfile(__file__.rsplit(".", 1)[0] + ".py")

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 33–44why, one step back
33 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 back
45 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 back
46 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 back
51 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 back
52 common_object_test(app)
5354 app = flask.Flask(__name__)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 55why, one step back
55 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 back
56 common_object_test(app)
5758 app = flask.Flask(__name__)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 59why, one step back
59 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 back
60 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 back
64 app.config.from_mapping({}, {})

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 65–68why, one step back
65
6667def test_config_from_class():68 class Base(object):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 69why, one step back
69 TEST_KEY = "foo"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 70–71why, one step back
70
71 class Test(Base):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 72why, one step back
72 SECRET_KEY = "config"

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 73why, one step back
73

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 74–84why, one step back
74 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 back
85 app.config.from_envvar("FOO_SETTINGS")

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 86why, one step back
86 assert "'FOO_SETTINGS' is not set" in str(e.value)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 87why, one step back
87 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 88why, one step back
88

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 89–90why, one step back
89 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 back
91 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 back
99 os.environ = {"FOO_SETTINGS": "missing.cfg"}

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 100–101why, one step back
100 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 back
102 app.config.from_envvar("FOO_SETTINGS")

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 103why, one step back
103 msg = str(e.value)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 104–107why, one step back
104 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 back
108 assert msg.endswith("missing.cfg'")

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 109why, one step back
109 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 back
110 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 back
117 app.config.from_pyfile("missing.cfg")

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 118why, one step back
118 msg = str(e.value)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 119–121why, one step back
119 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 back
122 assert msg.endswith("missing.cfg'")

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 123why, one step back
123 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 back
124
125126def 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 back
129 app.config.from_json("missing.json")

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 130why, one step back
130 msg = str(e.value)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 131–133why, one step back
131 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 back
134 assert msg.endswith("missing.json'")

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 135why, one step back
135 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 back
136
137138def test_custom_config_class():139 class Config(flask.Config):140 pass141142 class Flask(flask.Flask):143 config_class = Config

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 144why, one step back
144

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 145–152why, one step back
145 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 back
153 app.config["PERMANENT_SESSION_LIFETIME"] = 42

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 154–158why, one step back
154 assert app.permanent_session_lifetime.seconds == 42
155156157def test_send_file_max_age():158 app = flask.Flask(__name__)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 159why, one step back
159 app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 160why, one step back
160 assert app.send_file_max_age_default.seconds == 3600

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 161why, one step back
161 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 back
162 assert app.send_file_max_age_default.seconds == 7200
163164165def test_get_namespace():166 app = flask.Flask(__name__)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 167–171why, one step back
167 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 back
172 assert 2 == len(foo_options)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 173–175why, one step back
173 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 back
176 assert 2 == len(bar_options)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 177–179why, one step back
177 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 back
180 assert 2 == len(foo_options)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 181–185why, one step back
181 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 back
186 assert 2 == len(bar_options)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 187–188why, one step back
187 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 back
189
190

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 191why, one step back
191@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 back
192def test_from_pyfile_weird_encoding(tmpdir, encoding):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 193–196why, one step back
193 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 back
197 # -*- coding: {0} -*-
198 TEST_VALUE = "föö"

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 199–203why, one step back
199 """.format(
200 encoding201 )202 ).encode(encoding)203 )

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 204–205why, one step back
204 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 back
206 value = app.config["TEST_VALUE"]

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 207–208why, one step back
207 if PY2:
208 value = value.decode(encoding)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 209why, one step back
209 assert value == u"föö"