Issues (5)

tests/test_munging.py (2 issues)

1
"""Test munging filters."""
2
3
from flask_extras.filters import munging
4
5
import pytest
6
7
8
class TestFilterVals:
9
    """All tests for filter_vals function."""
10
11
    def test_title_returns_invalid_first(self):
12
        """Test function."""
13
        assert munging.filter_vals({}, None) == {}
14
15
    def test_title_returns_invalid_second(self):
16
        """Test function."""
17
        assert munging.filter_vals(None, []) is None
18
19
    def test_title_returns_invalid_both(self):
20
        """Test function."""
21
        assert munging.filter_vals(None, None) is None
22
23
    def test_title_returns_valid_empty(self):
24
        """Test function."""
25
        assert munging.filter_vals(dict(), []) == {}
26
27
    def test_title_returns_valid_filtered_empty(self):
28
        """Test function."""
29
        assert munging.filter_vals(dict(foo='bar'), ['bar']) == {}
30
31
    def test_title_returns_valid_filtered(self):
32
        """Test function."""
33
        assert munging.filter_vals(
34
            dict(foo='bar', bar='foo'), ['bar']) == dict(bar='foo')
35
36
    def test_title_returns_valid_filtered_invalid_val(self):
37
        """Test function."""
38
        d = dict(foo='bar', bar='foo')
39
        assert munging.filter_vals(d, ['baz']) == d
40
41
42
class TestFilterKeys:
43
    """All tests for filter_keys function."""
44
45
    def test_title_returns_invalid_first(self):
46
        """Test function."""
47
        assert munging.filter_keys({}, None) == {}
48
49
    def test_title_returns_invalid_second(self):
50
        """Test function."""
51
        assert munging.filter_keys(None, []) is None
52
53
    def test_title_returns_invalid_both(self):
54
        """Test function."""
55
        assert munging.filter_keys(None, None) is None
56
57
    def test_title_returns_valid_empty(self):
58
        """Test function."""
59
        assert munging.filter_keys(dict(), []) == {}
60
61
    def test_title_returns_valid_filtered_empty(self):
62
        """Test function."""
63
        assert munging.filter_keys(dict(foo='bar'), ['foo']) == {}
64
65
    def test_title_returns_valid_filtered(self):
66
        """Test function."""
67
        assert munging.filter_keys(
68
            dict(foo='bar', bar='foo'), ['bar']) == dict(foo='bar')
69
70
    def test_title_returns_valid_filtered_invalid_val(self):
71
        """Test function."""
72
        d = dict(foo='bar', bar='foo')
73
        assert munging.filter_keys(d, ['baz']) == d
74
75
76
class TestFilterList:
77
    """All tests for filter_list function."""
78
79
    def test_title_returns_invalid_first(self):
80
        """Test function."""
81
        assert munging.filter_list([], None) == []
82
83
    def test_title_returns_invalid_second(self):
84
        """Test function."""
85
        assert munging.filter_list(None, []) is None
86
87
    def test_title_returns_invalid_both(self):
88
        """Test function."""
89
        assert munging.filter_list(None, None) is None
90
91
    def test_title_returns_invalid_dict(self):
92
        """Test function."""
93
        assert munging.filter_list(dict(), []) == dict()
94
95
    def test_title_returns_valid_filtered_empty(self):
96
        """Test function."""
97
        assert munging.filter_list([], ['foo']) == []
98
99
    def test_title_returns_valid_filtered(self):
100
        """Test function."""
101
        assert munging.filter_list(['foo', 'bar'], ['bar']) == ['foo']
102
103
    def test_title_returns_valid_filtered_invalid_val(self):
104
        """Test function."""
105
        assert munging.filter_list(['foo', 'bar'], ['baz']) == ['foo', 'bar']
106
107
108
class TestGroupBy:
109
    """All tests for group_by function."""
110
111
    def _get_obj(self, name):
112
        """Data for tests."""
113
        class ObjClass(object):
114
            def __init__(self, name=None):
115
                if name is not None:
116
                    self.name = name
117
        return ObjClass(name=name)
118
119
    def test_returns_no_objs_noname(self):
120
        """Test function."""
121
        objs = [None for _ in range(4)]
122
        res = munging.group_by(objs, attr=None)
123
        assert res.keys() == ['__unlabeled']
124
        assert len(res['__unlabeled']) == 4
125
126
    def test_returns_no_objs_with_name(self):
127
        """Test function."""
128
        objs = [None for _ in range(4)]
129
        res = munging.group_by(objs, attr='invalid-attr')
130
        assert res.keys() == ['__unlabeled']
131
        assert len(res['__unlabeled']) == 4
132
133
    def test_returns_objs_nogroup_noname(self):
134
        """Test function."""
135
        objs = [self._get_obj(name) for name in ['foo1']]
136
        res = munging.group_by(objs, attr=None)
137
        assert res.keys() == ['__unlabeled']
138
        assert len(res['__unlabeled']) == 1
139
140
    def test_returns_objs_nogroup_fallback(self):
141
        """Test function."""
142
        objs = [self._get_obj(name) for name in ['foo1']]
143
        res = munging.group_by(objs, attr=None, fallback='somegroup')
144
        assert res.keys() == ['somegroup']
145
        assert len(res['somegroup']) == 1
146
147
    def test_returns_objs_nogroup(self):
148
        """Test function."""
149
        objs = [self._get_obj(None)]
150
        res = munging.group_by(objs, attr='name')
151
        assert res.keys() == ['__unlabeled']
152
        assert len(res['__unlabeled']) == 1
153
154
    def test_returns_objs_group_custom_group(self):
155
        """Test function."""
156
        objs = [self._get_obj(name) for name in ['foo1', 'foo2']]
157
        groups = [('group1', ('foo1', 'foo2'))]
158
        res = munging.group_by(objs, groups=groups, attr='name')
159
        assert res.keys() == ['group1', '__unlabeled']
160
        assert len(res['group1']) == 2
161
162
    def test_returns_objs_group_custom_group_with_one_unlabeled(self):
163
        """Test function."""
164
        objs = [self._get_obj(name) for name in ['foo1', 'foo2', 'foo3']]
165
        groups = [('group1', ('foo1', 'foo2'))]
166
        res = munging.group_by(objs, groups=groups, attr='name')
167
        assert res.keys() == ['group1', '__unlabeled']
168
        assert len(res['group1']) == 2
169
        assert len(res['__unlabeled']) == 1
170
171
    def test_returns_objs_group_custom_group_with_one_unlabeled_complex(self):
172
        """Test function."""
173
        names = ['foo{}'.format(i) for i in range(1, 11)]
174
        objs = [self._get_obj(name) for name in names]
175
        groups = [
176
            ('group1', ('foo1', 'foo2', 'foo3')),
177
            ('group2', ('foo4', 'foo5', 'foo6')),
178
            ('group3', ('foo7', 'foo8', 'foo9')),
179
        ]
180
        res = munging.group_by(objs, groups=groups, attr='name')
181
        for key in res.keys():
182
            assert key in ['group1', 'group2', 'group3', '__unlabeled']
183
        assert len(res.keys()) == 4
184
        assert len(res['group1']) == 3
185
        assert len(res['group2']) == 3
186
        assert len(res['group3']) == 3
187
        assert len(res['__unlabeled']) == 1
188
189
    def test_returns_objs_group_custom_group_with_order_preserved(self):
190
        """Test function."""
191
        names = ['foo{}'.format(i) for i in range(1, 10)]
192
        objs = [self._get_obj(name) for name in names]
193
        groups = [
194
            ('group1', ('foo2', 'foo1', 'foo3')),
195
            ('group2', ('foo5', 'foo4', 'foo6')),
196
            ('group3', ('foo7', 'foo9', 'foo8')),
197
        ]
198
        res = munging.group_by(objs, groups=groups, attr='name')
199
        for key in res.keys():
200
            assert key in ['group1', 'group2', 'group3', '__unlabeled']
201
        for group in groups:
202
            label, items = group
203
            for i, item in enumerate(items):
204
                obj_label = getattr(res[label][i], 'name')
205
                assert item == obj_label
206
207
208 View Code Duplication
class TestSortDictKeysFromReflist:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
209
    """All tests for sort_dict_keys_from_reflist function."""
210
211
    def test_sort_dict_keys_from_reflist(self):
212
        """Test function."""
213
        data = dict(foo=1, bar=2, baz=3, quux=4)
214
        ref = ['quux', 'baz', 'foo', 'bar']
215
        expected = [('quux', 4), ('baz', 3), ('foo', 1), ('bar', 2)]
216
        assert munging.sort_dict_keys_from_reflist(data, ref) == expected
217
218
    def test_sort_dict_keys_from_reflist_nested(self):
219
        """Test function."""
220
        data = dict(foo=dict(inner1=1, inner2=2), bar=2, baz=3, quux=4)
221
        ref = ['quux', 'baz', 'foo', 'bar']
222
        expected = [
223
            ('quux', 4), ('baz', 3),
224
            ('foo', {'inner1': 1, 'inner2': 2}), ('bar', 2)]
225
        assert munging.sort_dict_keys_from_reflist(data, ref) == expected
226
227
    def test_sort_dict_keys_from_reflist_none(self):
228
        """Test function."""
229
        data = dict(foo=None, bar=2, baz=3, quux=4)
230
        ref = ['quux', 'baz', 'foo', 'bar']
231
        expected = [('quux', 4), ('baz', 3), ('foo', None), ('bar', 2)]
232
        assert munging.sort_dict_keys_from_reflist(data, ref) == expected
233
234
    def test_sort_dict_keys_from_reflist_missing_val(self):
235
        """Test function."""
236
        data = dict(foo=1, bar=2, baz=3, quux=4)
237
        ref = ['quux', 'baz', 'foo']
238
        expected = [('quux', 4), ('baz', 3), ('foo', 1)]
239
        assert munging.sort_dict_keys_from_reflist(data, ref) == expected
240
241
242 View Code Duplication
class TestSortDictValsFromReflist:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
243
    """All tests for sort_dict_vals_from_reflist function."""
244
245
    def test_sort_dict_vals_from_reflist(self):
246
        """Test function."""
247
        data = dict(foo=1, bar=2, baz=3, quux=4)
248
        ref = [4, 3, 1, 2]
249
        expected = [('quux', 4), ('baz', 3), ('foo', 1), ('bar', 2)]
250
        assert munging.sort_dict_vals_from_reflist(data, ref) == expected
251
252
    def test_sort_dict_vals_from_reflist_nested(self):
253
        """Test function."""
254
        data = dict(foo=dict(inner1=1, inner2=2), bar=2, baz=3, quux=4)
255
        ref = [4, 3, {'inner1': 1, 'inner2': 2}, 2]
256
        expected = [
257
            ('quux', 4), ('baz', 3),
258
            ('foo', {'inner1': 1, 'inner2': 2}), ('bar', 2)]
259
        assert munging.sort_dict_vals_from_reflist(data, ref) == expected
260
261
    def test_sort_dict_vals_from_reflist_none(self):
262
        """Test function."""
263
        data = dict(foo=None, bar=2, baz=3, quux=4)
264
        ref = [4, 3, None, 2]
265
        expected = [('quux', 4), ('baz', 3), ('foo', None), ('bar', 2)]
266
        assert munging.sort_dict_vals_from_reflist(data, ref) == expected
267
268
    def test_sort_dict_vals_from_reflist_missing_val(self):
269
        """Test function."""
270
        data = dict(foo=1, bar=2, baz=3, quux=4)
271
        ref = [4, 3, 1]
272
        expected = [('quux', 4), ('baz', 3), ('foo', 1)]
273
        assert munging.sort_dict_vals_from_reflist(data, ref) == expected
274