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