|
1
|
|
|
"""Test jinja filters.""" |
|
2
|
|
|
|
|
3
|
|
|
import json |
|
4
|
|
|
|
|
5
|
|
|
from flask_extras.filters import filters |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class MockClass: |
|
9
|
|
|
"""Empty class for testing.""" |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestCamel2Hyphen: |
|
13
|
|
|
"""All tests for camel2hyphen function.""" |
|
14
|
|
|
|
|
15
|
|
|
def test_empty_str(self): |
|
16
|
|
|
"""Test.""" |
|
17
|
|
|
assert filters.camel2hyphen('', ) == '' |
|
18
|
|
|
|
|
19
|
|
|
def test_valid_single_upper_first(self): |
|
20
|
|
|
"""Test.""" |
|
21
|
|
|
assert filters.camel2hyphen('CamelCase', ) == 'camel-case' |
|
22
|
|
|
|
|
23
|
|
|
def test_valid_three_hyphen(self): |
|
24
|
|
|
"""Test.""" |
|
25
|
|
|
assert filters.camel2hyphen('bCamelCase', ) == 'b-camel-case' |
|
26
|
|
|
|
|
27
|
|
|
def test_valid_repeating_upper(self): |
|
28
|
|
|
"""Test.""" |
|
29
|
|
|
assert filters.camel2hyphen('bCamelCASE', ) == 'b-camel-case' |
|
30
|
|
|
|
|
31
|
|
|
def test_valid_nubers(self): |
|
32
|
|
|
"""Test.""" |
|
33
|
|
|
assert filters.camel2hyphen('bCamelCASE123', ) == 'b-camel-case123' |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class TestToJson: |
|
37
|
|
|
"""All tests for to_json function.""" |
|
38
|
|
|
|
|
39
|
|
|
def test_empty_str(self): |
|
40
|
|
|
"""Test.""" |
|
41
|
|
|
assert filters.to_json('', ) == json.dumps('') |
|
42
|
|
|
|
|
43
|
|
|
def test_empty_str_with_indent(self): |
|
44
|
|
|
"""Test.""" |
|
45
|
|
|
assert filters.to_json('', indent=4) == json.dumps('', indent=4) |
|
46
|
|
|
|
|
47
|
|
|
def test_dict(self): |
|
48
|
|
|
"""Test.""" |
|
49
|
|
|
data = dict(name='bar', id=123) |
|
50
|
|
|
assert filters.to_json(data) == json.dumps(data) |
|
51
|
|
|
|
|
52
|
|
|
def test_dict_with_indent(self): |
|
53
|
|
|
"""Test.""" |
|
54
|
|
|
data = dict(name='bar', id=123) |
|
55
|
|
|
assert filters.to_json(data, indent=4) == json.dumps(data, indent=4) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class TestCssSelector: |
|
59
|
|
|
"""All tests for css_selector function.""" |
|
60
|
|
|
|
|
61
|
|
|
def test_title_returns_invalid(self): |
|
62
|
|
|
"""Test the return value for a valid type.""" |
|
63
|
|
|
assert filters.css_selector(123) == 123 |
|
64
|
|
|
|
|
65
|
|
|
def test_title_returns_str(self): |
|
66
|
|
|
"""Test the return value for a valid type.""" |
|
67
|
|
|
assert isinstance(filters.css_selector('foo bar'), str) |
|
68
|
|
|
|
|
69
|
|
|
def test_basic(self): |
|
70
|
|
|
"""Test the argument.""" |
|
71
|
|
|
assert filters.css_selector('Hello World') == 'hello-world' |
|
72
|
|
|
|
|
73
|
|
|
def test_no_lowercase(self): |
|
74
|
|
|
"""Test the argument.""" |
|
75
|
|
|
expected = 'Hello-World' |
|
76
|
|
|
assert filters.css_selector('Hello World', lowercase=False) == expected |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
class TestTitle: |
|
80
|
|
|
"""All tests for title function.""" |
|
81
|
|
|
|
|
82
|
|
|
def test_title_returns_str(self): |
|
83
|
|
|
"""Test the return value for a valid type.""" |
|
84
|
|
|
assert isinstance(filters.title('foo bar'), str) |
|
85
|
|
|
|
|
86
|
|
|
def test_title_word(self): |
|
87
|
|
|
"""Test the `word` argument.""" |
|
88
|
|
|
assert filters.title('foo bar') == 'Foo bar' |
|
89
|
|
|
|
|
90
|
|
|
def test_title_capitalize(self): |
|
91
|
|
|
"""Test the `capitalize` argument.""" |
|
92
|
|
|
assert filters.title('foo bar', capitalize=True) == 'Foo Bar' |
|
93
|
|
|
|
|
94
|
|
|
def test_title_capitalize_sentence(self): |
|
95
|
|
|
"""Test the `capitalize` argument.""" |
|
96
|
|
|
res = filters.title('the quick brown fox... ah forget it', |
|
97
|
|
|
capitalize=True) |
|
98
|
|
|
expected = 'The Quick Brown Fox... Ah Forget It' |
|
99
|
|
|
assert res == expected |
|
100
|
|
|
|
|
101
|
|
|
def test_title_none(self): |
|
102
|
|
|
"""Test the function with None argument.""" |
|
103
|
|
|
assert filters.questionize_label(None) == '' |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
class TestQuestionizeLabel: |
|
107
|
|
|
"""All tests for questionize label function.""" |
|
108
|
|
|
|
|
109
|
|
|
def test_questionize_label_returns_str(self): |
|
110
|
|
|
"""Test the return value for a valid type.""" |
|
111
|
|
|
assert isinstance(filters.questionize_label('foo bar'), str) |
|
112
|
|
|
|
|
113
|
|
|
def test_questionize_label_word_is(self): |
|
114
|
|
|
"""Test the `word` argument.""" |
|
115
|
|
|
assert filters.questionize_label('is_cool') == 'cool?' |
|
116
|
|
|
|
|
117
|
|
|
def test_questionize_label_word_has(self): |
|
118
|
|
|
"""Test the `word` argument.""" |
|
119
|
|
|
assert filters.questionize_label('has_stuff') == 'stuff?' |
|
120
|
|
|
|
|
121
|
|
|
def test_questionize_label_none(self): |
|
122
|
|
|
"""Test the function with None argument.""" |
|
123
|
|
|
assert filters.questionize_label(None) == '' |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
class TestFirstOf: |
|
127
|
|
|
"""All tests for first of function.""" |
|
128
|
|
|
|
|
129
|
|
|
def test_firstof_all_false(self): |
|
130
|
|
|
"""Test what is returned when all values are falsy.""" |
|
131
|
|
|
assert filters.firstof([None, False, 0]) == '' |
|
132
|
|
|
|
|
133
|
|
|
def test_firstof_last_true(self): |
|
134
|
|
|
"""Test what is returned when last value is true.""" |
|
135
|
|
|
assert filters.firstof([None, False, 0, 'yay']) == 'yay' |
|
136
|
|
|
|
|
137
|
|
|
def test_firstof_first_true(self): |
|
138
|
|
|
"""Test what is returned when first value is true.""" |
|
139
|
|
|
assert filters.firstof(['yay', False, 0, 'yay']) == 'yay' |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
class TestAdd: |
|
143
|
|
|
"""All tests for add function.""" |
|
144
|
|
|
|
|
145
|
|
|
def test_returns_updated_list(self): |
|
146
|
|
|
"""Test return value.""" |
|
147
|
|
|
assert filters.add([1, 2], 3) == [1, 2, 3] |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
class TestCut: |
|
151
|
|
|
"""All tests for cut function.""" |
|
152
|
|
|
|
|
153
|
|
|
def test_returns_updated_string(self): |
|
154
|
|
|
"""Test return value.""" |
|
155
|
|
|
assert filters.cut('Hello world', ['world']) == 'Hello ' |
|
156
|
|
|
|
|
157
|
|
|
def test_returns_updated_multi(self): |
|
158
|
|
|
"""Test return value.""" |
|
159
|
|
|
assert filters.cut( |
|
160
|
|
|
'Well hello world', ['hello', 'world']) == 'Well ' |
|
161
|
|
|
|
|
162
|
|
|
def test_returns_updated_multispace(self): |
|
163
|
|
|
"""Test return value.""" |
|
164
|
|
|
assert filters.cut( |
|
165
|
|
|
'String with spaces', [' ']) == 'Stringwithspaces' |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
class TestAddSlashes: |
|
169
|
|
|
"""All tests for add slashes function.""" |
|
170
|
|
|
|
|
171
|
|
|
def test_returns_updated_basic(self): |
|
172
|
|
|
"""Test return value.""" |
|
173
|
|
|
res = filters.addslashes("I'm using Flask!") |
|
174
|
|
|
assert res == "I\\'m using Flask!" |
|
175
|
|
|
|
|
176
|
|
|
def test_returns_updated_empty(self): |
|
177
|
|
|
"""Test return value.""" |
|
178
|
|
|
res = filters.addslashes("Using Flask!") |
|
179
|
|
|
assert res == "Using Flask!" |
|
180
|
|
|
|
|
181
|
|
|
def test_returns_updated_complex(self): |
|
182
|
|
|
"""Test return value.""" |
|
183
|
|
|
res = filters.addslashes("I'm u's'i'n'g Flask!") |
|
184
|
|
|
assert res == "I\\'m u\\'s\\'i\\'n\\'g Flask!" |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
class TestDefaultVal: |
|
188
|
|
|
"""All tests for default val function.""" |
|
189
|
|
|
|
|
190
|
|
|
def test_returns_default(self): |
|
191
|
|
|
"""Test return value.""" |
|
192
|
|
|
assert filters.default(False, 'default') == 'default' |
|
193
|
|
|
|
|
194
|
|
|
def test_returns_original(self): |
|
195
|
|
|
"""Test return value.""" |
|
196
|
|
|
assert filters.default(1, 'default') == 1 |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
class TestDefaultIfNoneVal: |
|
200
|
|
|
"""All tests for default if none function.""" |
|
201
|
|
|
|
|
202
|
|
|
def test_returns_default(self): |
|
203
|
|
|
"""Test return value.""" |
|
204
|
|
|
assert filters.default_if_none(None, 'default') == 'default' |
|
205
|
|
|
|
|
206
|
|
|
def test_returns_original(self): |
|
207
|
|
|
"""Test return value.""" |
|
208
|
|
|
assert filters.default_if_none(1, 'default') == 1 |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
class TestGetDigit: |
|
212
|
|
|
"""All tests for get digit function.""" |
|
213
|
|
|
|
|
214
|
|
|
def test_returns_index_empty(self): |
|
215
|
|
|
"""Test return value.""" |
|
216
|
|
|
assert filters.get_digit(123456789, 0) == 123456789 |
|
217
|
|
|
|
|
218
|
|
|
def test_returns_index_end(self): |
|
219
|
|
|
"""Test return value.""" |
|
220
|
|
|
assert filters.get_digit(123456789, 1) == 9 |
|
221
|
|
|
|
|
222
|
|
|
def test_returns_index_mid(self): |
|
223
|
|
|
"""Test return value.""" |
|
224
|
|
|
assert filters.get_digit(123456789, 5) == 5 |
|
225
|
|
|
|
|
226
|
|
|
def test_returns_index_beg(self): |
|
227
|
|
|
"""Test return value.""" |
|
228
|
|
|
assert filters.get_digit(123456789, 9) == 1 |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
class TestLengthIs: |
|
232
|
|
|
"""All tests for length is function.""" |
|
233
|
|
|
|
|
234
|
|
|
def test_returns_false(self): |
|
235
|
|
|
"""Test return value.""" |
|
236
|
|
|
assert not filters.length_is('three', 4) |
|
237
|
|
|
|
|
238
|
|
|
def test_returns_true(self): |
|
239
|
|
|
"""Test return value.""" |
|
240
|
|
|
assert filters.length_is('one', 3) |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
class TestIsUrl: |
|
244
|
|
|
"""All tests for is url function.""" |
|
245
|
|
|
|
|
246
|
|
|
def test_returns_urls_true(self): |
|
247
|
|
|
"""Test return value.""" |
|
248
|
|
|
assert filters.is_url('http://foo.bar') |
|
249
|
|
|
assert filters.is_url('https://foo.bar') |
|
250
|
|
|
|
|
251
|
|
|
def test_returns_urls_false(self): |
|
252
|
|
|
"""Test return value.""" |
|
253
|
|
|
assert not filters.is_url('//foo.bar') |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
class TestLJust: |
|
257
|
|
|
"""All tests for ljust function.""" |
|
258
|
|
|
|
|
259
|
|
|
def test_returns_lpadding(self): |
|
260
|
|
|
"""Test return value.""" |
|
261
|
|
|
assert filters.ljust('Flask', 10) == 'Flask ' |
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
class TestRJust: |
|
265
|
|
|
"""All tests for rjust function.""" |
|
266
|
|
|
|
|
267
|
|
|
def test_returns_rpadding(self): |
|
268
|
|
|
"""Test return value.""" |
|
269
|
|
|
assert filters.rjust('Flask', 10) == ' Flask' |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
class TestMakeList: |
|
273
|
|
|
"""All tests for make list function.""" |
|
274
|
|
|
|
|
275
|
|
|
def test_list2list(self): |
|
276
|
|
|
"""Test return value.""" |
|
277
|
|
|
assert filters.make_list([1, 2]) == [1, 2] |
|
278
|
|
|
|
|
279
|
|
|
def test_ints_not_coerced(self): |
|
280
|
|
|
"""Test return value.""" |
|
281
|
|
|
assert filters.make_list( |
|
282
|
|
|
'12', coerce_numbers=False) == ['1', '2'] |
|
283
|
|
|
|
|
284
|
|
|
def test_ints_coerced(self): |
|
285
|
|
|
"""Test return value.""" |
|
286
|
|
|
assert filters.make_list('12') == [1, 2] |
|
287
|
|
|
|
|
288
|
|
|
def test_dict(self): |
|
289
|
|
|
"""Test return value.""" |
|
290
|
|
|
assert filters.make_list({'foo': 'bar'}) == [('foo', 'bar')] |
|
291
|
|
|
|
|
292
|
|
|
def test_list(self): |
|
293
|
|
|
"""Test return value.""" |
|
294
|
|
|
assert filters.make_list([1, 2]) == [1, 2] |
|
295
|
|
|
|
|
296
|
|
|
def test_str(self): |
|
297
|
|
|
"""Test return value.""" |
|
298
|
|
|
assert filters.make_list('abc') == ['a', 'b', 'c'] |
|
299
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
class TestPhone2Numeric: |
|
302
|
|
|
"""All tests for phone2numeric function.""" |
|
303
|
|
|
|
|
304
|
|
|
def test_basic(self): |
|
305
|
|
|
"""Test return value.""" |
|
306
|
|
|
assert filters.phone2numeric('1800-COLLeCT') == '1800-2655328' |
|
307
|
|
|
|
|
308
|
|
|
def test_1_thru_9(self): |
|
309
|
|
|
"""Test return value.""" |
|
310
|
|
|
assert filters.phone2numeric('1800-ADGJMPTX') == '1800-23456789' |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
class TestSlugify: |
|
314
|
|
|
"""All tests for slugify function.""" |
|
315
|
|
|
|
|
316
|
|
|
def test_slugify_plain(self): |
|
317
|
|
|
"""Test return value.""" |
|
318
|
|
|
assert filters.slugify('My news title!') == 'my-news-title' |
|
319
|
|
|
|
|
320
|
|
|
def test_slugify_complex(self): |
|
321
|
|
|
"""Test return value.""" |
|
322
|
|
|
res = filters.slugify('I am an OBFUsc@@Ted URL!!! Foo bar') |
|
323
|
|
|
expected = 'i-am-an-obfusc--ted-url----foo-bar' |
|
324
|
|
|
assert res == expected |
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
class TestPagetitle: |
|
328
|
|
|
"""All tests for pagetitle function.""" |
|
329
|
|
|
|
|
330
|
|
|
def test_title_plain(self): |
|
331
|
|
|
"""Test return value.""" |
|
332
|
|
|
assert filters.pagetitle('/foo/bar/bam') == ' > foo > bar > bam' |
|
333
|
|
|
|
|
334
|
|
|
def test_title_removefirst(self): |
|
335
|
|
|
"""Test return value.""" |
|
336
|
|
|
res = filters.pagetitle('/foo/bar/bam', divider=' | ') |
|
337
|
|
|
expected = ' | foo | bar | bam' |
|
338
|
|
|
assert res == expected |
|
339
|
|
|
|
|
340
|
|
|
def test_title_divider(self): |
|
341
|
|
|
"""Test return value.""" |
|
342
|
|
|
res = filters.pagetitle('/foo/bar/bam', remove_first=True) |
|
343
|
|
|
assert res == 'foo > bar > bam' |
|
344
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
class TestGreet: |
|
347
|
|
|
"""All tests for greet function.""" |
|
348
|
|
|
|
|
349
|
|
|
def test_greet(self): |
|
350
|
|
|
"""Test return value.""" |
|
351
|
|
|
assert filters.greet('Chris') == 'Hello, Chris!' |
|
352
|
|
|
|
|
353
|
|
|
def test_greet_override(self): |
|
354
|
|
|
"""Test return value.""" |
|
355
|
|
|
assert filters.greet( |
|
356
|
|
|
'Chris', greeting='Bonjour' == 'Bonjour, Chris!') |
|
357
|
|
|
|
|
358
|
|
|
|
|
359
|
|
|
class TestIsList: |
|
360
|
|
|
"""All tests for islist function.""" |
|
361
|
|
|
|
|
362
|
|
|
def test_islist(self): |
|
363
|
|
|
"""Test return value.""" |
|
364
|
|
|
assert filters.islist([1, 2, 3]) |
|
365
|
|
|
|
|
366
|
|
|
def test_notislist(self): |
|
367
|
|
|
"""Test return value.""" |
|
368
|
|
|
assert not filters.islist('Foo') |
|
369
|
|
|
assert not filters.islist({'foo': 'bar'}) |
|
370
|
|
|
assert not filters.islist(1) |
|
371
|
|
|
assert not filters.islist(1.0) |
|
372
|
|
|
|
|
373
|
|
|
|
|
374
|
|
|
class TestSql2dict: |
|
375
|
|
|
"""All tests for sql2dict function.""" |
|
376
|
|
|
|
|
377
|
|
|
def test_none(self): |
|
378
|
|
|
"""Test return value.""" |
|
379
|
|
|
assert filters.sql2dict(None) == [] |
|
380
|
|
|
|
|
381
|
|
|
def test_set(self): |
|
382
|
|
|
"""Test return value.""" |
|
383
|
|
|
mm = MockClass() |
|
384
|
|
|
mm.__dict__ = {'foo': 'bar'} |
|
385
|
|
|
assert filters.sql2dict([mm]) == [{'foo': 'bar'}] |
|
386
|
|
|
|