1
+ from typing import Any
2
+
1
3
from django .template import Context , Template
2
4
from django .test import RequestFactory
3
5
@@ -55,7 +57,7 @@ def test_href(self):
55
57
# The tag name, a value of "list_item" would be the equivalent of "{% list_item %}".
56
58
tag = ""
57
59
58
- def assertContext (self , args : dict = {} ) -> any :
60
+ def assertContext (self , args : dict | None = None ) -> Any :
59
61
"""
60
62
Asserts that a context is returned by the inclusion tag.
61
63
@@ -64,11 +66,12 @@ def assertContext(self, args: dict = {}) -> any:
64
66
65
67
Returns: The return value of the tag's function.
66
68
"""
69
+ args = args or {}
67
70
context = self .call_function (args )
68
71
self .assertTrue (context )
69
72
return context
70
73
71
- def assertRender (self , args : dict = {} , data = {} ) -> str :
74
+ def assertRender (self , args : dict | None = None , data : dict | None = None ) -> str :
72
75
"""
73
76
Asserts that something is rendered by the tag.
74
77
@@ -79,12 +82,14 @@ def assertRender(self, args: dict = {}, data={}) -> str:
79
82
80
83
Returns: The str rendered by the tag.
81
84
"""
85
+ data = data or {}
86
+ args = args or {}
82
87
html = self .render (args , data )
83
88
self .assertTrue (html )
84
89
return html
85
90
86
91
def assertSelector (
87
- self , selector : str , args : dict = {} , data : dict = {}
92
+ self , selector : str , args : dict | None = None , data : dict | None = None
88
93
) -> ResultSet :
89
94
"""
90
95
Asserts that an HTML tag matching `selector` is present in the tag's rendered output.
@@ -97,12 +102,14 @@ def assertSelector(
97
102
98
103
Returns: bs4.element.ResultSet
99
104
"""
105
+ data = data or {}
106
+ args = args or {}
100
107
nodes = self .select (selector , args , data )
101
108
self .assertTrue (nodes , msg = f"Did not find selector { selector } " )
102
109
return nodes
103
110
104
111
def assertNotSelector (
105
- self , selector : str , args : dict = {} , data : dict = {}
112
+ self , selector : str , args : dict | None = None , data : dict | None = None
106
113
) -> ResultSet :
107
114
"""
108
115
Asserts that an HTML tag matching `selector` is not present in the tag's rendered output.
@@ -115,12 +122,18 @@ def assertNotSelector(
115
122
116
123
Returns: bs4.element.ResultSet
117
124
"""
125
+ data = data or {}
126
+ args = args or {}
118
127
nodes = self .select (selector , args , data )
119
128
self .assertFalse (nodes )
120
129
return nodes
121
130
122
131
def assertTextContent (
123
- self , selector : str , text : str , args : dict = {}, data : dict = {}
132
+ self ,
133
+ selector : str ,
134
+ text : str ,
135
+ args : dict | None = None ,
136
+ data : dict | None = None ,
124
137
) -> str :
125
138
"""
126
139
Asserts that an HTML tag matching `selector` has textual content matching `text`.
@@ -134,11 +147,13 @@ def assertTextContent(
134
147
135
148
Returns: bs4.element.ResultSet
136
149
"""
150
+ data = data or {}
151
+ args = args or {}
137
152
node = self .select_one (selector , args , data )
138
153
self .assertEqual (str (text ).strip (), node .text .strip ())
139
154
return node .text
140
155
141
- def render (self , args = {} , data = {} ):
156
+ def render (self , args : dict | None = None , data : dict | None = None ):
142
157
"""
143
158
Renders the template tag with arguments (and optionally passing `data` to RequestFactory's query string).
144
159
@@ -149,6 +164,7 @@ def render(self, args={}, data={}):
149
164
150
165
Returns: The str rendered by the tag.
151
166
"""
167
+ data = data or {}
152
168
args = args or {}
153
169
template_context = self .get_template_context (args )
154
170
context = Context (
@@ -157,7 +173,9 @@ def render(self, args={}, data={}):
157
173
template = self .get_template (args )
158
174
return template .render (context )
159
175
160
- def select_one (self , selector : str , args : dict = {}, data : dict = {}) -> Tag :
176
+ def select_one (
177
+ self , selector : str , args : dict | None = None , data : dict | None = None
178
+ ) -> Tag | None :
161
179
"""
162
180
Returns a bs4.element.Tag for HTML tag matching `selector` within the str rendered by the tag.
163
181
@@ -169,11 +187,15 @@ def select_one(self, selector: str, args: dict = {}, data: dict = {}) -> Tag:
169
187
170
188
Returns: bs4.element.Tag
171
189
"""
190
+ data = data or {}
191
+ args = args or {}
172
192
html = self .render (args , data )
173
193
soup = BeautifulSoup (html , features = "lxml" )
174
194
return soup .select_one (selector )
175
195
176
- def select (self , selector : str , args : dict = {}, data : dict = {}) -> ResultSet :
196
+ def select (
197
+ self , selector : str , args : dict | None = None , data : dict | None = None
198
+ ) -> ResultSet :
177
199
"""
178
200
Returns a bs4.element.ResultSet for HTML tags matching `selector` within the str rendered by the tag.
179
201
@@ -185,11 +207,13 @@ def select(self, selector: str, args: dict = {}, data: dict = {}) -> ResultSet:
185
207
186
208
Returns: bs4.element.ResultSet
187
209
"""
210
+ data = data or {}
211
+ args = args or {}
188
212
html = self .render (args , data )
189
213
soup = BeautifulSoup (html , features = "lxml" )
190
214
return soup .select (selector )
191
215
192
- def call_function (self , args : dict = {} ) -> any :
216
+ def call_function (self , args : dict | None = None ) -> Any :
193
217
"""
194
218
Calls the tag's function directly.
195
219
@@ -198,13 +222,15 @@ def call_function(self, args: dict = {}) -> any:
198
222
199
223
Returns: The return value of the tag's function.
200
224
"""
225
+ if args is None :
226
+ args = {}
201
227
template = self .get_template (args )
202
228
nodelist = template .compile_nodelist ()
203
229
inclusion_node = nodelist [1 ]
204
230
function = inclusion_node .func
205
231
return function (** args )
206
232
207
- def get_template (self , args = {} ):
233
+ def get_template (self , args : dict | None = None ):
208
234
"""
209
235
Returns the (Django) Template instance (in order to render the tag).
210
236
A templates str is constructed and then passed to a django.template.Template, the resulting instance is
@@ -215,6 +241,8 @@ def get_template(self, args={}):
215
241
216
242
Returns: django.template.Template
217
243
"""
244
+ if args is None :
245
+ args = {}
218
246
args = self .get_args (args )
219
247
template = (
220
248
"{% load " + self .library + " %}{% " + self .tag + " " + args + " " + " %}"
@@ -315,7 +343,9 @@ def test_contents(self):
315
343
# A value of "{% list_item %}" would be the equivalent of "{% render_list %}{% list_item %}{% endrender_list %}".
316
344
contents = ""
317
345
318
- def assertContents (self , args = {}, data = {}, contents_context = {}) -> str :
346
+ def assertContents (
347
+ self , args : dict | None = None , data : dict | None = None , contents_context = None
348
+ ) -> str :
319
349
"""
320
350
Asserts that rendered HTML of contents is present in the tag's rendered output.
321
351
@@ -327,13 +357,16 @@ def assertContents(self, args={}, data={}, contents_context={}) -> str:
327
357
328
358
Returns: The contents html.
329
359
"""
360
+ contents_context = contents_context or {}
361
+ data = data or {}
362
+ args = args or {}
330
363
html = self .render (args , data )
331
364
context = Context (contents_context )
332
365
contents_html = self .get_contents (context )
333
366
self .assertIn (contents_html , html )
334
367
return contents_html
335
368
336
- def get_contents (self , context : Context | dict = {} ) -> str :
369
+ def get_contents (self , context : Context | dict | None = None ) -> str :
337
370
"""
338
371
Renders contents HTML.
339
372
@@ -342,11 +375,13 @@ def get_contents(self, context: Context | dict = {}) -> str:
342
375
343
376
Returns: The contents html.
344
377
"""
378
+ if context is None :
379
+ context = {}
345
380
return Template ("{% load " + self .library + " %}" + self .contents ).render (
346
381
context
347
382
)
348
383
349
- def get_template (self , args = {} ):
384
+ def get_template (self , args : dict | None = None ):
350
385
"""
351
386
Returns the (Django) Template instance (in order to render the tag).
352
387
A templates str is constructed and then passed to a django.template.Template, the resulting instance is
@@ -357,6 +392,8 @@ def get_template(self, args={}):
357
392
358
393
Returns: django.template.Template
359
394
"""
395
+ if args is None :
396
+ args = {}
360
397
args = self .get_args (args )
361
398
template = (
362
399
"{% load "
0 commit comments