test_appctx.py
This file
Path
tests/test_appctx.py
221 lines · 5.1 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
The walk stopped after 200 commits: lines older than c74f469 point at it.
Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 tests.appctx4 ~~~~~~~~~~~~56 Tests the application context.7Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
line 8why, one step back8 :copyright: © 2010 by the Pallets team.Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 9–17why, one step back9 :license: BSD, see LICENSE for more details.10"""1112import pytest1314import flask151617def test_basic_url_generation(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 18–19why, one step back18 app.config["SERVER_NAME"] = "localhost"19 app.config["PREFERRED_URL_SCHEME"] = "https"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 20why, one step back20Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 21why, one step back21 @app.route("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 22–25why, one step back22 def index():23 pass2425 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 26–27why, one step back26 rv = flask.url_for("index")27 assert rv == "https://localhost/"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 28–32why, one step back282930def test_url_generation_requires_server_name(app):31 with app.app_context():32 with pytest.raises(RuntimeError):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 33why, one step back33 flask.url_for("index")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 34–37why, one step back343536def test_url_generation_without_context_fails():37 with pytest.raises(RuntimeError):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 38why, one step back38 flask.url_for("index")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 39–73why, one step back394041def test_request_context_means_app_context(app):42 with app.test_request_context():43 assert flask.current_app._get_current_object() == app44 assert flask._app_ctx_stack.top is None454647def test_app_context_provides_current_app(app):48 with app.app_context():49 assert flask.current_app._get_current_object() == app50 assert flask._app_ctx_stack.top is None515253def test_app_tearing_down(app):54 cleanup_stuff = []5556 @app.teardown_appcontext57 def cleanup(exception):58 cleanup_stuff.append(exception)5960 with app.app_context():61 pass6263 assert cleanup_stuff == [None]646566def test_app_tearing_down_with_previous_exception(app):67 cleanup_stuff = []6869 @app.teardown_appcontext70 def cleanup(exception):71 cleanup_stuff.append(exception)7273 try:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 74why, one step back74 raise Exception("dummy")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 75–92why, one step back75 except Exception:76 pass7778 with app.app_context():79 pass8081 assert cleanup_stuff == [None]828384def test_app_tearing_down_with_handled_exception_by_except_block(app):85 cleanup_stuff = []8687 @app.teardown_appcontext88 def cleanup(exception):89 cleanup_stuff.append(exception)9091 with app.app_context():92 try:Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 93why, one step back93 raise Exception("dummy")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 94–100why, one step back94 except Exception:95 pass9697 assert cleanup_stuff == [None]9899100def test_app_tearing_down_with_handled_exception_by_app_handler(app, client):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 101why, one step back101 app.config["PROPAGATE_EXCEPTIONS"] = TrueMerge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 102–107why, one step back102 cleanup_stuff = []103104 @app.teardown_appcontext105 def cleanup(exception):106 cleanup_stuff.append(exception)107Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 108why, one step back108 @app.route("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 109why, one step back109 def index():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 110why, one step back110 raise Exception("dummy")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 111–116why, one step back111112 @app.errorhandler(Exception)113 def handler(f):114 return flask.jsonify(str(f))115116 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 117why, one step back117 client.get("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 118–122why, one step back118119 assert cleanup_stuff == [None]120121122def test_app_tearing_down_with_unhandled_exception(app, client):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 123why, one step back123 app.config["PROPAGATE_EXCEPTIONS"] = TrueMerge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 124–129why, one step back124 cleanup_stuff = []125126 @app.teardown_appcontext127 def cleanup(exception):128 cleanup_stuff.append(exception)129Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 130why, one step back130 @app.route("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 131why, one step back131 def index():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 132why, one step back132 raise Exception("dummy")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 133–135why, one step back133134 with pytest.raises(Exception):135 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 136why, one step back136 client.get("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 137–139why, one step back137138 assert len(cleanup_stuff) == 1139 assert isinstance(cleanup_stuff[0], Exception)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 140why, one step back140 assert str(cleanup_stuff[0]) == "dummy"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 141–144why, one step back141142143def test_app_ctx_globals_methods(app, app_ctx):144 # getMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 145–146why, one step back145 assert flask.g.get("foo") is None146 assert flask.g.get("foo", "bar") == "bar"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 147why, one step back147 # __contains__Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 148–150why, one step back148 assert "foo" not in flask.g149 flask.g.foo = "bar"150 assert "foo" in flask.gMerge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 151why, one step back151 # setdefaultMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 152–154why, one step back152 flask.g.setdefault("bar", "the cake is a lie")153 flask.g.setdefault("bar", "hello world")154 assert flask.g.bar == "the cake is a lie"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 155why, one step back155 # popMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 156why, one step back156 assert flask.g.pop("bar") == "the cake is a lie"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 157why, one step back157 with pytest.raises(KeyError):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 158–159why, one step back158 flask.g.pop("bar")159 assert flask.g.pop("bar", "more cake") == "more cake"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 160why, one step back160 # __iter__Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 161–162why, one step back161 assert list(flask.g) == ["foo"]162 # __repr__Merge pull request #2807 from eso31/masterDavid Lordbefore adoption8080ead
line 163why, one step back163 assert repr(flask.g) == "<flask.g of 'flask_test'>"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 164–168why, one step back164165166def test_custom_app_ctx_globals_class(app):167 class CustomRequestGlobals(object):168 def __init__(self):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 169why, one step back169 self.spam = "eggs"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 170–172why, one step back170171 app.app_ctx_globals_class = CustomRequestGlobals172 with app.app_context():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 173why, one step back173 assert flask.render_template_string("{{ g.spam }}") == "eggs"Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 174–180why, one step back174175176def test_context_refcounts(app, client):177 called = []178179 @app.teardown_request180 def teardown_req(error=None):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 181why, one step back181 called.append("request")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 182–184why, one step back182183 @app.teardown_appcontext184 def teardown_app(error=None):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 185why, one step back185 called.append("app")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 186why, one step back186Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 187why, one step back187 @app.route("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 188–192why, one step back188 def index():189 with flask._app_ctx_stack.top:190 with flask._request_ctx_stack.top:191 pass192 env = flask._request_ctx_stack.top.request.environMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 193–194why, one step back193 assert env["werkzeug.request"] is not None194 return u""Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 195why, one step back195Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 196why, one step back196 res = client.get("/")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 197why, one step back197 assert res.status_code == 200Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 198–199why, one step back198 assert res.data == b""199 assert called == ["request", "app"]Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 200–211why, one step back200201202def test_clean_pop(app):203 app.testing = False204 called = []205206 @app.teardown_request207 def teardown_req(error=None):208 1 / 0209210 @app.teardown_appcontext211 def teardown_app(error=None):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 212why, one step back212 called.append("TEARDOWN")Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
lines 213–219why, one step back213214 try:215 with app.test_request_context():216 called.append(flask.current_app.name)217 except ZeroDivisionError:218 pass219Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 220why, one step back220 assert called == ["flask_test", "TEARDOWN"]Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469
line 221why, one step back221 assert not flask.current_app