From b061e135c27e423ab098b2e7a6e9137ca67cf718 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Fri, 15 Mar 2013 00:49:44 -0400 Subject: [PATCH] Add tests for cachefile truthiness --- tests/test_cachefiles.py | 29 ++++++++++++++++++++++++++--- tests/utils.py | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tests/test_cachefiles.py b/tests/test_cachefiles.py index 6579e89..d3c2902 100644 --- a/tests/test_cachefiles.py +++ b/tests/test_cachefiles.py @@ -1,18 +1,41 @@ from imagekit.cachefiles import ImageCacheFile from nose.tools import raises from .imagegenerators import TestSpec -from .utils import assert_file_is_falsy +from .utils import (assert_file_is_truthy, assert_file_is_falsy, + DummyAsyncCacheFileBackend, get_unique_image_file) -def test_no_source(): +def test_no_source_falsiness(): """ - Ensure sourceless specs are falsy. + Ensure cache files generated from sourceless specs are falsy. + """ spec = TestSpec(source=None) file = ImageCacheFile(spec) assert_file_is_falsy(file) +def test_sync_backend_truthiness(): + """ + Ensure that a cachefile with a synchronous cache file backend (the default) + is truthy. + + """ + spec = TestSpec(source=get_unique_image_file()) + file = ImageCacheFile(spec) + assert_file_is_truthy(file) + + +def test_async_backend_falsiness(): + """ + Ensure that a cachefile with an asynchronous cache file backend is falsy. + + """ + spec = TestSpec(source=get_unique_image_file()) + file = ImageCacheFile(spec, cachefile_backend=DummyAsyncCacheFileBackend()) + assert_file_is_falsy(file) + + @raises(TestSpec.MissingSource) def test_no_source_error(): spec = TestSpec(source=None) diff --git a/tests/utils.py b/tests/utils.py index 39f2271..07085df 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,9 +3,11 @@ import os from django.conf import settings from django.core.files import File from django.template import Context, Template +from imagekit.cachefiles.backends import Simple, CacheFileState from imagekit.lib import Image, StringIO from nose.tools import assert_true, assert_false import pickle +from tempfile import NamedTemporaryFile from .models import Photo @@ -21,6 +23,12 @@ def get_image_file(): return open(path, 'r+b') +def get_unique_image_file(): + file = NamedTemporaryFile() + file.write(get_image_file().read()) + return file + + def create_image(): return Image.open(get_image_file()) @@ -62,3 +70,13 @@ def assert_file_is_falsy(file): def assert_file_is_truthy(file): assert_true(bool(file), 'File is not truthy') + + +class DummyAsyncCacheFileBackend(Simple): + """ + A cache file backend meant to simulate async generation (by marking the + file as pending but never actually creating it). + + """ + def _generate(self, file): + self.set_state(file, CacheFileState.PENDING)