Completed
Push — master ( 64da81...89f930 )
by Chris
01:28
created

test_returns_objs_group_custom_group_with_one_unlabeled_complex()   C

Complexity

Conditions 10

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
dl 0
loc 17
rs 6

How to fix   Complexity   

Complexity

Complex classes like TestGroupBy.test_returns_objs_group_custom_group_with_one_unlabeled_complex() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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_objs_nogroup_noname(self):
118
        """Test function."""
119
        objs = [self._get_obj(name) for name in ['foo1']]
120
        res = munging.group_by(objs, attr=None)
121
        assert res.keys() == ['__unlabeled']
122
        assert len(res['__unlabeled']) == 1
123
124
    def test_returns_objs_nogroup(self):
125
        """Test function."""
126
        objs = [self._get_obj(None)]
127
        res = munging.group_by(objs, attr='name')
128
        assert res.keys() == ['__unlabeled']
129
        assert len(res['__unlabeled']) == 1
130
131
    def test_returns_objs_group_custom_group(self):
132
        """Test function."""
133
        objs = [self._get_obj(name) for name in ['foo1', 'foo2']]
134
        groups = {'foo1': 'group1', 'foo2': 'group1'}
135
        res = munging.group_by(objs, groups=groups, attr='name')
136
        assert res.keys() == ['group1']
137
        assert len(res['group1']) == 2
138
139
    def test_returns_objs_group_custom_group_with_one_unlabeled(self):
140
        """Test function."""
141
        objs = [self._get_obj(name) for name in ['foo1', 'foo2', 'foo3']]
142
        groups = {'foo1': 'group1', 'foo2': 'group1'}
143
        res = munging.group_by(objs, groups=groups, attr='name')
144
        assert res.keys() == ['group1', '__unlabeled']
145
        assert len(res['group1']) == 2
146
        assert len(res['__unlabeled']) == 1
147
148
    def test_returns_objs_group_custom_group_with_one_unlabeled_complex(self):
149
        """Test function."""
150
        names = ['foo{}'.format(i) for i in range(1, 11)]
151
        objs = [self._get_obj(name) for name in names]
152
        groups = {
153
            'foo1': 'group1', 'foo2': 'group1', 'foo3': 'group1',
154
            'foo4': 'group2', 'foo5': 'group2', 'foo6': 'group2',
155
            'foo7': 'group3', 'foo8': 'group3', 'foo9': 'group3',
156
        }
157
        res = munging.group_by(objs, groups=groups, attr='name')
158
        for key in res.keys():
159
            assert key in ['group1', 'group2', 'group3', '__unlabeled']
160
        assert len(res.keys()) == 4
161
        assert len(res['group1']) == 3
162
        assert len(res['group2']) == 3
163
        assert len(res['group3']) == 3
164
        assert len(res['__unlabeled']) == 1
165