| Conditions | 63 |
| Total Lines | 170 |
| 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_template() 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,misplaced-comparison-constant,singleton-comparison |
||
| 11 | def describe_template(): |
||
| 12 | |||
| 13 | def it_supports_comparison(): |
||
| 14 | t1 = Template('abc', "A Thing") |
||
| 15 | t2 = Template('def') |
||
| 16 | t3 = Template('def', "Do This") |
||
| 17 | |||
| 18 | assert t1 != t2 |
||
| 19 | assert t2 == t3 |
||
| 20 | assert t1 < t3 |
||
| 21 | |||
| 22 | def describe_get_path(): |
||
| 23 | |||
| 24 | @patch('os.path.isfile', Mock(return_value=True)) |
||
| 25 | def it_returns_default_when_no_style(template): |
||
| 26 | expect(template.get_path()) == "abc/default.png" |
||
| 27 | |||
| 28 | @patch('os.path.isfile', Mock(return_value=True)) |
||
| 29 | def it_returns_alternate_when_style_provided(template): |
||
| 30 | expect(template.get_path('Custom')) == "abc/custom.png" |
||
| 31 | |||
| 32 | @patch('os.path.isfile', Mock(return_value=True)) |
||
| 33 | def it_returns_default_when_style_is_none(template): |
||
| 34 | expect(template.get_path(None)) == "abc/default.png" |
||
| 35 | |||
| 36 | @patch('os.path.isfile', Mock(return_value=False)) |
||
| 37 | def it_considers_urls_valid_styles(template): |
||
| 38 | url = "http://example.com" |
||
| 39 | path = "/tmp/a9b9f04336ce0181a08e774e01113b31" |
||
| 40 | expect(template.get_path(url)) == path |
||
| 41 | |||
| 42 | @patch('os.path.isfile', Mock(return_value=True)) |
||
| 43 | def it_caches_file_downloads(template): |
||
| 44 | url = "http://this/will/be/ignored" |
||
| 45 | path = "/tmp/d888710f0697650eb68fc9dcbb976d4c" |
||
| 46 | expect(template.get_path(url)) == path |
||
| 47 | |||
| 48 | def it_handles_bad_urls(template): |
||
| 49 | url = "http://invalid" |
||
| 50 | expect(template.get_path(url)) == None |
||
| 51 | |||
| 52 | def describe_path(): |
||
| 53 | |||
| 54 | def is_returned_when_file_exists(template): |
||
| 55 | template.root = "my_root" |
||
| 56 | |||
| 57 | with patch('os.path.isfile', Mock(return_value=True)): |
||
| 58 | path = template.path |
||
| 59 | |||
| 60 | assert "my_root/abc/default.png" == path |
||
| 61 | |||
| 62 | def is_none_when_no_file(template): |
||
| 63 | template.root = "my_root" |
||
| 64 | |||
| 65 | with patch('os.path.isfile', Mock(return_value=False)): |
||
| 66 | path = template.path |
||
| 67 | |||
| 68 | assert None is path |
||
| 69 | |||
| 70 | def describe_default_path(): |
||
| 71 | |||
| 72 | def is_based_on_lines(template): |
||
| 73 | assert "foo/bar" == template.default_path |
||
| 74 | |||
| 75 | def is_underscore_when_no_lines(template): |
||
| 76 | template.lines = [] |
||
| 77 | |||
| 78 | assert "_" == template.default_path |
||
| 79 | |||
| 80 | def describe_styles(): |
||
| 81 | |||
| 82 | @patch('os.listdir', Mock(return_value=[])) |
||
| 83 | def is_empty_when_no_alternate_images(template): |
||
| 84 | expect(template.styles) == [] |
||
| 85 | |||
| 86 | @patch('os.listdir', Mock(return_value=['foo.jpg', 'bar.png'])) |
||
| 87 | def is_filesnames_of_alternate_images(template): |
||
| 88 | expect(template.styles) == ['bar', 'foo'] |
||
| 89 | |||
| 90 | def describe_sample_path(): |
||
| 91 | |||
| 92 | def is_based_on_lines(template): |
||
| 93 | assert "foo/bar" == template.sample_path |
||
| 94 | |||
| 95 | def is_placeholder_when_no_lines(template): |
||
| 96 | template.lines = [] |
||
| 97 | |||
| 98 | assert "your-text/goes-here" == template.sample_path |
||
| 99 | |||
| 100 | def describe_match(): |
||
| 101 | |||
| 102 | def it_returns_none_when_no_match(template): |
||
| 103 | expect(template.match("")) == (0, None) |
||
| 104 | |||
| 105 | def it_returns_the_best_matching_result(template): |
||
| 106 | template.compile_regexes([r"(\w*)/?(abc)", r"(\w*)/?(def)"]) |
||
| 107 | |||
| 108 | expect(template.match("_/def")) == (0.42, "_/def") |
||
| 109 | |||
| 110 | def describe_validate_meta(): |
||
| 111 | |||
| 112 | def with_no_name(template): |
||
| 113 | template.name = None |
||
| 114 | |||
| 115 | assert False is template.validate_meta() |
||
| 116 | |||
| 117 | def with_no_default_image(template): |
||
| 118 | assert False is template.validate_meta() |
||
| 119 | |||
| 120 | def with_nonalphanumberic_name(template): |
||
| 121 | template.name = "'ABC' Meme" |
||
| 122 | |||
| 123 | assert False is template.validate_meta() |
||
| 124 | |||
| 125 | def describe_validate_link(): |
||
| 126 | |||
| 127 | def with_bad_link(template): |
||
| 128 | mock_response = Mock() |
||
| 129 | mock_response.status_code = 404 |
||
| 130 | |||
| 131 | with patch('requests.get', Mock(return_value=mock_response)): |
||
| 132 | template.link = "example.com/fake" |
||
| 133 | |||
| 134 | assert False is template.validate_link() |
||
| 135 | |||
| 136 | @patch('os.path.isfile', Mock(return_value=True)) |
||
| 137 | def with_cached_valid_link(template): |
||
| 138 | template.link = "example.com" |
||
| 139 | |||
| 140 | assert True is template.validate_link() |
||
| 141 | |||
| 142 | def describe_validate_size(): |
||
| 143 | |||
| 144 | @pytest.mark.parametrize('dimensions,valid', [ |
||
| 145 | ((Template.MIN_WIDTH, Template.MIN_HEIGHT), True), |
||
| 146 | ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT), False), |
||
| 147 | ((Template.MIN_WIDTH, Template.MIN_HEIGHT - 1), False), |
||
| 148 | ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False), |
||
| 149 | ]) |
||
| 150 | @patch('PIL.Image.open') |
||
| 151 | def with_various_dimenions(mock_open, template, dimensions, valid): |
||
| 152 | mock_img = Mock() |
||
| 153 | mock_img.size = dimensions |
||
| 154 | mock_open.return_value = mock_img |
||
| 155 | |||
| 156 | assert valid is template.validate_size() |
||
| 157 | |||
| 158 | def describe_validate_regexes(): |
||
| 159 | |||
| 160 | def with_missing_split(template): |
||
| 161 | template.compile_regexes([".*"]) |
||
| 162 | |||
| 163 | assert False is template.validate_regexes() |
||
| 164 | |||
| 165 | def describe_validate(): |
||
| 166 | |||
| 167 | def with_no_validators(template): |
||
| 168 | assert True is template.validate(validators=[]) |
||
| 169 | |||
| 170 | def with_all_passing_validators(template): |
||
| 171 | """Verify a template is valid if all validators pass.""" |
||
| 172 | mock_validators = [lambda: True] |
||
| 173 | |||
| 174 | assert True is template.validate(validators=mock_validators) |
||
| 175 | |||
| 176 | def with_one_failing_validator(template): |
||
| 177 | """Verify a template is invalid if any validators fail.""" |
||
| 178 | mock_validators = [lambda: False] |
||
| 179 | |||
| 180 | assert False is template.validate(validators=mock_validators) |
||
| 181 |