Skip to content

Commit 992ecbb

Browse files
committed
fix tests for Flask v2.3 and above
Reload the flask_themes2 module to create new global flask objects. Needed because Flask 2.3+ treats setup after requests as an error, so our Flask objects can't persist between tests.
1 parent 40b2625 commit 992ecbb

File tree

1 file changed

+64
-47
lines changed

1 file changed

+64
-47
lines changed

tests/test_themes.py

+64-47
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,26 @@
66
from __future__ import with_statement
77

88
import os
9+
from importlib import reload
910
from operator import attrgetter
1011

1112
from flask import Flask, render_template, url_for
12-
from flask_themes2 import (
13-
Theme,
14-
ThemeManager,
15-
Themes,
16-
get_theme,
17-
get_themes_list,
18-
load_themes_from,
19-
packaged_themes_loader,
20-
render_theme_template,
21-
static_file_url,
22-
template_exists,
23-
theme_paths_loader,
24-
themes_blueprint,
25-
)
2613
from jinja2 import FileSystemLoader
2714

2815
TESTS = os.path.dirname(__file__)
2916

3017

18+
def import_flask_themes2():
19+
import flask_themes2
20+
flask_themes2 = reload(flask_themes2)
21+
return flask_themes2
22+
23+
3124
class TestThemeObject(object):
3225
def test_theme(self):
26+
flask_themes2 = import_flask_themes2()
3327
path = os.path.join(TESTS, "themes", "cool")
34-
cool = Theme(path)
28+
cool = flask_themes2.Theme(path)
3529
assert cool.name == "Cool Blue v1"
3630
assert cool.identifier == "cool"
3731
assert cool.path == os.path.abspath(path)
@@ -41,39 +35,44 @@ def test_theme(self):
4135
assert isinstance(cool.jinja_loader, FileSystemLoader)
4236

4337
def test_license_text(self):
38+
flask_themes2 = import_flask_themes2()
4439
path = os.path.join(TESTS, "themes", "plain")
45-
plain = Theme(path)
40+
plain = flask_themes2.Theme(path)
4641
assert plain.license_text.strip() == "The license."
4742

4843

4944
class TestLoaders(object):
5045
def test_load_themes_from(self):
46+
flask_themes2 = import_flask_themes2()
5147
path = os.path.join(TESTS, "themes")
52-
themes_iter = load_themes_from(path)
48+
themes_iter = flask_themes2.load_themes_from(path)
5349
themes = list(sorted(themes_iter, key=attrgetter("identifier")))
5450
assert themes[0].identifier == "cool"
5551
assert themes[1].identifier == "notthis"
5652
assert themes[2].identifier == "plain"
5753

5854
def test_packaged_themes_loader(self):
55+
flask_themes2 = import_flask_themes2()
5956
app = Flask(__name__)
60-
themes_iter = packaged_themes_loader(app)
57+
themes_iter = flask_themes2.packaged_themes_loader(app)
6158
themes = list(sorted(themes_iter, key=attrgetter("identifier")))
6259
assert themes[0].identifier == "cool"
6360
assert themes[1].identifier == "notthis"
6461
assert themes[2].identifier == "plain"
6562

6663
def test_theme_paths_loader(self):
64+
flask_themes2 = import_flask_themes2()
6765
app = Flask(__name__)
6866
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
69-
themes = list(theme_paths_loader(app))
67+
themes = list(flask_themes2.theme_paths_loader(app))
7068
assert themes[0].identifier == "cool"
7169

7270

7371
class TestSetup(object):
7472
def test_manager(self):
73+
flask_themes2 = import_flask_themes2()
7574
app = Flask(__name__)
76-
manager = ThemeManager(app, "testing")
75+
manager = flask_themes2.ThemeManager(app, "testing")
7776
assert app.theme_manager is manager
7877
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
7978
manager.refresh()
@@ -82,30 +81,32 @@ def test_manager(self):
8281
assert manager.themes["cool"].name == "Cool Blue v2"
8382

8483
def test_setup_themes(self):
84+
flask_themes2 = import_flask_themes2()
8585
app = Flask(__name__)
8686
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
87-
Themes(app, app_identifier="testing")
87+
flask_themes2.Themes(app, app_identifier="testing")
8888

8989
assert hasattr(app, "theme_manager")
9090
assert "_themes" in app.blueprints
9191
assert "theme" in app.jinja_env.globals
9292
assert "theme_static" in app.jinja_env.globals
9393

9494
def test_get_helpers(self):
95+
flask_themes2 = import_flask_themes2()
9596
app = Flask(__name__)
9697
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
97-
Themes(app, app_identifier="testing")
98+
flask_themes2.Themes(app, app_identifier="testing")
9899

99100
with app.test_request_context("/"):
100101
cool = app.theme_manager.themes["cool"]
101102
plain = app.theme_manager.themes["plain"]
102-
assert get_theme("cool") is cool
103-
assert get_theme("plain") is plain
104-
tl = get_themes_list()
103+
assert flask_themes2.get_theme("cool") is cool
104+
assert flask_themes2.get_theme("plain") is plain
105+
tl = flask_themes2.get_themes_list()
105106
assert tl[0] is cool
106107
assert tl[1] is plain
107108
try:
108-
get_theme("notthis")
109+
flask_themes2.get_theme("notthis")
109110
except KeyError:
110111
pass
111112
else:
@@ -116,76 +117,91 @@ def test_get_helpers(self):
116117

117118
class TestStatic(object):
118119
def test_static_file_url(self):
120+
flask_themes2 = import_flask_themes2()
119121
app = Flask(__name__)
120122
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
121-
Themes(app, app_identifier="testing")
123+
flask_themes2.Themes(app, app_identifier="testing")
122124

123125
with app.test_request_context("/"):
124-
url = static_file_url("cool", "style.css")
126+
url = flask_themes2.static_file_url("cool", "style.css")
125127
genurl = url_for("_themes.static", themeid="cool", filename="style.css")
126128
assert url == genurl
127129

128130

129131
class TestTemplates(object):
130132
def test_template_exists(self):
133+
flask_themes2 = import_flask_themes2()
131134
app = Flask(__name__)
132135
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
133-
Themes(app, app_identifier="testing")
136+
flask_themes2.Themes(app, app_identifier="testing")
134137

135138
with app.test_request_context("/"):
136-
assert template_exists("hello.html")
137-
assert template_exists("_themes/cool/hello.html")
138-
assert not template_exists("_themes/plain/hello.html")
139+
assert flask_themes2.template_exists("hello.html")
140+
assert flask_themes2.template_exists("_themes/cool/hello.html")
141+
assert not flask_themes2.template_exists("_themes/plain/hello.html")
139142

140-
def test_loader(self):
143+
def test_test_loader(self):
144+
flask_themes2 = import_flask_themes2()
141145
app = Flask(__name__)
142146
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
143-
Themes(app, app_identifier="testing")
147+
flask_themes2.Themes(app, app_identifier="testing")
144148

145149
with app.test_request_context("/"):
146-
src = themes_blueprint.jinja_loader.get_source(
150+
src = flask_themes2.themes_blueprint.jinja_loader.get_source(
147151
app.jinja_env, "_themes/cool/hello.html"
148152
)
149153
assert src[0].strip() == "Hello from Cool Blue v2."
150154

151155
def test_render_theme_template(self):
156+
flask_themes2 = import_flask_themes2()
152157
app = Flask(__name__)
153158
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
154-
Themes(app, app_identifier="testing")
159+
flask_themes2.Themes(app, app_identifier="testing")
155160

156161
with app.test_request_context("/"):
157-
coolsrc = render_theme_template("cool", "hello.html").strip()
158-
plainsrc = render_theme_template("plain", "hello.html").strip()
162+
coolsrc = flask_themes2.render_theme_template("cool", "hello.html").strip()
163+
plainsrc = flask_themes2.render_theme_template(
164+
"plain", "hello.html"
165+
).strip()
159166
assert coolsrc == "Hello from Cool Blue v2."
160167
assert plainsrc == "Hello from the application"
161168

162169
def test_active_theme(self):
170+
flask_themes2 = import_flask_themes2()
163171
app = Flask(__name__)
164172
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
165-
Themes(app, app_identifier="testing")
173+
flask_themes2.Themes(app, app_identifier="testing")
166174

167175
with app.test_request_context("/"):
168176
appdata = render_template("active.html").strip()
169-
cooldata = render_theme_template("cool", "active.html").strip()
170-
plaindata = render_theme_template("plain", "active.html").strip()
177+
cooldata = flask_themes2.render_theme_template(
178+
"cool", "active.html"
179+
).strip()
180+
plaindata = flask_themes2.render_theme_template(
181+
"plain", "active.html"
182+
).strip()
171183
assert appdata == "Application, Active theme: none"
172184
assert cooldata == "Cool Blue v2, Active theme: cool"
173185
assert plaindata == "Application, Active theme: plain"
174186

175187
def test_theme_static(self):
188+
flask_themes2 = import_flask_themes2()
176189
app = Flask(__name__)
177190
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
178-
Themes(app, app_identifier="testing")
191+
flask_themes2.Themes(app, app_identifier="testing")
179192

180193
with app.test_request_context("/"):
181-
coolurl = static_file_url("cool", "style.css")
182-
cooldata = render_theme_template("cool", "static.html").strip()
194+
coolurl = flask_themes2.static_file_url("cool", "style.css")
195+
cooldata = flask_themes2.render_theme_template(
196+
"cool", "static.html"
197+
).strip()
183198
assert cooldata == "Cool Blue v2, %s" % coolurl
184199

185200
def test_theme_static_outside(self):
201+
flask_themes2 = import_flask_themes2()
186202
app = Flask(__name__)
187203
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
188-
Themes(app, app_identifier="testing")
204+
flask_themes2.Themes(app, app_identifier="testing")
189205

190206
with app.test_request_context("/"):
191207
try:
@@ -198,11 +214,12 @@ def test_theme_static_outside(self):
198214
)
199215

200216
def test_theme_include_static(self):
217+
flask_themes2 = import_flask_themes2()
201218
app = Flask(__name__)
202219
app.config["THEME_PATHS"] = [os.path.join(TESTS, "morethemes")]
203-
Themes(app, app_identifier="testing")
220+
flask_themes2.Themes(app, app_identifier="testing")
204221

205222
with app.test_request_context("/"):
206223
data = render_template("static_parent.html").strip()
207-
url = static_file_url("plain", "style.css")
224+
url = flask_themes2.static_file_url("plain", "style.css")
208225
assert data == "Application, Plain, %s" % url

0 commit comments

Comments
 (0)