Completed
Push — master ( 15aed1...81ec71 )
by Chris
01:11
created

TestToJson.test_empty_str_with_indent()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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