Passed
Branch master (2e0d62)
by Max
50s
created

tests.structured_data.test__prewritten_methods   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 109
rs 9.0399
c 0
b 0
f 0
wmc 42

How to fix   Complexity   

Complexity

Complex classes like tests.structured_data.test__prewritten_methods 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
import pytest
2
3
4
@pytest.fixture(scope='session')
5
def classes(_enum_constructor, _prewritten_methods):
6
    class Base(tuple):
7
8
        __slots__ = ()
9
10
        __repr__ = _prewritten_methods.PrewrittenMethods.__repr__
11
        __eq__ = _prewritten_methods.PrewrittenMethods.__eq__
12
        __ne__ = _prewritten_methods.PrewrittenMethods.__ne__
13
        __lt__ = _prewritten_methods.PrewrittenMethods.__lt__
14
        __le__ = _prewritten_methods.PrewrittenMethods.__le__
15
        __gt__ = _prewritten_methods.PrewrittenMethods.__gt__
16
        __ge__ = _prewritten_methods.PrewrittenMethods.__ge__
17
        __hash__ = _prewritten_methods.PrewrittenMethods.__hash__
18
        __setattr__ = _prewritten_methods.PrewrittenMethods.__setattr__
19
        __delattr__ = _prewritten_methods.PrewrittenMethods.__delattr__
20
        __bool__ = _prewritten_methods.PrewrittenMethods.__bool__
21
22
    class Derived1(Base):
23
24
        __slots__ = ()
25
26
        an_attr = None
27
28
    class Derived2(Base):
29
30
        __slots__ = ()
31
32
    Base.__init_subclass__ = _prewritten_methods.PrewrittenMethods.__init_subclass__
33
34
    for cls in (Derived1, Derived2):
35
        _enum_constructor.ENUM_BASES[cls] = Base
36
37
    _prewritten_methods.SUBCLASS_ORDER[Base] = (Derived1, Derived2)
38
39
    return Base, Derived1, Derived2
40
41
42
def test_repr(classes):
43
    assert repr(classes[1]()) == 'classes.<locals>.Derived1()'
44
45
46
def test_eq(classes):
47
    assert classes[1]() == classes[1]()
48
    assert not (classes[1]() == classes[2]())
49
50
51
def test_ne(classes):
52
    assert not (classes[1]() != classes[1]())
53
    assert classes[1]() != classes[2]()
54
    assert classes[1]() != (True,)
55
56
57
def test_lt(classes):
58
    assert not (classes[1]() < classes[1]())
59
    assert classes[1]() < classes[2]()
60
    with pytest.raises(TypeError):
61
        assert not classes[1]() < (True,)
62
63
64
def test_le(classes):
65
    assert classes[1]() <= classes[1]()
66
    assert classes[1]() <= classes[2]()
67
    with pytest.raises(TypeError):
68
        assert not classes[1]() <= ()
69
70
71
def test_gt(classes):
72
    assert not (classes[1]() > classes[1]())
73
    assert not (classes[1]() > classes[2]())
74
    with pytest.raises(TypeError):
75
        assert not not (classes[1]() > (True,))
76
77
78
def test_ge(classes):
79
    assert classes[1]() >= classes[1]()
80
    assert not (classes[1]() >= classes[2]())
81
    with pytest.raises(TypeError):
82
        assert not classes[1]() >= ()
83
84
85
def test_hash(classes):
86
    assert hash(classes[1]()) == hash(())
87
88
89
def test_cant_set(classes):
90
    with pytest.raises(AttributeError):
91
        classes[1]().attr = None
92
    with pytest.raises(AttributeError):
93
        classes[1]().__slots__ = None
94
95
96
def test_cant_del(classes):
97
    with pytest.raises(AttributeError):
98
        del classes[1]().attr
99
100
101
def test_bool(classes):
102
    assert classes[1]()
103
104
105
def test_cant_subclass(classes):
106
    with pytest.raises(TypeError):
107
        class Test(classes[0]):
108
            pass
109