Completed
Pull Request — master (#132)
by Jace
01:42
created

describe_get()   F

Complexity

Conditions 22

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 22
dl 0
loc 51
rs 2.9306

7 Methods

Rating   Name   Duplication   Size   Complexity  
A when_default_text() 0 11 3
A it_returns_list_when_no_key() 0 7 4
A when_dashes_in_key() 0 4 2
A when_no_default_text() 0 6 3
A it_redirects_when_key_is_an_alias() 0 5 3
A it_redirects_when_text_is_provided() 0 5 3
A when_alternate_sytles_available() 0 5 3

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_get() 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=misplaced-comparison-constant
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
3
4
from .conftest import load
5
6
7
def describe_get():
8
9
    def when_default_text(client):
10
        response = client.get("/templates/iw")
11
12
        assert 200 == response.status_code
13
        assert dict(
14
            name="Insanity Wolf",
15
            description="http://knowyourmeme.com/memes/insanity-wolf",
16
            aliases=['insanity', 'insanity-wolf', 'iw'],
17
            styles=[],
18
            example="http://localhost/iw/does-testing/in-production",
19
        ) == load(response)
20
21
    def when_no_default_text(client):
22
        response = client.get("/templates/keanu")
23
24
        assert 200 == response.status_code
25
        assert "http://localhost/keanu/your-text/goes-here" == \
26
            load(response)['example']
27
28
    def when_alternate_sytles_available(client):
29
        response = client.get("/templates/sad-biden")
30
31
        assert 200 == response.status_code
32
        assert ['down', 'scowl', 'window'] == load(response)['styles']
33
34
    def when_dashes_in_key(client):
35
        response = client.get("/templates/awkward-awesome")
36
37
        assert 200 == response.status_code
38
39
    def it_returns_list_when_no_key(client):
40
        response = client.get("/templates/")
41
42
        assert 200 == response.status_code
43
        data = load(response)
44
        assert "http://localhost/templates/iw" == data['Insanity Wolf']
45
        assert len(data) >= 20  # there should be many memes
46
47
    def it_redirects_when_text_is_provided(client):
48
        response = client.get("/templates/iw/top/bottom")
49
50
        assert 302 == response.status_code
51
        assert '<a href="/iw/top/bottom">' in load(response, as_json=False)
52
53
    def it_redirects_when_key_is_an_alias(client):
54
        response = client.get("/templates/insanity-wolf")
55
56
        assert 302 == response.status_code
57
        assert '<a href="/templates/iw">' in load(response, as_json=False)
58
59
60
def describe_post():
61
62
    def it_returns_an_error(client):
63
        response = client.post("/templates/")
64
65
        assert 403 == response.status_code
66
        assert dict(
67
            message="http://github.com/jacebrowning/memegen/blob/master/CONTRIBUTING.md"
68
        ) == load(response)
69