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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
import pytest
import flask
from flask.globals import app_ctx
from flask.globals import request_ctx
def test_basic_url_generation(app):
app.config["SERVER_NAME"] = "localhost"
app.config["PREFERRED_URL_SCHEME"] = "https"
@app.route("/")
def index():
pass
with app.app_context():
rv = flask.url_for("index")
assert rv == "https://localhost/"
def test_url_generation_requires_server_name(app):
with app.app_context():
with pytest.raises(RuntimeError):
flask.url_for("index")
def test_url_generation_without_context_fails():
with pytest.raises(RuntimeError):
flask.url_for("index")
def test_request_context_means_app_context(app):
with app.test_request_context():
assert flask.current_app._get_current_object() is app
assert not flask.current_app
def test_app_context_provides_current_app(app):
with app.app_context():
assert flask.current_app._get_current_object() is app
assert not flask.current_app
def test_app_tearing_down(app):
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
with app.app_context():
pass
assert cleanup_stuff == [None]
def test_app_tearing_down_with_previous_exception(app):
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
try:
raise Exception("dummy")
except Exception:
pass
with app.app_context():
pass
assert cleanup_stuff == [None]
def test_app_tearing_down_with_handled_exception_by_except_block(app):
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
with app.app_context():
try:
raise Exception("dummy")
except Exception:
pass
assert cleanup_stuff == [None]
def test_app_tearing_down_with_handled_exception_by_app_handler(app, client):
app.config["PROPAGATE_EXCEPTIONS"] = True
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
@app.route("/")
def index():
raise Exception("dummy")
@app.errorhandler(Exception)
def handler(f):
return flask.jsonify(str(f))
with app.app_context():
client.get("/")
assert cleanup_stuff == [None]
def test_app_tearing_down_with_unhandled_exception(app, client):
app.config["PROPAGATE_EXCEPTIONS"] = True
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
@app.route("/")
def index():
raise ValueError("dummy")
with pytest.raises(ValueError, match="dummy"):
with app.app_context():
client.get("/")
assert len(cleanup_stuff) == 1
assert isinstance(cleanup_stuff[0], ValueError)
assert str(cleanup_stuff[0]) == "dummy"
def test_app_ctx_globals_methods(app, app_ctx):
# get
assert flask.g.get("foo") is None
assert flask.g.get("foo", "bar") == "bar"
# __contains__
assert "foo" not in flask.g
flask.g.foo = "bar"
assert "foo" in flask.g
# setdefault
flask.g.setdefault("bar", "the cake is a lie")
flask.g.setdefault("bar", "hello world")
assert flask.g.bar == "the cake is a lie"
# pop
assert flask.g.pop("bar") == "the cake is a lie"
with pytest.raises(KeyError):
flask.g.pop("bar")
assert flask.g.pop("bar", "more cake") == "more cake"
# __iter__
assert list(flask.g) == ["foo"]
# __repr__
assert repr(flask.g) == "<flask.g of 'flask_test'>"
def test_custom_app_ctx_globals_class(app):
class CustomRequestGlobals:
def __init__(self):
self.spam = "eggs"
app.app_ctx_globals_class = CustomRequestGlobals
with app.app_context():
assert flask.render_template_string("{{ g.spam }}") == "eggs"
def test_context_refcounts(app, client):
called = []
@app.teardown_request
def teardown_req(error=None):
called.append("request")
@app.teardown_appcontext
def teardown_app(error=None):
called.append("app")
@app.route("/")
def index():
with app_ctx:
with request_ctx:
pass
assert flask.request.environ["werkzeug.request"] is not None
return ""
res = client.get("/")
assert res.status_code == 200
assert res.data == b""
assert called == ["request", "app"]
def test_clean_pop(app):
app.testing = False
called = []
@app.teardown_request
def teardown_req(error=None):
raise ZeroDivisionError
@app.teardown_appcontext
def teardown_app(error=None):
called.append("TEARDOWN")
with app.app_context():
called.append(flask.current_app.name)
assert called == ["flask_test", "TEARDOWN"]
assert not flask.current_app
|