[pre-commit.ci lite] apply automatic fixes
This commit is contained in:
parent
fb763eaf61
commit
554fad132e
3 changed files with 139 additions and 0 deletions
|
|
@ -9,27 +9,32 @@ import flask
|
|||
TEST_KEY = "foo"
|
||||
SECRET_KEY = "config"
|
||||
|
||||
|
||||
def common_object_test(app):
|
||||
assert app.secret_key == "config"
|
||||
assert app.config["TEST_KEY"] == "foo"
|
||||
assert "TestConfig" not in app.config
|
||||
|
||||
|
||||
def test_config_from_pyfile():
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_pyfile(f"{__file__.rsplit('.', 1)[0]}.py")
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_config_from_object():
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_config_from_file_json():
|
||||
app = flask.Flask(__name__)
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_config_from_file_toml():
|
||||
tomllib = pytest.importorskip("tomllib", reason="tomllib added in 3.11")
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -39,6 +44,7 @@ def test_config_from_file_toml():
|
|||
)
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_from_prefixed_env(monkeypatch):
|
||||
monkeypatch.setenv("FLASK_STRING", "value")
|
||||
monkeypatch.setenv("FLASK_BOOL", "true")
|
||||
|
|
@ -59,6 +65,7 @@ def test_from_prefixed_env(monkeypatch):
|
|||
assert app.config["DICT"] == {"k": "v"}
|
||||
assert "OTHER" not in app.config
|
||||
|
||||
|
||||
def test_from_prefixed_env_custom_prefix(monkeypatch):
|
||||
monkeypatch.setenv("FLASK_A", "a")
|
||||
monkeypatch.setenv("NOT_FLASK_A", "b")
|
||||
|
|
@ -68,6 +75,7 @@ def test_from_prefixed_env_custom_prefix(monkeypatch):
|
|||
|
||||
assert app.config["A"] == "b"
|
||||
|
||||
|
||||
def test_from_prefixed_env_nested(monkeypatch):
|
||||
monkeypatch.setenv("FLASK_EXIST__ok", "other")
|
||||
monkeypatch.setenv("FLASK_EXIST__inner__ik", "2")
|
||||
|
|
@ -98,6 +106,7 @@ def test_from_prefixed_env_nested(monkeypatch):
|
|||
|
||||
assert app.config["NEW"] == {"K": "v"}
|
||||
|
||||
|
||||
def test_config_from_mapping():
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_mapping({"SECRET_KEY": "config", "TEST_KEY": "foo"})
|
||||
|
|
@ -119,6 +128,7 @@ def test_config_from_mapping():
|
|||
with pytest.raises(TypeError):
|
||||
app.config.from_mapping({}, {})
|
||||
|
||||
|
||||
def test_config_from_class():
|
||||
class Base:
|
||||
TEST_KEY = "foo"
|
||||
|
|
@ -130,6 +140,7 @@ def test_config_from_class():
|
|||
app.config.from_object(Test)
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_config_from_envvar(monkeypatch):
|
||||
monkeypatch.setattr("os.environ", {})
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -146,6 +157,7 @@ def test_config_from_envvar(monkeypatch):
|
|||
assert app.config.from_envvar("FOO_SETTINGS")
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_config_from_envvar_missing(monkeypatch):
|
||||
monkeypatch.setattr("os.environ", {"FOO_SETTINGS": "missing.cfg"})
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -158,6 +170,7 @@ def test_config_from_envvar_missing(monkeypatch):
|
|||
assert msg.endswith("missing.cfg'")
|
||||
assert not app.config.from_envvar("FOO_SETTINGS", silent=True)
|
||||
|
||||
|
||||
def test_config_missing():
|
||||
app = flask.Flask(__name__)
|
||||
with pytest.raises(IOError) as e:
|
||||
|
|
@ -169,6 +182,7 @@ def test_config_missing():
|
|||
assert msg.endswith("missing.cfg'")
|
||||
assert not app.config.from_pyfile("missing.cfg", silent=True)
|
||||
|
||||
|
||||
def test_config_missing_file():
|
||||
app = flask.Flask(__name__)
|
||||
with pytest.raises(IOError) as e:
|
||||
|
|
@ -180,6 +194,7 @@ def test_config_missing_file():
|
|||
assert msg.endswith("missing.json'")
|
||||
assert not app.config.from_file("missing.json", load=json.load, silent=True)
|
||||
|
||||
|
||||
def test_custom_config_class():
|
||||
class Config(flask.Config):
|
||||
pass
|
||||
|
|
@ -192,11 +207,13 @@ def test_custom_config_class():
|
|||
app.config.from_object(__name__)
|
||||
common_object_test(app)
|
||||
|
||||
|
||||
def test_session_lifetime():
|
||||
app = flask.Flask(__name__)
|
||||
app.config["PERMANENT_SESSION_LIFETIME"] = 42
|
||||
assert app.permanent_session_lifetime.seconds == 42
|
||||
|
||||
|
||||
def test_get_namespace():
|
||||
app = flask.Flask(__name__)
|
||||
app.config["FOO_OPTION_1"] = "foo option 1"
|
||||
|
|
@ -222,6 +239,7 @@ def test_get_namespace():
|
|||
assert "bar stuff 1" == bar_options["BAR_STUFF_1"]
|
||||
assert "bar stuff 2" == bar_options["BAR_STUFF_2"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])
|
||||
def test_from_pyfile_weird_encoding(tmp_path, encoding):
|
||||
f = tmp_path / "my_config.py"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue