Completed
Push — master ( 42af00...a650c4 )
by Johannes
52s
created

tests.TestRenderVariations   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 77
Duplicated Lines 0 %
Metric Value
wmc 21
dl 0
loc 77
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_no_replace() 0 13 4
A test_replace() 0 14 4
B test_multiprocessing() 0 13 6
A test_no_options() 0 10 2
A test_none_default_storage() 0 10 2
A test_invalid_field_path() 0 10 3
1
import os
2
import time
3
4
from django.core.management import CommandError, call_command
5
6
import pytest
7
from tests.models import ManualVariationsModel, MyStorageModel, ThumbnailModel
8
9
10
@pytest.mark.django_db
11
class TestRenderVariations(object):
12
    def test_no_options(self, image_upload_file):
13
        obj = ManualVariationsModel.customer_manager.create(
14
            image=image_upload_file
15
        )
16
        file_path = obj.image.thumbnail.path
17
        call_command(
18
            'rendervariations',
19
            'tests.ManualVariationsModel.image'
20
        )
21
        assert os.path.exists(file_path)
22
23
    def test_multiprocessing(self, image_upload_file):
24
        file_names = [
25
            ManualVariationsModel.customer_manager.create(
26
                image=image_upload_file
27
            ).image.thumbnail.path
28
            for _ in range(100)
29
        ]
30
        assert not any([os.path.exists(f) for f in file_names])
31
        call_command(
32
            'rendervariations',
33
            'tests.ManualVariationsModel.image'
34
        )
35
        assert any([os.path.exists(f) for f in file_names])
36
37
    def test_no_replace(self, image_upload_file):
38
        obj = ThumbnailModel.objects.create(image=image_upload_file)
39
        file_path = obj.image.thumbnail.path
40
        assert os.path.exists(file_path)
41
        before = os.path.getmtime(file_path)
42
        time.sleep(1)
43
        call_command(
44
            'rendervariations',
45
            'tests.ThumbnailModel.image',
46
        )
47
        assert os.path.exists(file_path)
48
        after = os.path.getmtime(file_path)
49
        assert before == after
50
51
    def test_replace(self, image_upload_file):
52
        obj = ThumbnailModel.objects.create(image=image_upload_file)
53
        file_path = obj.image.thumbnail.path
54
        assert os.path.exists(file_path)
55
        before = os.path.getmtime(file_path)
56
        time.sleep(1)
57
        call_command(
58
            'rendervariations',
59
            'tests.ThumbnailModel.image',
60
            replace=True
61
        )
62
        assert os.path.exists(file_path)
63
        after = os.path.getmtime(file_path)
64
        assert before != after
65
66
    def test_none_default_storage(self, image_upload_file):
67
        obj = MyStorageModel.customer_manager.create(
68
            image=image_upload_file
69
        )
70
        file_path = obj.image.thumbnail.path
71
        call_command(
72
            'rendervariations',
73
            'tests.MyStorageModel.image'
74
        )
75
        assert os.path.exists(file_path)
76
77
    def test_invalid_field_path(self):
78
        with pytest.raises(CommandError) as exc_info:
79
            call_command(
80
                'rendervariations',
81
                'MyStorageModel.image'
82
            )
83
84
        error_message = "Error parsing field_path 'MyStorageModel.image'. "\
85
                        "Use format <app.model.field app.model.field>."
86
        assert str(exc_info.value) == error_message
87