Skip to content
Myr

test_signals.py

This file

Path

tests/test_signals.py

181 lines · 4.8 KiB · Python

Last changed by

use ruff linter and formatterDavid Lordbefore adoption54e05a2

History of this file

Take it

The walk stopped after 200 commits: lines older than 93a7f08 point at it.

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 1–100why, one step back
1import flask
234def test_template_rendered(app, client):5 @app.route("/")6 def index():7 return flask.render_template("simple_template.html", whiskey=42)89 recorded = []1011 def record(sender, template, context):12 recorded.append((template, context))1314 flask.template_rendered.connect(record, app)15 try:16 client.get("/")17 assert len(recorded) == 118 template, context = recorded[0]19 assert template.name == "simple_template.html"20 assert context["whiskey"] == 4221 finally:22 flask.template_rendered.disconnect(record, app)232425def test_before_render_template():26 app = flask.Flask(__name__)2728 @app.route("/")29 def index():30 return flask.render_template("simple_template.html", whiskey=42)3132 recorded = []3334 def record(sender, template, context):35 context["whiskey"] = 4336 recorded.append((template, context))3738 flask.before_render_template.connect(record, app)39 try:40 rv = app.test_client().get("/")41 assert len(recorded) == 142 template, context = recorded[0]43 assert template.name == "simple_template.html"44 assert context["whiskey"] == 4345 assert rv.data == b"<h1>43</h1>"46 finally:47 flask.before_render_template.disconnect(record, app)484950def test_request_signals():51 app = flask.Flask(__name__)52 calls = []5354 def before_request_signal(sender):55 calls.append("before-signal")5657 def after_request_signal(sender, response):58 assert response.data == b"stuff"59 calls.append("after-signal")6061 @app.before_request62 def before_request_handler():63 calls.append("before-handler")6465 @app.after_request66 def after_request_handler(response):67 calls.append("after-handler")68 response.data = "stuff"69 return response7071 @app.route("/")72 def index():73 calls.append("handler")74 return "ignored anyway"7576 flask.request_started.connect(before_request_signal, app)77 flask.request_finished.connect(after_request_signal, app)7879 try:80 rv = app.test_client().get("/")81 assert rv.data == b"stuff"8283 assert calls == [84 "before-signal",85 "before-handler",86 "handler",87 "after-handler",88 "after-signal",89 ]90 finally:91 flask.request_started.disconnect(before_request_signal, app)92 flask.request_finished.disconnect(after_request_signal, app)939495def test_request_exception_signal():96 app = flask.Flask(__name__)97 recorded = []9899 @app.route("/")100 def index():

use ruff linter and formatterDavid Lordbefore adoption54e05a2

line 101why, one step back
101 raise ZeroDivisionError

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 102–114why, one step back
102
103 def record(sender, exception):104 recorded.append(exception)105106 flask.got_request_exception.connect(record, app)107 try:108 assert app.test_client().get("/").status_code == 500109 assert len(recorded) == 1110 assert isinstance(recorded[0], ZeroDivisionError)111 finally:112 flask.got_request_exception.disconnect(record, app)113114

Merge pull request #4666 from pallets/debug-contextDavid Lordbefore adoption6916697

line 115why, one step back
115def test_appcontext_signals(app, client):

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 116–130why, one step back
116 recorded = []
117118 def record_push(sender, **kwargs):119 recorded.append("push")120121 def record_pop(sender, **kwargs):122 recorded.append("pop")123124 @app.route("/")125 def index():126 return "Hello"127128 flask.appcontext_pushed.connect(record_push, app)129 flask.appcontext_popped.connect(record_pop, app)130 try:

Merge pull request #4666 from pallets/debug-contextDavid Lordbefore adoption6916697

lines 131–132why, one step back
131 rv = client.get("/")
132 assert rv.data == b"Hello"

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 133–162why, one step back
133 assert recorded == ["push", "pop"]
134 finally:135 flask.appcontext_pushed.disconnect(record_push, app)136 flask.appcontext_popped.disconnect(record_pop, app)137138139def test_flash_signal(app):140 @app.route("/")141 def index():142 flask.flash("This is a flash message", category="notice")143 return flask.redirect("/other")144145 recorded = []146147 def record(sender, message, category):148 recorded.append((message, category))149150 flask.message_flashed.connect(record, app)151 try:152 client = app.test_client()153 with client.session_transaction():154 client.get("/")155 assert len(recorded) == 1156 message, category = recorded[0]157 assert message == "This is a flash message"158 assert category == "notice"159 finally:160 flask.message_flashed.disconnect(record, app)161162

Merge pull request #4666 from pallets/debug-contextDavid Lordbefore adoption6916697

lines 163–164why, one step back
163def test_appcontext_tearing_down_signal(app, client):
164 app.testing = False

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 165–166why, one step back
165 recorded = []
166

Merge pull request #4666 from pallets/debug-contextDavid Lordbefore adoption6916697

lines 167–168why, one step back
167 def record_teardown(sender, exc):
168 recorded.append(exc)

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 169–171why, one step back
169
170 @app.route("/")171 def index():

use ruff linter and formatterDavid Lordbefore adoption54e05a2

line 172why, one step back
172 raise ZeroDivisionError

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 173–175why, one step back
173
174 flask.appcontext_tearing_down.connect(record_teardown, app)175 try:

Merge pull request #4666 from pallets/debug-contextDavid Lordbefore adoption6916697

lines 176–179why, one step back
176 rv = client.get("/")
177 assert rv.status_code == 500178 assert len(recorded) == 1179 assert isinstance(recorded[0], ZeroDivisionError)

pin os and python version in rtd buildDavid Lordbefore adoption93a7f08

lines 180–181why, one step back
180 finally:
181 flask.appcontext_tearing_down.disconnect(record_teardown, app)