2
2
3
3
import pytest
4
4
from dominate import tags
5
+ from flask import Blueprint
6
+ from flask import Flask
5
7
6
8
from bootlace .util import as_tag
9
+ from bootlace .util import is_active_blueprint
10
+ from bootlace .util import is_active_endpoint
7
11
from bootlace .util import Tag
8
12
9
13
14
+ @pytest .fixture
15
+ def app (app : Flask ) -> Flask :
16
+
17
+ @app .route ("/" )
18
+ def home () -> str :
19
+ return "Home"
20
+
21
+ @app .route ("/about" )
22
+ def about () -> str :
23
+ return "About"
24
+
25
+ @app .route ("/contact" )
26
+ def contact () -> str :
27
+ return "Contact"
28
+
29
+ return app
30
+
31
+
32
+ @pytest .fixture
33
+ def bp (app : Flask ) -> Blueprint :
34
+ bp = Blueprint ("bp" , __name__ )
35
+
36
+ @bp .route ("/archive" )
37
+ def archive () -> str :
38
+ return "Archive"
39
+
40
+ @bp .route ("/post/<id>" )
41
+ def post (id : str ) -> str :
42
+ return "Post"
43
+
44
+ app .register_blueprint (bp )
45
+
46
+ return bp
47
+
48
+
10
49
class Taggable :
11
50
def __tag__ (self ) -> tags .html_tag :
12
51
return tags .div ()
@@ -29,7 +68,7 @@ def test_as_tag(tag: Any, expected: str) -> None:
29
68
30
69
def test_as_tag_warning () -> None :
31
70
with pytest .warns (UserWarning ):
32
- assert as_tag (1 ).render () == "1\n <!--Rendered type int not supported-->\n " # type: ignore
71
+ assert as_tag (1 ).render () == "1\n <!--Rendered type int not supported-->\n "
33
72
34
73
35
74
def test_classes () -> None :
@@ -102,3 +141,38 @@ def test_tag_configurator() -> None:
102
141
a .classes .discard ("test" )
103
142
104
143
assert as_tag (a ).render () == '<a class="other" href="/test"></a>'
144
+
145
+
146
+ @pytest .mark .usefixtures ("bp" )
147
+ @pytest .mark .parametrize (
148
+ "uri,endpoint,kwargs,expected" ,
149
+ [
150
+ ("/" , "home" , {}, True ),
151
+ ("/about" , "home" , {}, False ),
152
+ ("/post/a" , "bp.post" , {"id" : "a" }, True ),
153
+ ("/post/b" , "bp.post" , {"id" : "a" }, False ),
154
+ ("/archive" , "bp.archive" , {}, True ),
155
+ ],
156
+ )
157
+ def test_is_active_endpoint (app : Flask , uri : str , endpoint : str , kwargs : dict [str , str ], expected : bool ) -> None :
158
+
159
+ with app .test_request_context (uri ):
160
+ print (f"Testing { uri } -> { endpoint } with { kwargs } " )
161
+ assert is_active_endpoint (endpoint , kwargs ) is expected
162
+
163
+
164
+ @pytest .mark .usefixtures ("bp" )
165
+ @pytest .mark .parametrize (
166
+ "uri,blueprint,expected" ,
167
+ [
168
+ ("/" , None , True ),
169
+ ("/about" , "bp" , False ),
170
+ ("/post/a" , "bp" , True ),
171
+ ("/archive" , "bp" , True ),
172
+ ],
173
+ )
174
+ def test_is_active_blueprint (app : Flask , uri : str , blueprint : str , expected : bool ) -> None :
175
+
176
+ with app .test_request_context (uri ):
177
+ print (f"Testing { uri } -> { blueprint } " )
178
+ assert is_active_blueprint (blueprint ) is expected
0 commit comments