Skip to content
Myr

test_config.py

This file

Path

tests/test_config.py

250 lines · 7.6 KiB · Python

Last changed by

Merge branch '2.3.x'David Lordbefore adoption57e926c

History of this file

Take it

The walk stopped after 200 commits: lines older than 48ee204 point at it.

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 1–30why, one step back
1import json
2import os34import pytest56import flask78# config keys used for the TestConfig9TEST_KEY = "foo"10SECRET_KEY = "config"111213def common_object_test(app):14 assert app.secret_key == "config"15 assert app.config["TEST_KEY"] == "foo"16 assert "TestConfig" not in app.config171819def test_config_from_pyfile():20 app = flask.Flask(__name__)21 app.config.from_pyfile(f"{__file__.rsplit('.', 1)[0]}.py")22 common_object_test(app)232425def test_config_from_object():26 app = flask.Flask(__name__)27 app.config.from_object(__name__)28 common_object_test(app)2930

Merge pull request #4992 from AntoineMath/file_modeDavid Lordbefore adoption2ad4d8d

line 31why, one step back
31def test_config_from_file_json():

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 32–34why, one step back
32 app = flask.Flask(__name__)
33 current_dir = os.path.dirname(os.path.abspath(__file__))34 app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)

Merge pull request #4992 from AntoineMath/file_modeDavid Lordbefore adoption2ad4d8d

lines 35–44why, one step back
35 common_object_test(app)
363738def test_config_from_file_toml():39 tomllib = pytest.importorskip("tomllib", reason="tomllib added in 3.11")40 app = flask.Flask(__name__)41 current_dir = os.path.dirname(os.path.abspath(__file__))42 app.config.from_file(43 os.path.join(current_dir, "static", "config.toml"), tomllib.load, text=False44 )

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 45–47why, one step back
45 common_object_test(app)
4647

Merge pull request #4479 from pgjones/env_configDavid Lordbefore adoption2f5a2ab

lines 48–109why, one step back
48def test_from_prefixed_env(monkeypatch):
49 monkeypatch.setenv("FLASK_STRING", "value")50 monkeypatch.setenv("FLASK_BOOL", "true")51 monkeypatch.setenv("FLASK_INT", "1")52 monkeypatch.setenv("FLASK_FLOAT", "1.2")53 monkeypatch.setenv("FLASK_LIST", "[1, 2]")54 monkeypatch.setenv("FLASK_DICT", '{"k": "v"}')55 monkeypatch.setenv("NOT_FLASK_OTHER", "other")5657 app = flask.Flask(__name__)58 app.config.from_prefixed_env()5960 assert app.config["STRING"] == "value"61 assert app.config["BOOL"] is True62 assert app.config["INT"] == 163 assert app.config["FLOAT"] == 1.264 assert app.config["LIST"] == [1, 2]65 assert app.config["DICT"] == {"k": "v"}66 assert "OTHER" not in app.config676869def test_from_prefixed_env_custom_prefix(monkeypatch):70 monkeypatch.setenv("FLASK_A", "a")71 monkeypatch.setenv("NOT_FLASK_A", "b")7273 app = flask.Flask(__name__)74 app.config.from_prefixed_env("NOT_FLASK")7576 assert app.config["A"] == "b"777879def test_from_prefixed_env_nested(monkeypatch):80 monkeypatch.setenv("FLASK_EXIST__ok", "other")81 monkeypatch.setenv("FLASK_EXIST__inner__ik", "2")82 monkeypatch.setenv("FLASK_EXIST__new__more", '{"k": false}')83 monkeypatch.setenv("FLASK_NEW__K", "v")8485 app = flask.Flask(__name__)86 app.config["EXIST"] = {"ok": "value", "flag": True, "inner": {"ik": 1}}87 app.config.from_prefixed_env()8889 if os.name != "nt":90 assert app.config["EXIST"] == {91 "ok": "other",92 "flag": True,93 "inner": {"ik": 2},94 "new": {"more": {"k": False}},95 }96 else:97 # Windows env var keys are always uppercase.98 assert app.config["EXIST"] == {99 "ok": "value",100 "OK": "other",101 "flag": True,102 "inner": {"ik": 1},103 "INNER": {"IK": 2},104 "NEW": {"MORE": {"k": False}},105 }106107 assert app.config["NEW"] == {"K": "v"}108109

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 110–123why, one step back
110def test_config_from_mapping():
111 app = flask.Flask(__name__)112 app.config.from_mapping({"SECRET_KEY": "config", "TEST_KEY": "foo"})113 common_object_test(app)114115 app = flask.Flask(__name__)116 app.config.from_mapping([("SECRET_KEY", "config"), ("TEST_KEY", "foo")])117 common_object_test(app)118119 app = flask.Flask(__name__)120 app.config.from_mapping(SECRET_KEY="config", TEST_KEY="foo")121 common_object_test(app)122123 app = flask.Flask(__name__)

Merge pull request #4613 from citramon/test_for_from_mapping_config_skip_not_upperDavid Lordbefore adoptionc6e3fc8

lines 124–127why, one step back
124 app.config.from_mapping(SECRET_KEY="config", TEST_KEY="foo", skip_key="skip")
125 common_object_test(app)126127 app = flask.Flask(__name__)

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 128–146why, one step back
128 with pytest.raises(TypeError):
129 app.config.from_mapping({}, {})130131132def test_config_from_class():133 class Base:134 TEST_KEY = "foo"135136 class Test(Base):137 SECRET_KEY = "config"138139 app = flask.Flask(__name__)140 app.config.from_object(Test)141 common_object_test(app)142143144def test_config_from_envvar(monkeypatch):145 monkeypatch.setattr("os.environ", {})146 app = flask.Flask(__name__)

Merge branch '2.1.x'David Lordbefore adoptionf976d5b

line 147why, one step back
147

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 148–149why, one step back
148 with pytest.raises(RuntimeError) as e:
149 app.config.from_envvar("FOO_SETTINGS")

Merge branch '2.1.x'David Lordbefore adoptionf976d5b

lines 150–151why, one step back
150
151 assert "'FOO_SETTINGS' is not set" in str(e.value)

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 152–162why, one step back
152 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)
153154 monkeypatch.setattr(155 "os.environ", {"FOO_SETTINGS": f"{__file__.rsplit('.', 1)[0]}.py"}156 )157 assert app.config.from_envvar("FOO_SETTINGS")158 common_object_test(app)159160161def test_config_from_envvar_missing(monkeypatch):162 monkeypatch.setattr("os.environ", {"FOO_SETTINGS": "missing.cfg"})

Merge branch '2.1.x'David Lordbefore adoptionf976d5b

line 163why, one step back
163 app = flask.Flask(__name__)

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 164–243why, one step back
164 with pytest.raises(IOError) as e:
165 app.config.from_envvar("FOO_SETTINGS")166 msg = str(e.value)167 assert msg.startswith(168 "[Errno 2] Unable to load configuration file (No such file or directory):"169 )170 assert msg.endswith("missing.cfg'")171 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)172173174def test_config_missing():175 app = flask.Flask(__name__)176 with pytest.raises(IOError) as e:177 app.config.from_pyfile("missing.cfg")178 msg = str(e.value)179 assert msg.startswith(180 "[Errno 2] Unable to load configuration file (No such file or directory):"181 )182 assert msg.endswith("missing.cfg'")183 assert not app.config.from_pyfile("missing.cfg", silent=True)184185186def test_config_missing_file():187 app = flask.Flask(__name__)188 with pytest.raises(IOError) as e:189 app.config.from_file("missing.json", load=json.load)190 msg = str(e.value)191 assert msg.startswith(192 "[Errno 2] Unable to load configuration file (No such file or directory):"193 )194 assert msg.endswith("missing.json'")195 assert not app.config.from_file("missing.json", load=json.load, silent=True)196197198def test_custom_config_class():199 class Config(flask.Config):200 pass201202 class Flask(flask.Flask):203 config_class = Config204205 app = Flask(__name__)206 assert isinstance(app.config, Config)207 app.config.from_object(__name__)208 common_object_test(app)209210211def test_session_lifetime():212 app = flask.Flask(__name__)213 app.config["PERMANENT_SESSION_LIFETIME"] = 42214 assert app.permanent_session_lifetime.seconds == 42215216217def test_get_namespace():218 app = flask.Flask(__name__)219 app.config["FOO_OPTION_1"] = "foo option 1"220 app.config["FOO_OPTION_2"] = "foo option 2"221 app.config["BAR_STUFF_1"] = "bar stuff 1"222 app.config["BAR_STUFF_2"] = "bar stuff 2"223 foo_options = app.config.get_namespace("FOO_")224 assert 2 == len(foo_options)225 assert "foo option 1" == foo_options["option_1"]226 assert "foo option 2" == foo_options["option_2"]227 bar_options = app.config.get_namespace("BAR_", lowercase=False)228 assert 2 == len(bar_options)229 assert "bar stuff 1" == bar_options["STUFF_1"]230 assert "bar stuff 2" == bar_options["STUFF_2"]231 foo_options = app.config.get_namespace("FOO_", trim_namespace=False)232 assert 2 == len(foo_options)233 assert "foo option 1" == foo_options["foo_option_1"]234 assert "foo option 2" == foo_options["foo_option_2"]235 bar_options = app.config.get_namespace(236 "BAR_", lowercase=False, trim_namespace=False237 )238 assert 2 == len(bar_options)239 assert "bar stuff 1" == bar_options["BAR_STUFF_1"]240 assert "bar stuff 2" == bar_options["BAR_STUFF_2"]241242243@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])

Merge branch '2.3.x'David Lordbefore adoption57e926c

lines 244–246why, one step back
244def test_from_pyfile_weird_encoding(tmp_path, encoding):
245 f = tmp_path / "my_config.py"246 f.write_text(f'# -*- coding: {encoding} -*-\nTEST_VALUE = "föö"\n', encoding)

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

line 247why, one step back
247 app = flask.Flask(__name__)

Merge branch '2.3.x'David Lordbefore adoption57e926c

line 248why, one step back
248 app.config.from_pyfile(os.fspath(f))

Merge pull request #4189 from mgu/feat/use-exclude-patterns-reloaderDavid Lordbefore adoption48ee204

lines 249–250why, one step back
249 value = app.config["TEST_VALUE"]
250 assert value == "föö"