From c92f53c1b09f5e7dc01790d7a5f611a1bd2f0856 Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Tue, 23 Sep 2014 18:32:00 -0400 Subject: [PATCH] Test that Optimistic strategy doesn't cause reads Using the Optimistic strategy should prevent IO ops when you cast the file as a boolean. --- setup.py | 1 + tests/test_optimistic_strategy.py | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_optimistic_strategy.py diff --git a/setup.py b/setup.py index 9a65738..8f60701 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ setup( 'nose-progressive==1.5', 'django-nose==1.2', 'Pillow<3.0', + 'mock==1.0.1', ], test_suite='testrunner.run_tests', install_requires=[ diff --git a/tests/test_optimistic_strategy.py b/tests/test_optimistic_strategy.py new file mode 100644 index 0000000..d3ce959 --- /dev/null +++ b/tests/test_optimistic_strategy.py @@ -0,0 +1,37 @@ +from nose.tools import assert_true, assert_false +from imagekit.cachefiles import ImageCacheFile +from mock import Mock +from .utils import create_image +from django.core.files.storage import FileSystemStorage +from imagekit.cachefiles.backends import Simple as SimpleCFBackend +from imagekit.cachefiles.strategies import Optimistic as OptimisticStrategy + + +class ImageGenerator(object): + 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 + peform IO operations. + + """ + file = get_image_cache_file() + bool(file) + assert_false(file.storage.exists.called) + assert_false(file.storage.open.called)