| Total Complexity | 40 |
| Total Lines | 154 |
| Duplicated Lines | 17.53 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TestRenderVariations 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 | import hashlib |
||
| 14 | @pytest.mark.django_db |
||
| 15 | class TestRenderVariations: |
||
| 16 | |||
| 17 | @pytest.fixture(autouse=True) |
||
| 18 | def _swap_concurrent_executor(self, monkeypatch): |
||
| 19 | """Use ThreadPoolExecutor for coverage reports.""" |
||
| 20 | monkeypatch.setattr( |
||
| 21 | 'concurrent.futures.ProcessPoolExecutor', |
||
| 22 | ThreadPoolExecutor, |
||
| 23 | ) |
||
| 24 | |||
| 25 | def test_no_options(self, image_upload_file): |
||
| 26 | obj = ThumbnailModel.objects.create( |
||
| 27 | image=image_upload_file |
||
| 28 | ) |
||
| 29 | file_path = obj.image.thumbnail.path |
||
| 30 | obj.image.delete_variations() |
||
| 31 | call_command( |
||
| 32 | 'rendervariations', |
||
| 33 | 'tests.ThumbnailModel.image' |
||
| 34 | ) |
||
| 35 | assert os.path.exists(file_path) |
||
| 36 | |||
| 37 | def test_multiprocessing(self, image_upload_file): |
||
| 38 | objs = [ |
||
| 39 | ThumbnailModel.objects.create( |
||
| 40 | image=image_upload_file |
||
| 41 | ) |
||
| 42 | for _ in range(100) |
||
| 43 | ] |
||
| 44 | file_names = [ |
||
| 45 | obj.image.thumbnail.path |
||
| 46 | for obj in objs |
||
| 47 | ] |
||
| 48 | for obj in objs: |
||
| 49 | obj.image.delete_variations() |
||
| 50 | assert not any([os.path.exists(f) for f in file_names]) |
||
| 51 | call_command( |
||
| 52 | 'rendervariations', |
||
| 53 | 'tests.ThumbnailModel.image' |
||
| 54 | ) |
||
| 55 | assert any([os.path.exists(f) for f in file_names]) |
||
| 56 | |||
| 57 | View Code Duplication | def test_no_replace(self, image_upload_file): |
|
|
|
|||
| 58 | obj = ThumbnailModel.objects.create(image=image_upload_file) |
||
| 59 | file_path = obj.image.thumbnail.path |
||
| 60 | assert os.path.exists(file_path) |
||
| 61 | before = os.path.getmtime(file_path) |
||
| 62 | time.sleep(1) |
||
| 63 | call_command( |
||
| 64 | 'rendervariations', |
||
| 65 | 'tests.ThumbnailModel.image', |
||
| 66 | ) |
||
| 67 | assert os.path.exists(file_path) |
||
| 68 | after = os.path.getmtime(file_path) |
||
| 69 | assert before == after |
||
| 70 | |||
| 71 | View Code Duplication | def test_replace(self, image_upload_file): |
|
| 72 | obj = ThumbnailModel.objects.create(image=image_upload_file) |
||
| 73 | file_path = obj.image.thumbnail.path |
||
| 74 | assert os.path.exists(file_path) |
||
| 75 | before = os.path.getmtime(file_path) |
||
| 76 | time.sleep(1) |
||
| 77 | call_command( |
||
| 78 | 'rendervariations', |
||
| 79 | 'tests.ThumbnailModel.image', |
||
| 80 | replace=True |
||
| 81 | ) |
||
| 82 | assert os.path.exists(file_path) |
||
| 83 | after = os.path.getmtime(file_path) |
||
| 84 | assert before != after |
||
| 85 | |||
| 86 | def test_ignore_missing(self, image_upload_file): |
||
| 87 | obj = ThumbnailModel.objects.create(image=image_upload_file) |
||
| 88 | file_path = obj.image.path |
||
| 89 | assert os.path.exists(file_path) |
||
| 90 | os.remove(file_path) |
||
| 91 | assert not os.path.exists(file_path) |
||
| 92 | time.sleep(1) |
||
| 93 | call_command( |
||
| 94 | 'rendervariations', |
||
| 95 | 'tests.ThumbnailModel.image', |
||
| 96 | '--ignore-missing', |
||
| 97 | replace=True, |
||
| 98 | ) |
||
| 99 | |||
| 100 | def test_short_ignore_missing(self, image_upload_file): |
||
| 101 | obj = ThumbnailModel.objects.create(image=image_upload_file) |
||
| 102 | file_path = obj.image.path |
||
| 103 | assert os.path.exists(file_path) |
||
| 104 | os.remove(file_path) |
||
| 105 | assert not os.path.exists(file_path) |
||
| 106 | time.sleep(1) |
||
| 107 | call_command( |
||
| 108 | 'rendervariations', |
||
| 109 | 'tests.ThumbnailModel.image', |
||
| 110 | '-i', |
||
| 111 | replace=True, |
||
| 112 | ) |
||
| 113 | |||
| 114 | def test_no_ignore_missing(self, image_upload_file): |
||
| 115 | obj = ThumbnailModel.objects.create(image=image_upload_file) |
||
| 116 | file_path = obj.image.path |
||
| 117 | assert os.path.exists(file_path) |
||
| 118 | os.remove(file_path) |
||
| 119 | assert not os.path.exists(file_path) |
||
| 120 | time.sleep(1) |
||
| 121 | with pytest.raises(CommandError): |
||
| 122 | call_command( |
||
| 123 | 'rendervariations', |
||
| 124 | 'tests.ThumbnailModel.image', |
||
| 125 | replace=True, |
||
| 126 | ) |
||
| 127 | |||
| 128 | def test_none_default_storage(self, image_upload_file): |
||
| 129 | obj = MyStorageModel.customer_manager.create( |
||
| 130 | image=image_upload_file |
||
| 131 | ) |
||
| 132 | file_path = obj.image.thumbnail.path |
||
| 133 | obj.image.delete_variations() |
||
| 134 | call_command( |
||
| 135 | 'rendervariations', |
||
| 136 | 'tests.MyStorageModel.image' |
||
| 137 | ) |
||
| 138 | assert os.path.exists(file_path) |
||
| 139 | |||
| 140 | def test_invalid_field_path(self): |
||
| 141 | with pytest.raises(CommandError) as exc_info: |
||
| 142 | call_command( |
||
| 143 | 'rendervariations', |
||
| 144 | 'MyStorageModel.image' |
||
| 145 | ) |
||
| 146 | |||
| 147 | error_message = "Error parsing field_path 'MyStorageModel.image'. "\ |
||
| 148 | "Use format <app.model.field app.model.field>." |
||
| 149 | assert str(exc_info.value) == error_message |
||
| 150 | |||
| 151 | def test_custom_render_variations(self, image_upload_file): |
||
| 152 | obj = CustomRenderVariationsModel.objects.create( |
||
| 153 | image=image_upload_file |
||
| 154 | ) |
||
| 155 | file_path = obj.image.thumbnail.path |
||
| 156 | assert os.path.exists(file_path) |
||
| 157 | with open(file_path, 'rb') as f: |
||
| 158 | before = hashlib.md5(f.read()).hexdigest() |
||
| 159 | call_command( |
||
| 160 | 'rendervariations', |
||
| 161 | 'tests.CustomRenderVariationsModel.image', |
||
| 162 | replace=True, |
||
| 163 | ) |
||
| 164 | assert os.path.exists(file_path) |
||
| 165 | with open(file_path, 'rb') as f: |
||
| 166 | after = hashlib.md5(f.read()).hexdigest() |
||
| 167 | assert before == after |
||
| 168 |