File: test_optimistic_strategy.py

package info (click to toggle)
python-django-imagekit 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 692 kB
  • sloc: python: 1,975; makefile: 133; sh: 6
file content (51 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from unittest.mock import Mock

from django.core.files.storage import FileSystemStorage

from imagekit.cachefiles import ImageCacheFile
from imagekit.cachefiles.backends import Simple as SimpleCFBackend
from imagekit.cachefiles.strategies import Optimistic as OptimisticStrategy

from .utils import create_image


class ImageGenerator:
    def generate(self):
        return create_image()

    def get_hash(self):
        return 'abc123'


def get_image_cache_file():
    storage = Mock(FileSystemStorage)
    backend = SimpleCFBackend()
    strategy = OptimisticStrategy()
    generator = ImageGenerator()
    return ImageCacheFile(generator, storage=storage,
                          cachefile_backend=backend,
                          cachefile_strategy=strategy)


def test_no_io_on_bool():
    """
    When checking the truthiness of an ImageCacheFile, the storage shouldn't
    perform IO operations.

    """
    file = get_image_cache_file()
    bool(file)
    assert not file.storage.exists.called
    assert not file.storage.open.called


def test_no_io_on_url():
    """
    When getting the URL of an ImageCacheFile, the storage shouldn't be
    checked.

    """
    file = get_image_cache_file()
    file.url
    assert not file.storage.exists.called
    assert not file.storage.open.called