Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
.tox
.idea
.vscode
.coverage
.coverage.*
.venv
MANIFEST
build
dist
Expand Down
42 changes: 23 additions & 19 deletions tests/test_cachefiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ def test_sync_backend_truthiness():
is truthy.

"""
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec)
assert_file_is_truthy(file)
with get_unique_image_file() as source_file:
spec = TestSpec(source=source_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)
with get_unique_image_file() as source_file:
spec = TestSpec(source=source_file)
file = ImageCacheFile(spec, cachefile_backend=DummyAsyncCacheFileBackend())
assert_file_is_falsy(file)


def test_no_source_error():
Expand All @@ -68,13 +70,14 @@ def test_repr_does_not_send_existence_required():
# import here to apply mock
from imagekit.cachefiles import ImageCacheFile

spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(
spec,
cachefile_backend=DummyAsyncCacheFileBackend()
)
file.__repr__()
assert signal.send.called is False
with get_unique_image_file() as source_file:
spec = TestSpec(source=source_file)
file = ImageCacheFile(
spec,
cachefile_backend=DummyAsyncCacheFileBackend()
)
file.__repr__()
assert signal.send.called is False


def test_memcached_cache_key():
Expand Down Expand Up @@ -118,12 +121,13 @@ def test_lazyfile_stringification():


def test_generate_file_already_exists(caplog):
spec = TestSpec(source=get_unique_image_file())
file_1 = ImageCacheFile(spec)
file_1._generate()
# generate another cache image with the same name
file_2 = ImageCacheFile(spec, name=file_1.name)
file_2._generate()
with get_unique_image_file() as source_file:
spec = TestSpec(source=source_file)
file_1 = ImageCacheFile(spec)
file_1._generate()
# generate another cache image with the same name
file_2 = ImageCacheFile(spec, name=file_1.name)
file_2._generate()

assert len(caplog.records) == 1
storage, name, actual_name, cachefile_backend = caplog.records[0].args
Expand Down
16 changes: 8 additions & 8 deletions tests/test_optimistic_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def test_no_io_on_bool():
perform IO operations.

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


def test_no_io_on_url():
Expand All @@ -45,7 +45,7 @@ def test_no_io_on_url():
checked.

"""
file = get_image_cache_file()
file.url
assert not file.storage.exists.called
assert not file.storage.open.called
with get_image_cache_file() as file:
file.url
assert not file.storage.exists.called
assert not file.storage.open.called
21 changes: 11 additions & 10 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def test_circular_ref():

def test_cachefiles():
clear_imagekit_cache()
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec)
file.url
# remove link to file from spec source generator
# test __getstate__ of ImageCacheFile
file.generator.source = None
restored_file = pickleback(file)
assert file is not restored_file
# Assertion for #437 and #451
assert file.storage is restored_file.storage
with get_unique_image_file() as source_file:
spec = TestSpec(source=source_file)
file = ImageCacheFile(spec)
file.url
# remove link to file from spec source generator
# test __getstate__ of ImageCacheFile
file.generator.source = None
restored_file = pickleback(file)
assert file is not restored_file
# Assertion for #437 and #451
assert file.storage is restored_file.storage