Completed
Push — master ( 7af861...bb4bfe )
by Chris
01:13
created

TestSlugify.test_slugify_complex()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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