Completed
Pull Request — master (#244)
by Jace
07:52 queued 06:28
created

describe_get()   F

Complexity

Conditions 68

Size

Total Lines 203

Duplication

Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 0 Features 0
Metric Value
cc 68
c 16
b 0
f 0
dl 0
loc 203
rs 2

31 Methods

Rating   Name   Duplication   Size   Complexity  
A when_missing_dashes() 0 5 3
A it_redirects_on_unknown_fonts() 0 6 1
A it_keeps_font_after_redirect() 0 6 1
B with_nominal_text() 0 10 5
A with_only_1_line() 0 5 3
A when_missing() 0 9 2
A when_existing() 0 8 1
A when_jpeg_extension_without_text() 0 5 3
A when_too_much_text_for_a_filename() 0 9 3
A when_unknown_template() 0 5 3
A when_jpg() 0 5 3
A when_no_text() 0 5 3
A it_keeps_alt_after_text_redirect() 0 5 3
A it_keeps_alt_after_template_redirect() 0 5 3
A it_redirects_to_lose_alt_when_bad_url() 0 7 1
A when_jpeg_extension_with_text() 0 5 3
A describe_hidden() 0 7 4
F describe_redirects() 0 35 16
F describe_custom_style() 0 64 20
A it_returns_an_error_with_non_image_urls() 0 5 1
A it_redirects_to_lose_alt_when_default_style() 0 5 3
A it_redirects_to_lose_alt_when_unknown_url() 0 7 1
A describe_custom_font() 0 21 4
B describe_errors() 0 18 7
A when_provided() 0 5 1
A describe_latest() 0 20 4
A when_url() 0 6 1
A with_lots_of_text() 0 8 3
A when_aliased_template() 0 5 3
A it_redirects_to_lose_alt_when_unknown_style() 0 5 3
F describe_visible() 0 27 12

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,misplaced-comparison-constant,expression-not-assigned
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
2
3
import os
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 .conftest import load
9
10
TESTS = os.path.dirname(__file__)
11
ROOT = os.path.dirname(TESTS)
12
IMAGES = os.path.join(ROOT, 'data', 'images')
13
14
15
def describe_get():
16
17
    def describe_visible():
18
19
        def with_nominal_text(client):
20
            path = os.path.join(IMAGES, 'iw', 'hello', 'world' + '.img')
21
            if os.path.exists(path):
22
                os.remove(path)
23
24
            response = client.get("/iw/hello/world.jpg")
25
26
            assert 200 == response.status_code
27
            assert 'image/jpeg' == response.mimetype
28
            assert os.path.isfile(path)
29
30
        def with_only_1_line(client):
31
            response = client.get("/iw/hello.jpg")
32
33
            assert 200 == response.status_code
34
            assert 'image/jpeg' == response.mimetype
35
36
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
37
        def with_lots_of_text(client):
38
            top = "-".join(["hello"] * 20)
39
            bottom = "-".join(["world"] * 20)
40
            response = client.get("/iw/" + top + "/" + bottom + ".jpg")
41
42
            assert 200 == response.status_code
43
            assert 'image/jpeg' == response.mimetype
44
45
    def describe_hidden():
46
47
        def when_jpg(client):
48
            response = client.get("/_aXcJaGVsbG8vd29ybGQJ.jpg")
49
50
            assert 200 == response.status_code
51
            assert 'image/jpeg' == response.mimetype
52
53
    def describe_custom_style():
54
55
        def when_provided(client):
56
            response = client.get("/sad-biden/hello.jpg?alt=scowl")
57
58
            assert 200 == response.status_code
59
            assert 'image/jpeg' == response.mimetype
60
61
        def it_redirects_to_lose_alt_when_default_style(client):
62
            response = client.get("/sad-biden/hello.jpg?alt=default")
63
64
            assert 302 == response.status_code
65
            assert '<a href="/sad-biden/hello.jpg">' in \
66
                load(response, as_json=False)
67
68
        def it_redirects_to_lose_alt_when_unknown_style(client):
69
            response = client.get("/sad-biden/hello.jpg?alt=__unknown__")
70
71
            assert 302 == response.status_code
72
            assert '<a href="/sad-biden/hello.jpg">' in \
73
                load(response, as_json=False)
74
75
        def it_keeps_alt_after_template_redirect(client):
76
            response = client.get("/sad-joe/hello.jpg?alt=scowl")
77
78
            assert 302 == response.status_code
79
            assert '<a href="/sad-biden/hello.jpg?alt=scowl">' in \
80
                load(response, as_json=False)
81
82
        def it_keeps_alt_after_text_redirect(client):
83
            response = client.get("/sad-biden.jpg?alt=scowl")
84
85
            assert 302 == response.status_code
86
            assert '-vote.jpg?alt=scowl">' in \
87
                load(response, as_json=False)
88
89
        def when_url(client):
90
            url = "http://www.gstatic.com/webp/gallery/1.jpg"
91
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
92
93
            expect(response.status_code) == 200
94
            expect(response.mimetype) == 'image/jpeg'
95
96
        def it_returns_an_error_with_non_image_urls(client):
97
            url = "http://example.com"
98
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
99
100
            expect(response.status_code) == 415
101
102
        def it_redirects_to_lose_alt_when_unknown_url(client):
103
            url = "http://example.com/not/a/real/image.jpg"
104
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
105
106
            expect(response.status_code) == 302
107
            expect(load(response, as_json=False)).contains(
108
                '<a href="/sad-biden/hello.jpg">')
109
110
        def it_redirects_to_lose_alt_when_bad_url(client):
111
            url = "http:invalid"
112
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
113
114
            expect(response.status_code) == 302
115
            expect(load(response, as_json=False)).contains(
116
                '<a href="/sad-biden/hello.jpg">')
117
118
    def describe_custom_font():
119
120
        def when_provided(client):
121
            response = client.get("/iw/hello.jpg?font=impact")
122
123
            expect(response.status_code) == 200
124
            expect(response.mimetype) == 'image/jpeg'
125
126
        def it_redirects_on_unknown_fonts(client):
127
            response = client.get("/iw/hello.jpg?font=__unknown__")
128
129
            expect(response.status_code) == 302
130
            expect(load(response, as_json=False)).contains(
131
                '<a href="/iw/hello.jpg">')
132
133
        def it_keeps_font_after_redirect(client):
134
            response = client.get("/iw/what%3F.jpg?font=impact")
135
136
            expect(response.status_code) == 302
137
            expect(load(response, as_json=False)).contains(
138
                '<a href="/iw/what~q.jpg?font=impact">')
139
140
    def describe_latest():
141
142
        def when_existing(client):
143
            client.get("/iw/my-first-meme.jpg")
144
145
            response = client.get("/latest.jpg")
146
147
            expect(response.status_code) == 302
148
            expect(load(response, as_json=False)).contains(
149
                '<a href="http://localhost/iw/my-first-meme.jpg">')
150
151
        def when_missing(client):
152
            with open(os.path.join(IMAGES, 'cache.yml'), 'w') as cache:
153
                cache.write("items: []")
154
155
            response = client.get("/latest.jpg")
156
157
            expect(response.status_code) == 302
158
            expect(load(response, as_json=False)).contains(
159
                '<a href="http://localhost/custom/your-meme/goes-here.jpg'
160
                '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/'
161
                'master/memegen/static/images/missing.png">')
162
163
    def describe_redirects():
164
165
        def when_missing_dashes(client):
166
            response = client.get("/iw/HelloThere_World/How-areYOU.jpg")
167
168
            assert 302 == response.status_code
169
            assert '<a href="/iw/hello-there-world/how-are-you.jpg">' in \
170
                load(response, as_json=False)
171
172
        def when_no_text(client):
173
            response = client.get("/live.jpg")
174
175
            assert 302 == response.status_code
176
            assert '<a href="/live/_/do-it-live!.jpg">' in \
177
                load(response, as_json=False)
178
179
        def when_aliased_template(client):
180
            response = client.get("/insanity-wolf/hello/world.jpg")
181
182
            assert 302 == response.status_code
183
            assert '<a href="/iw/hello/world.jpg">' in \
184
                load(response, as_json=False)
185
186
        def when_jpeg_extension_without_text(client):
187
            response = client.get("/iw.jpeg")
188
189
            assert 302 == response.status_code
190
            assert '<a href="/iw.jpg">' in \
191
                load(response, as_json=False)
192
193
        def when_jpeg_extension_with_text(client):
194
            response = client.get("/iw/hello/world.jpeg")
195
196
            assert 302 == response.status_code
197
            assert '<a href="/iw/hello/world.jpg">' in \
198
                load(response, as_json=False)
199
200
    def describe_errors():
201
202
        def when_unknown_template(client):
203
            response = client.get("/make/sudo/give.me.jpg")
204
205
            assert 200 == response.status_code
206
            assert 'image/jpeg' == response.mimetype
207
            # unit tests ensure this is a placeholder image
208
209
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
210
        def when_too_much_text_for_a_filename(client):
211
            top = "hello"
212
            bottom = "-".join(["world"] * 50)
213
            response = client.get("/iw/" + top + "/" + bottom + ".jpg")
214
215
            assert 414 == response.status_code
216
            assert {
217
                'message': "Filename too long."
218
            } == load(response)
219