6
6
from __future__ import with_statement
7
7
8
8
import os
9
+ from importlib import reload
9
10
from operator import attrgetter
10
11
11
12
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
- )
26
13
from jinja2 import FileSystemLoader
27
14
28
15
TESTS = os .path .dirname (__file__ )
29
16
30
17
18
+ def import_flask_themes2 ():
19
+ import flask_themes2
20
+ flask_themes2 = reload (flask_themes2 )
21
+ return flask_themes2
22
+
23
+
31
24
class TestThemeObject (object ):
32
25
def test_theme (self ):
26
+ flask_themes2 = import_flask_themes2 ()
33
27
path = os .path .join (TESTS , "themes" , "cool" )
34
- cool = Theme (path )
28
+ cool = flask_themes2 . Theme (path )
35
29
assert cool .name == "Cool Blue v1"
36
30
assert cool .identifier == "cool"
37
31
assert cool .path == os .path .abspath (path )
@@ -41,39 +35,44 @@ def test_theme(self):
41
35
assert isinstance (cool .jinja_loader , FileSystemLoader )
42
36
43
37
def test_license_text (self ):
38
+ flask_themes2 = import_flask_themes2 ()
44
39
path = os .path .join (TESTS , "themes" , "plain" )
45
- plain = Theme (path )
40
+ plain = flask_themes2 . Theme (path )
46
41
assert plain .license_text .strip () == "The license."
47
42
48
43
49
44
class TestLoaders (object ):
50
45
def test_load_themes_from (self ):
46
+ flask_themes2 = import_flask_themes2 ()
51
47
path = os .path .join (TESTS , "themes" )
52
- themes_iter = load_themes_from (path )
48
+ themes_iter = flask_themes2 . load_themes_from (path )
53
49
themes = list (sorted (themes_iter , key = attrgetter ("identifier" )))
54
50
assert themes [0 ].identifier == "cool"
55
51
assert themes [1 ].identifier == "notthis"
56
52
assert themes [2 ].identifier == "plain"
57
53
58
54
def test_packaged_themes_loader (self ):
55
+ flask_themes2 = import_flask_themes2 ()
59
56
app = Flask (__name__ )
60
- themes_iter = packaged_themes_loader (app )
57
+ themes_iter = flask_themes2 . packaged_themes_loader (app )
61
58
themes = list (sorted (themes_iter , key = attrgetter ("identifier" )))
62
59
assert themes [0 ].identifier == "cool"
63
60
assert themes [1 ].identifier == "notthis"
64
61
assert themes [2 ].identifier == "plain"
65
62
66
63
def test_theme_paths_loader (self ):
64
+ flask_themes2 = import_flask_themes2 ()
67
65
app = Flask (__name__ )
68
66
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 ))
70
68
assert themes [0 ].identifier == "cool"
71
69
72
70
73
71
class TestSetup (object ):
74
72
def test_manager (self ):
73
+ flask_themes2 = import_flask_themes2 ()
75
74
app = Flask (__name__ )
76
- manager = ThemeManager (app , "testing" )
75
+ manager = flask_themes2 . ThemeManager (app , "testing" )
77
76
assert app .theme_manager is manager
78
77
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
79
78
manager .refresh ()
@@ -82,30 +81,32 @@ def test_manager(self):
82
81
assert manager .themes ["cool" ].name == "Cool Blue v2"
83
82
84
83
def test_setup_themes (self ):
84
+ flask_themes2 = import_flask_themes2 ()
85
85
app = Flask (__name__ )
86
86
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
87
- Themes (app , app_identifier = "testing" )
87
+ flask_themes2 . Themes (app , app_identifier = "testing" )
88
88
89
89
assert hasattr (app , "theme_manager" )
90
90
assert "_themes" in app .blueprints
91
91
assert "theme" in app .jinja_env .globals
92
92
assert "theme_static" in app .jinja_env .globals
93
93
94
94
def test_get_helpers (self ):
95
+ flask_themes2 = import_flask_themes2 ()
95
96
app = Flask (__name__ )
96
97
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
97
- Themes (app , app_identifier = "testing" )
98
+ flask_themes2 . Themes (app , app_identifier = "testing" )
98
99
99
100
with app .test_request_context ("/" ):
100
101
cool = app .theme_manager .themes ["cool" ]
101
102
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 ()
105
106
assert tl [0 ] is cool
106
107
assert tl [1 ] is plain
107
108
try :
108
- get_theme ("notthis" )
109
+ flask_themes2 . get_theme ("notthis" )
109
110
except KeyError :
110
111
pass
111
112
else :
@@ -116,76 +117,91 @@ def test_get_helpers(self):
116
117
117
118
class TestStatic (object ):
118
119
def test_static_file_url (self ):
120
+ flask_themes2 = import_flask_themes2 ()
119
121
app = Flask (__name__ )
120
122
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
121
- Themes (app , app_identifier = "testing" )
123
+ flask_themes2 . Themes (app , app_identifier = "testing" )
122
124
123
125
with app .test_request_context ("/" ):
124
- url = static_file_url ("cool" , "style.css" )
126
+ url = flask_themes2 . static_file_url ("cool" , "style.css" )
125
127
genurl = url_for ("_themes.static" , themeid = "cool" , filename = "style.css" )
126
128
assert url == genurl
127
129
128
130
129
131
class TestTemplates (object ):
130
132
def test_template_exists (self ):
133
+ flask_themes2 = import_flask_themes2 ()
131
134
app = Flask (__name__ )
132
135
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
133
- Themes (app , app_identifier = "testing" )
136
+ flask_themes2 . Themes (app , app_identifier = "testing" )
134
137
135
138
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" )
139
142
140
- def test_loader (self ):
143
+ def test_test_loader (self ):
144
+ flask_themes2 = import_flask_themes2 ()
141
145
app = Flask (__name__ )
142
146
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
143
- Themes (app , app_identifier = "testing" )
147
+ flask_themes2 . Themes (app , app_identifier = "testing" )
144
148
145
149
with app .test_request_context ("/" ):
146
- src = themes_blueprint .jinja_loader .get_source (
150
+ src = flask_themes2 . themes_blueprint .jinja_loader .get_source (
147
151
app .jinja_env , "_themes/cool/hello.html"
148
152
)
149
153
assert src [0 ].strip () == "Hello from Cool Blue v2."
150
154
151
155
def test_render_theme_template (self ):
156
+ flask_themes2 = import_flask_themes2 ()
152
157
app = Flask (__name__ )
153
158
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
154
- Themes (app , app_identifier = "testing" )
159
+ flask_themes2 . Themes (app , app_identifier = "testing" )
155
160
156
161
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 ()
159
166
assert coolsrc == "Hello from Cool Blue v2."
160
167
assert plainsrc == "Hello from the application"
161
168
162
169
def test_active_theme (self ):
170
+ flask_themes2 = import_flask_themes2 ()
163
171
app = Flask (__name__ )
164
172
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
165
- Themes (app , app_identifier = "testing" )
173
+ flask_themes2 . Themes (app , app_identifier = "testing" )
166
174
167
175
with app .test_request_context ("/" ):
168
176
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 ()
171
183
assert appdata == "Application, Active theme: none"
172
184
assert cooldata == "Cool Blue v2, Active theme: cool"
173
185
assert plaindata == "Application, Active theme: plain"
174
186
175
187
def test_theme_static (self ):
188
+ flask_themes2 = import_flask_themes2 ()
176
189
app = Flask (__name__ )
177
190
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
178
- Themes (app , app_identifier = "testing" )
191
+ flask_themes2 . Themes (app , app_identifier = "testing" )
179
192
180
193
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 ()
183
198
assert cooldata == "Cool Blue v2, %s" % coolurl
184
199
185
200
def test_theme_static_outside (self ):
201
+ flask_themes2 = import_flask_themes2 ()
186
202
app = Flask (__name__ )
187
203
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
188
- Themes (app , app_identifier = "testing" )
204
+ flask_themes2 . Themes (app , app_identifier = "testing" )
189
205
190
206
with app .test_request_context ("/" ):
191
207
try :
@@ -198,11 +214,12 @@ def test_theme_static_outside(self):
198
214
)
199
215
200
216
def test_theme_include_static (self ):
217
+ flask_themes2 = import_flask_themes2 ()
201
218
app = Flask (__name__ )
202
219
app .config ["THEME_PATHS" ] = [os .path .join (TESTS , "morethemes" )]
203
- Themes (app , app_identifier = "testing" )
220
+ flask_themes2 . Themes (app , app_identifier = "testing" )
204
221
205
222
with app .test_request_context ("/" ):
206
223
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" )
208
225
assert data == "Application, Plain, %s" % url
0 commit comments