Completed
Push — master ( 079863...3f9dd1 )
by Jace
03:11
created

describe_template()   F

Complexity

Conditions 54

Size

Total Lines 137

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 54
dl 0
loc 137
rs 2

29 Methods

Rating   Name   Duplication   Size   Complexity  
A it_returns_alternate_when_style_provided() 0 3 1
A is_none_when_no_file() 0 7 3
B describe_path() 0 17 7
A it_supports_comparison() 0 8 4
A it_returns_default_when_no_style() 0 3 1
A is_underscore_when_no_lines() 0 4 2
A it_returns_default_when_style_is_none() 0 3 1
A is_returned_when_file_exists() 0 7 3
B describe_default_path() 0 9 5
A describe_get_path() 0 13 4
A is_placeholder_when_no_lines() 0 4 2
A with_no_default_image() 0 2 2
B describe_sample_path() 0 9 5
A describe_styles() 0 9 3
A describe_validate_size() 0 15 3
A with_no_name() 0 4 2
A with_cached_valid_link() 0 5 2
B describe_validate_meta() 0 14 7
A with_nonalphanumberic_name() 0 4 2
A with_bad_link() 0 8 3
A with_various_dimenions() 0 13 2
C describe_validate() 0 16 9
B describe_validate_link() 0 16 6
A with_one_failing_validator() 0 5 3
A is_empty_when_no_alternate_images() 0 3 1
A with_no_validators() 0 2 2
A is_filesnames_of_alternate_images() 0 3 1
A with_all_passing_validators() 0 5 3
A is_based_on_lines() 0 2 2

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 describe_template() 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=unused-variable
2
# pylint: disable=expression-not-assigned
3
# pylint: disable=misplaced-comparison-constant
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
4
5
from unittest.mock import patch, Mock
6
7
import pytest
8
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...
9
10
from memegen.domain import Template
11
12
13
def describe_template():
14
15
    def it_supports_comparison():
16
        t1 = Template('abc', "A Thing")
17
        t2 = Template('def')
18
        t3 = Template('def', "Do This")
19
20
        assert t1 != t2
21
        assert t2 == t3
22
        assert t1 < t3
23
24
    def describe_get_path():
25
26
        @patch('os.path.isfile', Mock(return_value=True))
27
        def it_returns_default_when_no_style(template):
28
            expect(template.get_path()) == "abc/default.png"
29
30
        @patch('os.path.isfile', Mock(return_value=True))
31
        def it_returns_alternate_when_style_provided(template):
32
            expect(template.get_path('Custom')) == "abc/custom.png"
33
34
        @patch('os.path.isfile', Mock(return_value=True))
35
        def it_returns_default_when_style_is_none(template):
36
            expect(template.get_path(None)) == "abc/default.png"
37
38
    def describe_path():
39
40
        def is_returned_when_file_exists(template):
41
            template.root = "my_root"
42
43
            with patch('os.path.isfile', Mock(return_value=True)):
44
                path = template.path
45
46
            assert "my_root/abc/default.png" == path
47
48
        def is_none_when_no_file(template):
49
            template.root = "my_root"
50
51
            with patch('os.path.isfile', Mock(return_value=False)):
52
                path = template.path
53
54
            assert None is path
55
56
    def describe_default_path():
57
58
        def is_based_on_lines(template):
59
            assert "foo/bar" == template.default_path
60
61
        def is_underscore_when_no_lines(template):
62
            template.lines = []
63
64
            assert "_" == template.default_path
65
66
    def describe_styles():
67
68
        @patch('os.listdir', Mock(return_value=[]))
69
        def is_empty_when_no_alternate_images(template):
70
            expect(template.styles) == []
71
72
        @patch('os.listdir', Mock(return_value=['foo.jpg', 'bar.png']))
73
        def is_filesnames_of_alternate_images(template):
74
            expect(template.styles) == ['bar', 'foo']
75
76
    def describe_sample_path():
77
78
        def is_based_on_lines(template):
79
            assert "foo/bar" == template.sample_path
80
81
        def is_placeholder_when_no_lines(template):
82
            template.lines = []
83
84
            assert "your-text/goes-here" == template.sample_path
85
86
    def describe_validate_meta():
87
88
        def with_no_name(template):
89
            template.name = None
90
91
            assert False is template.validate_meta()
92
93
        def with_no_default_image(template):
94
            assert False is template.validate_meta()
95
96
        def with_nonalphanumberic_name(template):
97
            template.name = "'ABC' Meme"
98
99
            assert False is template.validate_meta()
100
101
    def describe_validate_link():
102
103
        def with_bad_link(template):
104
            mock_response = Mock()
105
            mock_response.status_code = 404
106
107
            with patch('requests.get', Mock(return_value=mock_response)):
108
                template.link = "example.com/fake"
109
110
                assert False is template.validate_link()
111
112
        @patch('os.path.isfile', Mock(return_value=True))
113
        def with_cached_valid_link(template):
114
            template.link = "example.com"
115
116
            assert True is template.validate_link()
117
118
    def describe_validate_size():
119
120
        @pytest.mark.parametrize('dimensions,valid', [
121
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT), True),
122
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT), False),
123
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT - 1), False),
124
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False),
125
        ])
126
        @patch('PIL.Image.open')
127
        def with_various_dimenions(mock_open, template, dimensions, valid):
128
            mock_img = Mock()
129
            mock_img.size = dimensions
130
            mock_open.return_value = mock_img
131
132
            assert valid is template.validate_size()
133
134
    def describe_validate():
135
136
        def with_no_validators(template):
137
            assert True is template.validate(validators=[])
138
139
        def with_all_passing_validators(template):
140
            """Verify a template is valid if all validators pass."""
141
            mock_validators = [lambda: True]
142
143
            assert True is template.validate(validators=mock_validators)
144
145
        def with_one_failing_validator(template):
146
            """Verify a template is invalid if any validators fail."""
147
            mock_validators = [lambda: False]
148
149
            assert False is template.validate(validators=mock_validators)
150