Completed
Pull Request — master (#312)
by
unknown
10:10
created

describe_display()   F

Complexity

Conditions 13

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
c 3
b 0
f 0
dl 0
loc 49
rs 2.5507

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   Complexity   

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
24
    request_image = Mock(url="it's a path")
25
    request_image.headers.get = Mock(return_value="(not a browser)")
26
27
    request_share = Mock(url="it's a path?alt=style&share=true")
28
    request_share.headers.get = Mock(return_value="*/*")
29
30
    @patch('memegen.routes._utils.request', request_html)
31
    def it_returns_html_for_browsers(app):
32
33
        with app.test_request_context():
34
            html = display("my_title", "my_path", raw=True)
35
36
        print(html)
37
        assert "<title>my_title</title>" in html
38
        assert 'url("it\'s a path?alt=style")' in html
39
        assert "ga('create', 'my_tid', 'auto');" in html
40
41
    @patch('memegen.routes._utils.send_file')
42
    @patch('memegen.routes._utils.request', request_image)
43
    def it_returns_an_image_otherwise(mock_send_file):
44
45
        display("my_title", "my_path")
46
47
        expect(mock_send_file.mock_calls) == [
48
            call("my_path", mimetype='image/jpeg'),
49
        ]
50
51
52
    @patch('memegen.routes._utils.request', request_share)
53
    def it_returns_html_when_sharing(app):
54
55
        with app.test_request_context():
56
            html = display("my_title", "my_path", share=True, raw=True)
57
58
        print(html)
59
        assert "<title>my_title</title>" in html
60
        assert 'url("it\'s a path?alt=style&share=true")' in html
61
        assert "ga('create', 'my_tid', 'auto');" in html
62