Completed
Push — master ( 9ee029...d17eb3 )
by Chris
01:31
created

TestCssSelector.test_basic()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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