Completed
Push — develop ( e4f288...dec8f0 )
by Jace
02:13
created

yorm.test.describe_mapper()   F

Complexity

Conditions 24

Size

Total Lines 108

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 24
dl 0
loc 108
rs 2

21 Methods

Rating   Name   Duplication   Size   Complexity  
A yorm.test.can_be_set_true() 0 4 1
A yorm.test.it_creates_the_file() 0 6 1
A yorm.test.describe_delete() 0 15 3
A yorm.test.it_raises_an_exception_after_delete() 0 5 2
A yorm.test.is_false_after_fetch() 0 5 1
C yorm.test.describe_modified() 0 30 7
A yorm.test.can_be_set_false() 0 4 1
A yorm.test.describe_store() 0 6 2
A yorm.test.describe_fetch() 0 15 4
A yorm.test.is_true_after_delete() 0 4 1
A yorm.test.it_adds_missing_attributes() 0 7 1
A yorm.test.can_get_the_file_contents() 0 5 1
A yorm.test.it_pretends_when_fake() 0 6 1
A yorm.test.describe_text() 0 14 3
A yorm.test.it_deletes_the_file() 0 7 1
A yorm.test.it_creates_the_file_automatically() 0 4 1
A yorm.test.is_true_initially() 0 2 1
A yorm.test.describe_create() 0 21 4
A yorm.test.is_true_after_create() 0 4 1
A yorm.test.it_can_be_called_twice() 0 5 1
A yorm.test.can_set_the_file_contents() 0 6 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like yorm.test.describe_mapper() 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
# pylint: disable=missing-docstring,redefined-outer-name,unused-variable,expression-not-assigned
2
3
import pytest
4
from expecter import expect
0 ignored issues
show
Configuration introduced by
The import expecter could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6
import yorm
7
from yorm import exceptions
8
from yorm.mapper import Mapper
9
from yorm.types import Integer
10
11
12
class MyObject:
13
    var1 = 1
14
15
16
@pytest.fixture
17
def obj():
18
    return MyObject()
19
20
21
@pytest.fixture
22
def attrs():
23
    return {'var2': Integer, 'var3': Integer}
24
25
26
@pytest.yield_fixture(params=("real", "fake"))
27
def mapper(tmpdir, obj, attrs, request):
28
    path = request.param + "/path/to/file"
29
    backup = yorm.settings.fake
30
    if "fake" in path:
31
        yorm.settings.fake = True
32
    elif "real" in path:
33
        tmpdir.chdir()
34
    yield Mapper(obj, path, attrs)
35
    yorm.settings.fake = backup
36
37
38
@pytest.fixture
39
def mapper_real(tmpdir, obj, attrs):
40
    tmpdir.chdir()
41
    return Mapper(obj, "real/path/to/file", attrs)
42
43
44
@pytest.yield_fixture
45
def mapper_fake(obj, attrs):
46
    backup = yorm.settings.fake
47
    yorm.settings.fake = True
48
    yield Mapper(obj, "fake/path/to/file", attrs)
49
    yorm.settings.fake = backup
50
51
52
def describe_mapper():
53
54
    def describe_create():
55
56
        def it_creates_the_file(mapper_real):
57
            mapper_real.create()
58
59
            expect(mapper_real.path).exists()
60
            expect(mapper_real.exists).is_true()
61
            expect(mapper_real.deleted).is_false()
62
63
        def it_pretends_when_fake(mapper_fake):
64
            mapper_fake.create()
65
66
            expect(mapper_fake.path).missing()
67
            expect(mapper_fake.exists).is_true()
68
            expect(mapper_fake.deleted).is_false()
69
70
        def it_can_be_called_twice(mapper_real):
71
            mapper_real.create()
72
            mapper_real.create()  # should be ignored
73
74
            expect(mapper_real.path).exists()
75
76
    def describe_delete():
77
78
        def it_deletes_the_file(mapper):
79
            mapper.create()
80
            mapper.delete()
81
82
            expect(mapper.path).missing()
83
            expect(mapper.exists).is_false()
84
            expect(mapper.deleted).is_true()
85
86
        def it_can_be_called_twice(mapper):
87
            mapper.delete()
88
            mapper.delete()  # should be ignored
89
90
            expect(mapper.path).missing()
91
92
    def describe_fetch():
93
94
        def it_adds_missing_attributes(obj, mapper):
95
            mapper.create()
96
            mapper.fetch()
97
98
            expect(obj.var1) == 1
99
            expect(obj.var2) == 0
100
            expect(obj.var3) == 0
101
102
        def it_raises_an_exception_after_delete(mapper):
103
            mapper.delete()
104
105
            with expect.raises(exceptions.FileDeletedError):
106
                mapper.fetch()
107
108
    def describe_store():
109
110
        def it_creates_the_file_automatically(mapper_real):
111
            mapper_real.store()
112
113
            expect(mapper_real.path).exists()
114
115
    def describe_text():
116
117
        def can_get_the_file_contents(obj, mapper):
118
            obj.var3 = 42
119
            mapper.store()
120
121
            expect(mapper.text) == "var2: 0\nvar3: 42\n"
122
123
        def can_set_the_file_contents(obj, mapper):
124
            mapper.create()
125
            mapper.text = "var2: 42\n"
126
            mapper.fetch()
127
128
            expect(obj.var2) == 42
129
130
    def describe_modified():
131
132
        def is_true_initially(mapper):
133
            expect(mapper.modified).is_true()
134
135
        def is_true_after_create(mapper):
136
            mapper.create()
137
138
            expect(mapper.modified).is_true()
139
140
        def is_true_after_delete(mapper):
141
            mapper.delete()
142
143
            expect(mapper.modified).is_true()
144
145
        def is_false_after_fetch(mapper):
146
            mapper.create()
147
            mapper.fetch()
148
149
            expect(mapper.modified).is_false()
150
151
        def can_be_set_false(mapper):
152
            mapper.modified = False
153
154
            expect(mapper.modified).is_false()
155
156
        def can_be_set_true(mapper):
157
            mapper.modified = True
158
159
            expect(mapper.modified).is_true()
160