Completed
Pull Request — master (#179)
by Johannes
28s
created

TestRenderVariations   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 109
Duplicated Lines 24.77 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 30
c 8
b 0
f 0
dl 27
loc 109
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test_replace() 0 14 4
C test_multiprocessing() 15 19 8
A _swap_concurrent_executor() 0 4 1
B test_custom_render_variations() 0 17 6
A test_invalid_field_path() 0 10 3
A test_none_default_storage() 0 11 2
A test_no_options() 0 11 2
A test_no_replace() 11 13 4

How to fix   Duplicated Code   

Duplicated Code

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:

1
import hashlib
2
import os
3
import time
4
from concurrent.futures import ThreadPoolExecutor
5
6
import pytest
7
from django.core.management import CommandError, call_command
8
9
from tests.models import (
10
    CustomRenderVariationsModel, MyStorageModel, ThumbnailModel,
11
    ResizeCropModel)
12
13
14
@pytest.mark.django_db
15
class TestRenderVariations:
16
17
    @pytest.fixture(autouse=True)
18
    def _swap_concurrent_executor(self, monkeypatch):
19
        """Switch ProcessPoolExecutor with ThreadPoolExecutor to monitor coverage."""
20
        monkeypatch.setattr('concurrent.futures.ProcessPoolExecutor', ThreadPoolExecutor)
21
22
    def test_no_options(self, image_upload_file):
23
        obj = ThumbnailModel.objects.create(
24
            image=image_upload_file
25
        )
26
        file_path = obj.image.thumbnail.path
27
        obj.image.delete_variations()
28
        call_command(
29
            'rendervariations',
30
            'tests.ThumbnailModel.image'
31
        )
32
        assert os.path.exists(file_path)
33
34
    def test_multiprocessing(self, image_upload_file):
35
        objs = [
36
            ThumbnailModel.objects.create(
37 View Code Duplication
                image=image_upload_file
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
            )
39
            for _ in range(100)
40
        ]
41
        file_names = [
42
            obj.image.thumbnail.path
43
            for obj in objs
44
            ]
45
        for obj in objs:
46
            obj.image.delete_variations()
47
        assert not any([os.path.exists(f) for f in file_names])
48
        call_command(
49
            'rendervariations',
50
            'tests.ThumbnailModel.image'
51 View Code Duplication
        )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
        assert any([os.path.exists(f) for f in file_names])
53
54
    def test_no_replace(self, image_upload_file):
55
        obj = ThumbnailModel.objects.create(image=image_upload_file)
56
        file_path = obj.image.thumbnail.path
57
        assert os.path.exists(file_path)
58
        before = os.path.getmtime(file_path)
59
        time.sleep(1)
60
        call_command(
61
            'rendervariations',
62
            'tests.ThumbnailModel.image',
63
        )
64
        assert os.path.exists(file_path)
65
        after = os.path.getmtime(file_path)
66
        assert before == after
67
68
    def test_replace(self, image_upload_file):
69
        obj = ThumbnailModel.objects.create(image=image_upload_file)
70
        file_path = obj.image.thumbnail.path
71
        assert os.path.exists(file_path)
72
        before = os.path.getmtime(file_path)
73
        time.sleep(1)
74
        call_command(
75
            'rendervariations',
76
            'tests.ThumbnailModel.image',
77
            replace=True
78
        )
79
        assert os.path.exists(file_path)
80
        after = os.path.getmtime(file_path)
81
        assert before != after
82
83
    def test_none_default_storage(self, image_upload_file):
84
        obj = MyStorageModel.customer_manager.create(
85
            image=image_upload_file
86
        )
87
        file_path = obj.image.thumbnail.path
88
        obj.image.delete_variations()
89
        call_command(
90
            'rendervariations',
91
            'tests.MyStorageModel.image'
92
        )
93
        assert os.path.exists(file_path)
94
95
    def test_invalid_field_path(self):
96
        with pytest.raises(CommandError) as exc_info:
97
            call_command(
98
                'rendervariations',
99
                'MyStorageModel.image'
100
            )
101
102
        error_message = "Error parsing field_path 'MyStorageModel.image'. "\
103
                        "Use format <app.model.field app.model.field>."
104
        assert str(exc_info.value) == error_message
105
106
    def test_custom_render_variations(self, image_upload_file):
107
        obj = CustomRenderVariationsModel.objects.create(
108
            image=image_upload_file
109
        )
110
        file_path = obj.image.thumbnail.path
111
        assert os.path.exists(file_path)
112
        with open(file_path, 'rb') as f:
113
            before = hashlib.md5(f.read()).hexdigest()
114
        call_command(
115
            'rendervariations',
116
            'tests.CustomRenderVariationsModel.image',
117
            replace=True,
118
        )
119
        assert os.path.exists(file_path)
120
        with open(file_path, 'rb') as f:
121
            after = hashlib.md5(f.read()).hexdigest()
122
        assert before == after
123