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

describe_custom_style()   F

Complexity

Conditions 21

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 21
c 4
b 0
f 0
dl 0
loc 71
rs 2.5491

10 Methods

Rating   Name   Duplication   Size   Complexity  
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_default_style() 0 5 3
A when_provided() 0 5 1
A it_redirects_to_lose_alt_when_unknown_style() 0 5 3
A it_returns_an_error_with_non_image_urls() 0 5 1
A when_url() 0 6 1
A it_handles_int32_pixels() 0 6 1
A it_redirects_to_lose_alt_when_bad_url() 0 7 1
A it_redirects_to_lose_alt_when_unknown_url() 0 7 1

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_custom_style() 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,unused-argument,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_handles_int32_pixels(client):
103
            url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/201692816359570.jpeg"
104
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
105
106
            expect(response.status_code) == 200
107
            expect(response.mimetype) == 'image/jpeg'
108
109
        def it_redirects_to_lose_alt_when_unknown_url(client):
110
            url = "http://example.com/not/a/real/image.jpg"
111
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
112
113
            expect(response.status_code) == 302
114
            expect(load(response, as_json=False)).contains(
115
                '<a href="/sad-biden/hello.jpg">')
116
117
        def it_redirects_to_lose_alt_when_bad_url(client):
118
            url = "http:invalid"
119
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
120
121
            expect(response.status_code) == 302
122
            expect(load(response, as_json=False)).contains(
123
                '<a href="/sad-biden/hello.jpg">')
124
125
    def describe_custom_font():
126
127
        def when_provided(client):
128
            response = client.get("/iw/hello.jpg?font=impact")
129
130
            expect(response.status_code) == 200
131
            expect(response.mimetype) == 'image/jpeg'
132
133
        def it_redirects_on_unknown_fonts(client):
134
            response = client.get("/iw/hello.jpg?font=__unknown__")
135
136
            expect(response.status_code) == 302
137
            expect(load(response, as_json=False)).contains(
138
                '<a href="/iw/hello.jpg">')
139
140
        def it_keeps_font_after_redirect(client):
141
            response = client.get("/iw/what%3F.jpg?font=impact")
142
143
            expect(response.status_code) == 302
144
            expect(load(response, as_json=False)).contains(
145
                '<a href="/iw/what~q.jpg?font=impact">')
146
147
    def describe_latest():
148
149
        @pytest.fixture()
150
        def clear_cache():
151
            with open(os.path.join(IMAGES, 'cache.yml'), 'w') as cache:
152
                cache.write("items: []")
153
154
        def when_existing(client, clear_cache):
155
            client.get("/iw/my-first-meme.jpg")
156
157
            response = client.get("/latest.jpg")
158
159
            expect(response.status_code) == 302
160
            expect(load(response, as_json=False)).contains(
161
                '<a href="http://localhost/iw/my-first-meme.jpg">')
162
163
        def when_missing(client, clear_cache):
164
            response = client.get("/latest.jpg")
165
166
            expect(response.status_code) == 302
167
            expect(load(response, as_json=False)).contains(
168
                '<a href="http://localhost/custom/your-meme/goes-here.jpg'
169
                '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/'
170
                'master/memegen/static/images/missing.png">')
171
172
    def describe_redirects():
173
174
        def when_missing_dashes(client):
175
            response = client.get("/iw/HelloThere_World/How-areYOU.jpg")
176
177
            assert 302 == response.status_code
178
            assert '<a href="/iw/hello-there-world/how-are-you.jpg">' in \
179
                load(response, as_json=False)
180
181
        def when_no_text(client):
182
            response = client.get("/live.jpg")
183
184
            assert 302 == response.status_code
185
            assert '<a href="/live/_/do-it-live!.jpg">' in \
186
                load(response, as_json=False)
187
188
        def when_aliased_template(client):
189
            response = client.get("/insanity-wolf/hello/world.jpg")
190
191
            assert 302 == response.status_code
192
            assert '<a href="/iw/hello/world.jpg">' in \
193
                load(response, as_json=False)
194
195
        def when_jpeg_extension_without_text(client):
196
            response = client.get("/iw.jpeg")
197
198
            assert 302 == response.status_code
199
            assert '<a href="/iw.jpg">' in \
200
                load(response, as_json=False)
201
202
        def when_jpeg_extension_with_text(client):
203
            response = client.get("/iw/hello/world.jpeg")
204
205
            assert 302 == response.status_code
206
            assert '<a href="/iw/hello/world.jpg">' in \
207
                load(response, as_json=False)
208
209
    def describe_errors():
210
211
        def when_unknown_template(client):
212
            response = client.get("/make/sudo/give.me.jpg")
213
214
            assert 200 == response.status_code
215
            assert 'image/jpeg' == response.mimetype
216
            # unit tests ensure this is a placeholder image
217
218
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
219
        def when_too_much_text_for_a_filename(client):
220
            top = "hello"
221
            bottom = "-".join(["world"] * 50)
222
            response = client.get("/iw/" + top + "/" + bottom + ".jpg")
223
224
            assert 414 == response.status_code
225
            assert {
226
                'message': "Filename too long."
227
            } == load(response)
228