Completed
Push — master ( 84e615...a5de1a )
by Johannes
01:08
created

test_custom_render_variations()   B

Complexity

Conditions 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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