| Conditions | 63 |
| Total Lines | 146 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
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=misplaced-comparison-constant |
||
| 14 | def describe_get(): |
||
| 15 | |||
| 16 | def describe_visible(): |
||
| 17 | |||
| 18 | def basic(client): |
||
| 19 | path = os.path.join(IMAGES, 'iw', 'hello', 'world.jpg') |
||
| 20 | if os.path.exists(path): |
||
| 21 | os.remove(path) |
||
| 22 | |||
| 23 | response = client.get("/iw/hello/world.jpg") |
||
| 24 | |||
| 25 | assert 200 == response.status_code |
||
| 26 | assert 'image/jpeg' == response.mimetype |
||
| 27 | assert os.path.isfile(path) |
||
| 28 | |||
| 29 | def with_only_1_line(client): |
||
| 30 | response = client.get("/iw/hello.jpg") |
||
| 31 | |||
| 32 | assert 200 == response.status_code |
||
| 33 | assert 'image/jpeg' == response.mimetype |
||
| 34 | |||
| 35 | def with_lots_of_text(client): |
||
| 36 | top = "-".join(["hello"] * 20) |
||
| 37 | bottom = "-".join(["world"] * 20) |
||
| 38 | response = client.get("/iw/" + top + "/" + bottom + ".jpg") |
||
| 39 | |||
| 40 | assert 200 == response.status_code |
||
| 41 | assert 'image/jpeg' == response.mimetype |
||
| 42 | |||
| 43 | def describe_hidden(): |
||
| 44 | |||
| 45 | def when_jpg(client): |
||
| 46 | response = client.get("/_aXcJaGVsbG8vd29ybGQJ.jpg") |
||
| 47 | |||
| 48 | assert 200 == response.status_code |
||
| 49 | assert 'image/jpeg' == response.mimetype |
||
| 50 | |||
| 51 | def describe_alt(): |
||
| 52 | |||
| 53 | def when_valid(client): |
||
| 54 | response = client.get("/sad-biden/hello.jpg?alt=scowl") |
||
| 55 | |||
| 56 | assert 200 == response.status_code |
||
| 57 | assert 'image/jpeg' == response.mimetype |
||
| 58 | |||
| 59 | def it_redirects_to_lose_alt_when_default(client): |
||
| 60 | response = client.get("/sad-biden/hello.jpg?alt=default") |
||
| 61 | |||
| 62 | assert 302 == response.status_code |
||
| 63 | assert '<a href="/sad-biden/hello.jpg">' in \ |
||
| 64 | load(response, as_json=False) |
||
| 65 | |||
| 66 | def it_redirects_to_lose_alt_when_unknown(client): |
||
| 67 | response = client.get("/sad-biden/hello.jpg?alt=__unknown__") |
||
| 68 | |||
| 69 | assert 302 == response.status_code |
||
| 70 | assert '<a href="/sad-biden/hello.jpg">' in \ |
||
| 71 | load(response, as_json=False) |
||
| 72 | |||
| 73 | def it_keeps_alt_after_template_redirect(client): |
||
| 74 | response = client.get("/sad-joe/hello.jpg?alt=scowl") |
||
| 75 | |||
| 76 | assert 302 == response.status_code |
||
| 77 | assert '<a href="/sad-biden/hello.jpg?alt=scowl">' in \ |
||
| 78 | load(response, as_json=False) |
||
| 79 | |||
| 80 | def it_keeps_alt_after_text_redirect(client): |
||
| 81 | response = client.get("/sad-biden.jpg?alt=scowl") |
||
| 82 | |||
| 83 | assert 302 == response.status_code |
||
| 84 | assert '-vote.jpg?style=scowl">' in \ |
||
| 85 | load(response, as_json=False) |
||
| 86 | |||
| 87 | def describe_latest(): |
||
| 88 | |||
| 89 | def when_existing(client): |
||
| 90 | open(LATEST, 'w').close() # force the file to exist |
||
| 91 | response = client.get("/latest.jpg") |
||
| 92 | |||
| 93 | assert 200 == response.status_code |
||
| 94 | assert 'image/jpeg' == response.mimetype |
||
| 95 | |||
| 96 | def when_missing(client): |
||
| 97 | try: |
||
| 98 | os.remove(LATEST) |
||
| 99 | except FileNotFoundError: |
||
| 100 | pass |
||
| 101 | |||
| 102 | response = client.get("/latest.jpg") |
||
| 103 | |||
| 104 | assert 200 == response.status_code |
||
| 105 | assert 'image/png' == response.mimetype |
||
| 106 | |||
| 107 | def describe_redirects(): |
||
| 108 | |||
| 109 | def when_missing_dashes(client): |
||
| 110 | response = client.get("/iw/HelloThere_World/How-areYOU.jpg") |
||
| 111 | |||
| 112 | assert 302 == response.status_code |
||
| 113 | assert '<a href="/iw/hello-there-world/how-are-you.jpg">' in \ |
||
| 114 | load(response, as_json=False) |
||
| 115 | |||
| 116 | def when_no_text(client): |
||
| 117 | response = client.get("/live.jpg") |
||
| 118 | |||
| 119 | assert 302 == response.status_code |
||
| 120 | assert '<a href="/live/_/do-it-live!.jpg">' in \ |
||
| 121 | load(response, as_json=False) |
||
| 122 | |||
| 123 | def when_aliased_template(client): |
||
| 124 | response = client.get("/insanity-wolf/hello/world.jpg") |
||
| 125 | |||
| 126 | assert 302 == response.status_code |
||
| 127 | assert '<a href="/iw/hello/world.jpg">' in \ |
||
| 128 | load(response, as_json=False) |
||
| 129 | |||
| 130 | def when_jpeg_extension_without_text(client): |
||
| 131 | response = client.get("/iw.jpeg") |
||
| 132 | |||
| 133 | assert 302 == response.status_code |
||
| 134 | assert '<a href="/iw.jpg">' in \ |
||
| 135 | load(response, as_json=False) |
||
| 136 | |||
| 137 | def when_jpeg_extension_with_text(client): |
||
| 138 | response = client.get("/iw/hello/world.jpeg") |
||
| 139 | |||
| 140 | assert 302 == response.status_code |
||
| 141 | assert '<a href="/iw/hello/world.jpg">' in \ |
||
| 142 | load(response, as_json=False) |
||
| 143 | |||
| 144 | def describe_errors(): |
||
| 145 | |||
| 146 | def when_unknown_template(client): |
||
| 147 | response = client.get("/make/sudo/give.me.jpg") |
||
| 148 | |||
| 149 | assert 404 == response.status_code |
||
| 150 | |||
| 151 | def when_too_much_text_for_a_filename(client): |
||
| 152 | top = "hello" |
||
| 153 | bottom = "-".join(["world"] * 50) |
||
| 154 | response = client.get("/iw/" + top + "/" + bottom + ".jpg") |
||
| 155 | |||
| 156 | assert 414 == response.status_code |
||
| 157 | assert { |
||
| 158 | 'message': "Filename too long." |
||
| 159 | } == load(response) |
||
| 160 |