|
1
|
|
|
"""Test jinja filters.""" |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
from __future__ import absolute_import |
|
5
|
|
|
|
|
6
|
|
|
import unittest |
|
7
|
|
|
from mock import MagicMock |
|
8
|
|
|
from .. import filters |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class TitleTest(unittest.TestCase): |
|
12
|
|
|
"""All tests for title function.""" |
|
13
|
|
|
|
|
14
|
|
|
def test_title_returns_str(self): |
|
15
|
|
|
"""Test the return value for a valid type.""" |
|
16
|
|
|
self.assertIsInstance(filters.title('foo bar'), str) |
|
17
|
|
|
|
|
18
|
|
|
def test_title_word(self): |
|
19
|
|
|
"""Test the `word` argument.""" |
|
20
|
|
|
self.assertEqual(filters.title('foo bar'), 'Foo bar') |
|
21
|
|
|
|
|
22
|
|
|
def test_title_capitalize(self): |
|
23
|
|
|
"""Test the `capitalize` argument.""" |
|
24
|
|
|
self.assertEqual( |
|
25
|
|
|
filters.title('foo bar', capitalize=True), 'Foo Bar') |
|
26
|
|
|
|
|
27
|
|
|
def test_title_capitalize_sentence(self): |
|
28
|
|
|
"""Test the `capitalize` argument.""" |
|
29
|
|
|
self.assertEqual(filters.title( |
|
30
|
|
|
'the quick brown fox... ah forget it', capitalize=True), |
|
31
|
|
|
'The Quick Brown Fox... Ah Forget It') |
|
32
|
|
|
|
|
33
|
|
|
def test_title_none(self): |
|
34
|
|
|
"""Test the function with None argument.""" |
|
35
|
|
|
self.assertEqual(filters.questionize_label(None), '') |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class QuestionizeLabelTest(unittest.TestCase): |
|
39
|
|
|
"""All tests for questionize label function.""" |
|
40
|
|
|
|
|
41
|
|
|
def test_questionize_label_returns_str(self): |
|
42
|
|
|
"""Test the return value for a valid type.""" |
|
43
|
|
|
self.assertIsInstance(filters.questionize_label('foo bar'), str) |
|
44
|
|
|
|
|
45
|
|
|
def test_questionize_label_word_is(self): |
|
46
|
|
|
"""Test the `word` argument.""" |
|
47
|
|
|
self.assertEqual(filters.questionize_label('is_cool'), 'cool?') |
|
48
|
|
|
|
|
49
|
|
|
def test_questionize_label_word_has(self): |
|
50
|
|
|
"""Test the `word` argument.""" |
|
51
|
|
|
self.assertEqual(filters.questionize_label('has_stuff'), 'stuff?') |
|
52
|
|
|
|
|
53
|
|
|
def test_questionize_label_none(self): |
|
54
|
|
|
"""Test the function with None argument.""" |
|
55
|
|
|
self.assertEqual(filters.questionize_label(None), '') |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class FirstOfTest(unittest.TestCase): |
|
59
|
|
|
"""All tests for first of function.""" |
|
60
|
|
|
|
|
61
|
|
|
def test_firstof_all_false(self): |
|
62
|
|
|
"""Test what is returned when all values are falsy.""" |
|
63
|
|
|
self.assertEqual(filters.firstof([None, False, 0]), '') |
|
64
|
|
|
|
|
65
|
|
|
def test_firstof_last_true(self): |
|
66
|
|
|
"""Test what is returned when last value is true.""" |
|
67
|
|
|
self.assertEqual(filters.firstof([None, False, 0, 'yay']), 'yay') |
|
68
|
|
|
|
|
69
|
|
|
def test_firstof_first_true(self): |
|
70
|
|
|
"""Test what is returned when first value is true.""" |
|
71
|
|
|
self.assertEqual(filters.firstof(['yay', False, 0, 'yay']), 'yay') |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
class AddTest(unittest.TestCase): |
|
75
|
|
|
"""All tests for add function.""" |
|
76
|
|
|
|
|
77
|
|
|
def test_returns_updated_list(self): |
|
78
|
|
|
"""Test return value.""" |
|
79
|
|
|
self.assertEqual(filters.add([1, 2], 3), [1, 2, 3]) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
class CutTest(unittest.TestCase): |
|
83
|
|
|
"""All tests for cut function.""" |
|
84
|
|
|
|
|
85
|
|
|
def test_returns_updated_string(self): |
|
86
|
|
|
"""Test return value.""" |
|
87
|
|
|
self.assertEqual(filters.cut('Hello world', ['world']), 'Hello ') |
|
88
|
|
|
|
|
89
|
|
|
def test_returns_updated_multi(self): |
|
90
|
|
|
"""Test return value.""" |
|
91
|
|
|
self.assertEqual(filters.cut( |
|
92
|
|
|
'Well hello world', ['hello', 'world']), 'Well ') |
|
93
|
|
|
|
|
94
|
|
|
def test_returns_updated_multispace(self): |
|
95
|
|
|
"""Test return value.""" |
|
96
|
|
|
self.assertEqual(filters.cut( |
|
97
|
|
|
'String with spaces', [' ']), 'Stringwithspaces') |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
class AddSlashesTest(unittest.TestCase): |
|
101
|
|
|
"""All tests for add slashes function.""" |
|
102
|
|
|
|
|
103
|
|
|
def test_returns_updated_basic(self): |
|
104
|
|
|
"""Test return value.""" |
|
105
|
|
|
res = filters.addslashes("I'm using Flask!") |
|
106
|
|
|
self.assertEqual(res, "I\\'m using Flask!") |
|
107
|
|
|
|
|
108
|
|
|
def test_returns_updated_empty(self): |
|
109
|
|
|
"""Test return value.""" |
|
110
|
|
|
res = filters.addslashes("Using Flask!") |
|
111
|
|
|
self.assertEqual(res, "Using Flask!") |
|
112
|
|
|
|
|
113
|
|
|
def test_returns_updated_complex(self): |
|
114
|
|
|
"""Test return value.""" |
|
115
|
|
|
res = filters.addslashes("I'm u's'i'n'g Flask!") |
|
116
|
|
|
self.assertEqual(res, "I\\'m u\\'s\\'i\\'n\\'g Flask!") |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
class DefaultValTest(unittest.TestCase): |
|
120
|
|
|
"""All tests for default val function.""" |
|
121
|
|
|
|
|
122
|
|
|
def test_returns_default(self): |
|
123
|
|
|
"""Test return value.""" |
|
124
|
|
|
self.assertEqual(filters.default(False, 'default'), 'default') |
|
125
|
|
|
|
|
126
|
|
|
def test_returns_original(self): |
|
127
|
|
|
"""Test return value.""" |
|
128
|
|
|
self.assertEqual(filters.default(1, 'default'), 1) |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
class DefaultIfNoneValTest(unittest.TestCase): |
|
132
|
|
|
"""All tests for default if none function.""" |
|
133
|
|
|
|
|
134
|
|
|
def test_returns_default(self): |
|
135
|
|
|
"""Test return value.""" |
|
136
|
|
|
self.assertEqual(filters.default_if_none( |
|
137
|
|
|
None, 'default'), 'default') |
|
138
|
|
|
|
|
139
|
|
|
def test_returns_original(self): |
|
140
|
|
|
"""Test return value.""" |
|
141
|
|
|
self.assertEqual(filters.default_if_none(1, 'default'), 1) |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
class GetDigitTest(unittest.TestCase): |
|
145
|
|
|
"""All tests for get digit function.""" |
|
146
|
|
|
|
|
147
|
|
|
def test_returns_index_empty(self): |
|
148
|
|
|
"""Test return value.""" |
|
149
|
|
|
self.assertEqual(filters.get_digit(123456789, 0), 123456789) |
|
150
|
|
|
|
|
151
|
|
|
def test_returns_index_end(self): |
|
152
|
|
|
"""Test return value.""" |
|
153
|
|
|
self.assertEqual(filters.get_digit(123456789, 1), 9) |
|
154
|
|
|
|
|
155
|
|
|
def test_returns_index_mid(self): |
|
156
|
|
|
"""Test return value.""" |
|
157
|
|
|
self.assertEqual(filters.get_digit(123456789, 5), 5) |
|
158
|
|
|
|
|
159
|
|
|
def test_returns_index_beg(self): |
|
160
|
|
|
"""Test return value.""" |
|
161
|
|
|
self.assertEqual(filters.get_digit(123456789, 9), 1) |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
class LengthIsTest(unittest.TestCase): |
|
165
|
|
|
"""All tests for length is function.""" |
|
166
|
|
|
|
|
167
|
|
|
def test_returns_false(self): |
|
168
|
|
|
"""Test return value.""" |
|
169
|
|
|
self.assertFalse(filters.length_is('three', 4)) |
|
170
|
|
|
|
|
171
|
|
|
def test_returns_true(self): |
|
172
|
|
|
"""Test return value.""" |
|
173
|
|
|
self.assertTrue(filters.length_is('one', 3)) |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
class IsUrlTest(unittest.TestCase): |
|
177
|
|
|
"""All tests for is url function.""" |
|
178
|
|
|
|
|
179
|
|
|
def test_returns_urls_true(self): |
|
180
|
|
|
"""Test return value.""" |
|
181
|
|
|
self.assertTrue(filters.is_url('http://foo.bar')) |
|
182
|
|
|
self.assertTrue(filters.is_url('https://foo.bar')) |
|
183
|
|
|
|
|
184
|
|
|
def test_returns_urls_false(self): |
|
185
|
|
|
"""Test return value.""" |
|
186
|
|
|
self.assertFalse(filters.is_url('//foo.bar')) |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
class LJustTest(unittest.TestCase): |
|
190
|
|
|
"""All tests for ljust function.""" |
|
191
|
|
|
|
|
192
|
|
|
def test_returns_lpadding(self): |
|
193
|
|
|
"""Test return value.""" |
|
194
|
|
|
self.assertEqual(filters.ljust('Flask', 10), 'Flask ') |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
class RJustTest(unittest.TestCase): |
|
198
|
|
|
"""All tests for rjust function.""" |
|
199
|
|
|
|
|
200
|
|
|
def test_returns_rpadding(self): |
|
201
|
|
|
"""Test return value.""" |
|
202
|
|
|
self.assertEqual(filters.rjust('Flask', 10), ' Flask') |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
class MakeListTest(unittest.TestCase): |
|
206
|
|
|
"""All tests for make list function.""" |
|
207
|
|
|
|
|
208
|
|
|
def test_list2list(self): |
|
209
|
|
|
"""Test return value.""" |
|
210
|
|
|
self.assertEqual(filters.make_list([1, 2]), [1, 2]) |
|
211
|
|
|
|
|
212
|
|
|
def test_ints_not_coerced(self): |
|
213
|
|
|
"""Test return value.""" |
|
214
|
|
|
self.assertEqual(filters.make_list( |
|
215
|
|
|
'12', coerce_numbers=False), ['1', '2']) |
|
216
|
|
|
|
|
217
|
|
|
def test_ints_coerced(self): |
|
218
|
|
|
"""Test return value.""" |
|
219
|
|
|
self.assertEqual(filters.make_list('12'), [1, 2]) |
|
220
|
|
|
|
|
221
|
|
|
def test_dict(self): |
|
222
|
|
|
"""Test return value.""" |
|
223
|
|
|
self.assertEqual(filters.make_list({'foo': 'bar'}), [('foo', 'bar')]) |
|
224
|
|
|
|
|
225
|
|
|
def test_list(self): |
|
226
|
|
|
"""Test return value.""" |
|
227
|
|
|
self.assertEqual(filters.make_list([1, 2]), [1, 2]) |
|
228
|
|
|
|
|
229
|
|
|
def test_str(self): |
|
230
|
|
|
"""Test return value.""" |
|
231
|
|
|
self.assertEqual(filters.make_list('abc'), ['a', 'b', 'c']) |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
class Phone2NumericTest(unittest.TestCase): |
|
235
|
|
|
"""All tests for phone2numeric function.""" |
|
236
|
|
|
|
|
237
|
|
|
def test_basic(self): |
|
238
|
|
|
"""Test return value.""" |
|
239
|
|
|
self.assertEqual( |
|
240
|
|
|
filters.phone2numeric('1800-COLLeCT'), '1800-2655328') |
|
241
|
|
|
|
|
242
|
|
|
def test_1_thru_9(self): |
|
243
|
|
|
"""Test return value.""" |
|
244
|
|
|
self.assertEqual( |
|
245
|
|
|
filters.phone2numeric('1800-ADGJMPTX'), '1800-23456789') |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
class SlugifyTest(unittest.TestCase): |
|
249
|
|
|
"""All tests for slugify function.""" |
|
250
|
|
|
|
|
251
|
|
|
def test_slugify_plain(self): |
|
252
|
|
|
"""Test return value.""" |
|
253
|
|
|
self.assertEqual(filters.slugify('My news title!'), 'my-news-title') |
|
254
|
|
|
|
|
255
|
|
|
def test_slugify_complex(self): |
|
256
|
|
|
"""Test return value.""" |
|
257
|
|
|
self.assertEqual( |
|
258
|
|
|
filters.slugify('I am an OBFUsc@@Ted URL!!! Foo bar'), |
|
259
|
|
|
'i-am-an-obfusc--ted-url----foo-bar') |
|
260
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
class PagetitleTest(unittest.TestCase): |
|
263
|
|
|
"""All tests for pagetitle function.""" |
|
264
|
|
|
|
|
265
|
|
|
def test_title_plain(self): |
|
266
|
|
|
"""Test return value.""" |
|
267
|
|
|
self.assertEqual( |
|
268
|
|
|
filters.pagetitle('/foo/bar/bam'), ' > foo > bar > bam') |
|
269
|
|
|
|
|
270
|
|
|
def test_title_removefirst(self): |
|
271
|
|
|
"""Test return value.""" |
|
272
|
|
|
self.assertEqual( |
|
273
|
|
|
filters.pagetitle('/foo/bar/bam', divider=' | '), |
|
274
|
|
|
' | foo | bar | bam') |
|
275
|
|
|
|
|
276
|
|
|
def test_title_divider(self): |
|
277
|
|
|
"""Test return value.""" |
|
278
|
|
|
self.assertEqual( |
|
279
|
|
|
filters.pagetitle('/foo/bar/bam', remove_first=True), |
|
280
|
|
|
'foo > bar > bam') |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
class GreetTest(unittest.TestCase): |
|
284
|
|
|
"""All tests for greet function.""" |
|
285
|
|
|
|
|
286
|
|
|
def test_greet(self): |
|
287
|
|
|
"""Test return value.""" |
|
288
|
|
|
self.assertEqual(filters.greet('Chris'), 'Hello, Chris!') |
|
289
|
|
|
|
|
290
|
|
|
def test_greet_override(self): |
|
291
|
|
|
"""Test return value.""" |
|
292
|
|
|
self.assertEqual(filters.greet( |
|
293
|
|
|
'Chris', greeting='Bonjour'), 'Bonjour, Chris!') |
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
class IsListTest(unittest.TestCase): |
|
297
|
|
|
"""All tests for islist function.""" |
|
298
|
|
|
|
|
299
|
|
|
def test_islist(self): |
|
300
|
|
|
"""Test return value.""" |
|
301
|
|
|
self.assertTrue(filters.islist([1, 2, 3])) |
|
302
|
|
|
|
|
303
|
|
|
def test_notislist(self): |
|
304
|
|
|
"""Test return value.""" |
|
305
|
|
|
self.assertFalse(filters.islist('Foo')) |
|
306
|
|
|
self.assertFalse(filters.islist({'foo': 'bar'})) |
|
307
|
|
|
self.assertFalse(filters.islist(1)) |
|
308
|
|
|
self.assertFalse(filters.islist(1.0)) |
|
309
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
class Sql2dictTest(unittest.TestCase): |
|
312
|
|
|
"""All tests for sql2dict function.""" |
|
313
|
|
|
|
|
314
|
|
|
def setUp(self): |
|
315
|
|
|
"""Setup fake sql class.""" |
|
316
|
|
|
self.mm = MagicMock() |
|
317
|
|
|
self.mm.__dict__ = {'foo': 'bar'} |
|
318
|
|
|
|
|
319
|
|
|
def test_none(self): |
|
320
|
|
|
"""Test return value.""" |
|
321
|
|
|
self.assertEqual(filters.sql2dict(None), []) |
|
322
|
|
|
|
|
323
|
|
|
def test_set(self): |
|
324
|
|
|
"""Test return value.""" |
|
325
|
|
|
self.assertEqual(filters.sql2dict([self.mm]), [{'foo': 'bar'}]) |
|
326
|
|
|
|