Completed
Push — master ( 714d51...88053e )
by Chris
01:36
created

TestGroupBy.test_returns_no_objs_with_name()   A

Complexity

Conditions 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 6
rs 9.2
1
"""Test munging filters."""
2
3
from flask_extras.filters import munging
4
5
6
class TestFilterVals:
7
    """All tests for filter_vals function."""
8
9
    def test_title_returns_invalid_first(self):
10
        """Test function."""
11
        assert munging.filter_vals({}, None) == {}
12
13
    def test_title_returns_invalid_second(self):
14
        """Test function."""
15
        assert munging.filter_vals(None, []) is None
16
17
    def test_title_returns_invalid_both(self):
18
        """Test function."""
19
        assert munging.filter_vals(None, None) is None
20
21
    def test_title_returns_valid_empty(self):
22
        """Test function."""
23
        assert munging.filter_vals(dict(), []) == {}
24
25
    def test_title_returns_valid_filtered_empty(self):
26
        """Test function."""
27
        assert munging.filter_vals(dict(foo='bar'), ['bar']) == {}
28
29
    def test_title_returns_valid_filtered(self):
30
        """Test function."""
31
        assert munging.filter_vals(
32
            dict(foo='bar', bar='foo'), ['bar']) == dict(bar='foo')
33
34
    def test_title_returns_valid_filtered_invalid_val(self):
35
        """Test function."""
36
        d = dict(foo='bar', bar='foo')
37
        assert munging.filter_vals(d, ['baz']) == d
38
39
40
class TestFilterKeys:
41
    """All tests for filter_keys function."""
42
43
    def test_title_returns_invalid_first(self):
44
        """Test function."""
45
        assert munging.filter_keys({}, None) == {}
46
47
    def test_title_returns_invalid_second(self):
48
        """Test function."""
49
        assert munging.filter_keys(None, []) is None
50
51
    def test_title_returns_invalid_both(self):
52
        """Test function."""
53
        assert munging.filter_keys(None, None) is None
54
55
    def test_title_returns_valid_empty(self):
56
        """Test function."""
57
        assert munging.filter_keys(dict(), []) == {}
58
59
    def test_title_returns_valid_filtered_empty(self):
60
        """Test function."""
61
        assert munging.filter_keys(dict(foo='bar'), ['foo']) == {}
62
63
    def test_title_returns_valid_filtered(self):
64
        """Test function."""
65
        assert munging.filter_keys(
66
            dict(foo='bar', bar='foo'), ['bar']) == dict(foo='bar')
67
68
    def test_title_returns_valid_filtered_invalid_val(self):
69
        """Test function."""
70
        d = dict(foo='bar', bar='foo')
71
        assert munging.filter_keys(d, ['baz']) == d
72
73
74
class TestFilterList:
75
    """All tests for filter_list function."""
76
77
    def test_title_returns_invalid_first(self):
78
        """Test function."""
79
        assert munging.filter_list([], None) == []
80
81
    def test_title_returns_invalid_second(self):
82
        """Test function."""
83
        assert munging.filter_list(None, []) is None
84
85
    def test_title_returns_invalid_both(self):
86
        """Test function."""
87
        assert munging.filter_list(None, None) is None
88
89
    def test_title_returns_invalid_dict(self):
90
        """Test function."""
91
        assert munging.filter_list(dict(), []) == dict()
92
93
    def test_title_returns_valid_filtered_empty(self):
94
        """Test function."""
95
        assert munging.filter_list([], ['foo']) == []
96
97
    def test_title_returns_valid_filtered(self):
98
        """Test function."""
99
        assert munging.filter_list(['foo', 'bar'], ['bar']) == ['foo']
100
101
    def test_title_returns_valid_filtered_invalid_val(self):
102
        """Test function."""
103
        assert munging.filter_list(['foo', 'bar'], ['baz']) == ['foo', 'bar']
104
105
106
class TestGroupBy:
107
    """All tests for group_by function."""
108
109
    def _get_obj(self, name):
110
        """Data for tests."""
111
        class ObjClass(object):
112
            def __init__(self, name=None):
113
                if name is not None:
114
                    self.name = name
115
        return ObjClass(name=name)
116
117
    def test_returns_no_objs_noname(self):
118
        """Test function."""
119
        objs = [None for _ in range(4)]
120
        res = munging.group_by(objs, attr=None)
121
        assert res.keys() == ['__unlabeled']
122
        assert len(res['__unlabeled']) == 4
123
124
    def test_returns_no_objs_with_name(self):
125
        """Test function."""
126
        objs = [None for _ in range(4)]
127
        res = munging.group_by(objs, attr='invalid-attr')
128
        assert res.keys() == ['__unlabeled']
129
        assert len(res['__unlabeled']) == 4
130
131
    def test_returns_objs_nogroup_noname(self):
132
        """Test function."""
133
        objs = [self._get_obj(name) for name in ['foo1']]
134
        res = munging.group_by(objs, attr=None)
135
        assert res.keys() == ['__unlabeled']
136
        assert len(res['__unlabeled']) == 1
137
138
    def test_returns_objs_nogroup(self):
139
        """Test function."""
140
        objs = [self._get_obj(None)]
141
        res = munging.group_by(objs, attr='name')
142
        assert res.keys() == ['__unlabeled']
143
        assert len(res['__unlabeled']) == 1
144
145
    def test_returns_objs_group_custom_group(self):
146
        """Test function."""
147
        objs = [self._get_obj(name) for name in ['foo1', 'foo2']]
148
        groups = [('group1', ('foo1', 'foo2'))]
149
        res = munging.group_by(objs, groups=groups, attr='name')
150
        assert res.keys() == ['group1', '__unlabeled']
151
        assert len(res['group1']) == 2
152
153
    def test_returns_objs_group_custom_group_with_one_unlabeled(self):
154
        """Test function."""
155
        objs = [self._get_obj(name) for name in ['foo1', 'foo2', 'foo3']]
156
        groups = [('group1', ('foo1', 'foo2'))]
157
        res = munging.group_by(objs, groups=groups, attr='name')
158
        assert res.keys() == ['group1', '__unlabeled']
159
        assert len(res['group1']) == 2
160
        assert len(res['__unlabeled']) == 1
161
162
    def test_returns_objs_group_custom_group_with_one_unlabeled_complex(self):
163
        """Test function."""
164
        names = ['foo{}'.format(i) for i in range(1, 11)]
165
        objs = [self._get_obj(name) for name in names]
166
        groups = [
167
            ('group1', ('foo1', 'foo2', 'foo3')),
168
            ('group2', ('foo4', 'foo5', 'foo6')),
169
            ('group3', ('foo7', 'foo8', 'foo9')),
170
        ]
171
        res = munging.group_by(objs, groups=groups, attr='name')
172
        for key in res.keys():
173
            assert key in ['group1', 'group2', 'group3', '__unlabeled']
174
        assert len(res.keys()) == 4
175
        assert len(res['group1']) == 3
176
        # assert len(res['group2']) == 3
177
        # assert len(res['group3']) == 3
178
        assert len(res['__unlabeled']) == 1
179