|
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
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
call_command( |
|
|
|
|
|
|
52
|
|
|
'rendervariations', |
|
53
|
|
|
'tests.ThumbnailModel.image' |
|
54
|
|
|
) |
|
55
|
|
|
assert any([os.path.exists(f) for f in file_names]) |
|
56
|
|
|
|
|
57
|
|
|
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
|
|
|
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_none_default_storage(self, image_upload_file): |
|
87
|
|
|
obj = MyStorageModel.customer_manager.create( |
|
88
|
|
|
image=image_upload_file |
|
89
|
|
|
) |
|
90
|
|
|
file_path = obj.image.thumbnail.path |
|
91
|
|
|
obj.image.delete_variations() |
|
92
|
|
|
call_command( |
|
93
|
|
|
'rendervariations', |
|
94
|
|
|
'tests.MyStorageModel.image' |
|
95
|
|
|
) |
|
96
|
|
|
assert os.path.exists(file_path) |
|
97
|
|
|
|
|
98
|
|
|
def test_invalid_field_path(self): |
|
99
|
|
|
with pytest.raises(CommandError) as exc_info: |
|
100
|
|
|
call_command( |
|
101
|
|
|
'rendervariations', |
|
102
|
|
|
'MyStorageModel.image' |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
error_message = "Error parsing field_path 'MyStorageModel.image'. "\ |
|
106
|
|
|
"Use format <app.model.field app.model.field>." |
|
107
|
|
|
assert str(exc_info.value) == error_message |
|
108
|
|
|
|
|
109
|
|
|
def test_custom_render_variations(self, image_upload_file): |
|
110
|
|
|
obj = CustomRenderVariationsModel.objects.create( |
|
111
|
|
|
image=image_upload_file |
|
112
|
|
|
) |
|
113
|
|
|
file_path = obj.image.thumbnail.path |
|
114
|
|
|
assert os.path.exists(file_path) |
|
115
|
|
|
with open(file_path, 'rb') as f: |
|
116
|
|
|
before = hashlib.md5(f.read()).hexdigest() |
|
117
|
|
|
call_command( |
|
118
|
|
|
'rendervariations', |
|
119
|
|
|
'tests.CustomRenderVariationsModel.image', |
|
120
|
|
|
replace=True, |
|
121
|
|
|
) |
|
122
|
|
|
assert os.path.exists(file_path) |
|
123
|
|
|
with open(file_path, 'rb') as f: |
|
124
|
|
|
after = hashlib.md5(f.read()).hexdigest() |
|
125
|
|
|
assert before == after |
|
126
|
|
|
|