Skip to content
Myr

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 file

Take 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 back
1# -*- coding: utf-8 -*-
2"""3 tests.appctx4 ~~~~~~~~~~~~56 Tests the application context.7

Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c

line 8why, one step back
8 :copyright: © 2010 by the Pallets team.

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 9–17why, one step back
9 :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 back
18 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 back
20

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 21why, one step back
21 @app.route("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 22–25why, one step back
22 def index():
23 pass2425 with app.app_context():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 26–27why, one step back
26 rv = flask.url_for("index")
27 assert rv == "https://localhost/"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 28–32why, one step back
28
2930def 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 back
33 flask.url_for("index")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 34–37why, one step back
34
3536def 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 back
38 flask.url_for("index")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 39–73why, one step back
39
4041def 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 back
74 raise Exception("dummy")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 75–92why, one step back
75 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 back
93 raise Exception("dummy")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 94–100why, one step back
94 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 back
101 app.config["PROPAGATE_EXCEPTIONS"] = True

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 102–107why, one step back
102 cleanup_stuff = []
103104 @app.teardown_appcontext105 def cleanup(exception):106 cleanup_stuff.append(exception)107

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 108why, one step back
108 @app.route("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 109why, one step back
109 def index():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 110why, one step back
110 raise Exception("dummy")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 111–116why, one step back
111
112 @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 back
117 client.get("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 118–122why, one step back
118
119 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 back
123 app.config["PROPAGATE_EXCEPTIONS"] = True

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 124–129why, one step back
124 cleanup_stuff = []
125126 @app.teardown_appcontext127 def cleanup(exception):128 cleanup_stuff.append(exception)129

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 130why, one step back
130 @app.route("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 131why, one step back
131 def index():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 132why, one step back
132 raise Exception("dummy")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 133–135why, one step back
133
134 with pytest.raises(Exception):135 with app.app_context():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 136why, one step back
136 client.get("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 137–139why, one step back
137
138 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 back
140 assert str(cleanup_stuff[0]) == "dummy"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 141–144why, one step back
141
142143def test_app_ctx_globals_methods(app, app_ctx):144 # get

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 145–146why, one step back
145 assert flask.g.get("foo") is None
146 assert flask.g.get("foo", "bar") == "bar"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 147why, one step back
147 # __contains__

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 148–150why, one step back
148 assert "foo" not in flask.g
149 flask.g.foo = "bar"150 assert "foo" in flask.g

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 151why, one step back
151 # setdefault

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 152–154why, one step back
152 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 back
155 # pop

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 156why, one step back
156 assert flask.g.pop("bar") == "the cake is a lie"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 157why, one step back
157 with pytest.raises(KeyError):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 158–159why, one step back
158 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 back
160 # __iter__

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 161–162why, one step back
161 assert list(flask.g) == ["foo"]
162 # __repr__

Merge pull request #2807 from eso31/masterDavid Lordbefore adoption8080ead

line 163why, one step back
163 assert repr(flask.g) == "<flask.g of 'flask_test'>"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 164–168why, one step back
164
165166def 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 back
169 self.spam = "eggs"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 170–172why, one step back
170
171 app.app_ctx_globals_class = CustomRequestGlobals172 with app.app_context():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 173why, one step back
173 assert flask.render_template_string("{{ g.spam }}") == "eggs"

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 174–180why, one step back
174
175176def 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 back
181 called.append("request")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 182–184why, one step back
182
183 @app.teardown_appcontext184 def teardown_app(error=None):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 185why, one step back
185 called.append("app")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 186why, one step back
186

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 187why, one step back
187 @app.route("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 188–192why, one step back
188 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.environ

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 193–194why, one step back
193 assert env["werkzeug.request"] is not None
194 return u""

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 195why, one step back
195

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 196why, one step back
196 res = client.get("/")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 197why, one step back
197 assert res.status_code == 200

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 198–199why, one step back
198 assert res.data == b""
199 assert called == ["request", "app"]

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 200–211why, one step back
200
201202def 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 back
212 called.append("TEARDOWN")

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 213–219why, one step back
213
214 try:215 with app.test_request_context():216 called.append(flask.current_app.name)217 except ZeroDivisionError:218 pass219

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 220why, one step back
220 assert called == ["flask_test", "TEARDOWN"]

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 221why, one step back
221 assert not flask.current_app