-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_app.py
68 lines (54 loc) · 2.14 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import pytest
from noggin.app import create_app
@pytest.fixture
def environ_config():
varname = "NOGGIN_CONFIG_PATH"
old_config_value = os.getenv(varname)
os.environ[varname] = "defaults.py"
yield
if old_config_value is None:
del os.environ[varname]
else:
os.environ[varname] = old_config_value
def test_env_var(mocker, app_config, environ_config):
from_envvar = mocker.patch("flask.config.Config.from_envvar")
create_app(app_config)
from_envvar.assert_called_once_with("NOGGIN_CONFIG_PATH")
def test_templates_reload(app_config):
config = app_config.copy()
config["TEMPLATES_AUTO_RELOAD"] = True
app = create_app(config)
assert app.jinja_env.auto_reload is True
def test_logging(mocker, app_config):
config = app_config.copy()
config["LOGGING"] = "dummy-logging-config"
logging_config = mocker.patch("noggin.app.dictConfig")
create_app(config)
logging_config.assert_called_once_with("dummy-logging-config")
def test_templates_custom_directory_insertion(app_config, tmp_path):
tpl_dir = tmp_path / "templates"
os.makedirs(tpl_dir)
config = app_config.copy()
config["TEMPLATES_CUSTOM_DIRECTORIES"] = [tpl_dir]
app = create_app(config)
# Use the template placeholder
with open(tpl_dir / "after-navbar.html", "w") as tpl:
tpl.write("TESTING TEMPLATES CUSTOM DIR\n")
with app.test_client() as client:
response = client.get('/')
assert response.status_code == 200
assert "TESTING TEMPLATES CUSTOM DIR" in response.get_data(as_text=True)
def test_templates_custom_directory_override(app_config, tmp_path):
tpl_dir = tmp_path / "templates"
os.makedirs(tpl_dir)
config = app_config.copy()
config["TEMPLATES_CUSTOM_DIRECTORIES"] = [tpl_dir]
app = create_app(config)
# Override the whole page template
with open(tpl_dir / "index.html", "w") as tpl:
tpl.write("TESTING TEMPLATES CUSTOM DIR\n")
with app.test_client() as client:
response = client.get('/')
assert response.status_code == 200
assert response.get_data(as_text=True) == "TESTING TEMPLATES CUSTOM DIR"