Completed
Push — master ( 2a5ab9...12cd9a )
by Jace
14s
created

describe_display()   F

Complexity

Conditions 13

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 13
c 4
b 0
f 0
dl 0
loc 54
rs 3.5512

3 Methods

Rating   Name   Duplication   Size   Complexity  
A it_returns_an_image_otherwise() 0 7 1
B it_returns_html_when_sharing() 0 10 5
B it_returns_html_for_browsers() 0 10 5

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_display() 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,expression-not-assigned
2
3
from unittest.mock import patch, call, Mock
4
5
import pytest
6
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...
7
8
from memegen.app import create_app
9
from memegen.settings import get_config
10
from memegen.routes._utils import display
11
12
13
def describe_display():
14
15
    @pytest.fixture
16
    def app():
17
        app = create_app(get_config('test'))
18
        app.config['GOOGLE_ANALYTICS_TID'] = 'my_tid'
19
        return app
20
21
    request_html = Mock(url="it's a path?alt=style")
22
    request_html.headers.get = Mock(return_value="text/html")
23
    request_html.base_url = "it's a path"
24
    request_html.args = {'alt': ['style']}
25
26
    request_image = Mock(url="it's a path")
27
    request_image.headers.get = Mock(return_value="(not a browser)")
28
    request_image.base_url = "it's a path"
29
    request_image.args = {}
30
31
    request_share = Mock(url="it's a path?alt=style&share=true")
32
    request_share.headers.get = Mock(return_value="*/*")
33
    request_share.base_url = "it's a path"
34
    request_share.args = {'alt': ['style'], 'share': ['true']}
35
36
    @patch('memegen.routes._utils.request', request_html)
37
    def it_returns_html_for_browsers(app):
38
39
        with app.test_request_context():
40
            html = display("my_title", "my_path", raw=True)
41
42
        print(html)
43
        assert "<title>my_title</title>" in html
44
        assert 'url("it\'s a path?alt=style")' in html
45
        assert "ga('create', 'my_tid', 'auto');" in html
46
47
    @patch('memegen.routes._utils.send_file')
48
    @patch('memegen.routes._utils.request', request_image)
49
    def it_returns_an_image_otherwise(mock_send_file):
50
51
        display("my_title", "my_path")
52
53
        expect(mock_send_file.mock_calls) == [
54
            call("my_path", mimetype='image/jpeg'),
55
        ]
56
57
    @patch('memegen.routes._utils.request', request_share)
58
    def it_returns_html_when_sharing(app):
59
60
        with app.test_request_context():
61
            html = display("my_title", "my_path", share=True, raw=True)
62
63
        print(html)
64
        assert "<title>my_title</title>" in html
65
        assert 'url("it\'s a path?alt=style")' in html
66
        assert "ga('create', 'my_tid', 'auto');" in html
67