Completed
Pull Request — master (#81)
by
unknown
01:14
created

tests.TestRenderVariations.test_replace()   A

Complexity

Conditions 4

Size

Total Lines 14

Duplication

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