Skip to content

Commit 63b24f6

Browse files
authored
Merge pull request #584 from matthewwithanm/test/resource-warnings
Remove ResourceWarning during tests
2 parents 3dd227e + 5d9b10d commit 63b24f6

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
.tox
77
.idea
88
.vscode
9+
.coverage
10+
.coverage.*
11+
.venv
912
MANIFEST
1013
build
1114
dist

tests/test_cachefiles.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ def test_sync_backend_truthiness():
3030
is truthy.
3131
3232
"""
33-
spec = TestSpec(source=get_unique_image_file())
34-
file = ImageCacheFile(spec)
35-
assert_file_is_truthy(file)
33+
with get_unique_image_file() as source_file:
34+
spec = TestSpec(source=source_file)
35+
file = ImageCacheFile(spec)
36+
assert_file_is_truthy(file)
3637

3738

3839
def test_async_backend_falsiness():
3940
"""
4041
Ensure that a cachefile with an asynchronous cache file backend is falsy.
4142
4243
"""
43-
spec = TestSpec(source=get_unique_image_file())
44-
file = ImageCacheFile(spec, cachefile_backend=DummyAsyncCacheFileBackend())
45-
assert_file_is_falsy(file)
44+
with get_unique_image_file() as source_file:
45+
spec = TestSpec(source=source_file)
46+
file = ImageCacheFile(spec, cachefile_backend=DummyAsyncCacheFileBackend())
47+
assert_file_is_falsy(file)
4648

4749

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

71-
spec = TestSpec(source=get_unique_image_file())
72-
file = ImageCacheFile(
73-
spec,
74-
cachefile_backend=DummyAsyncCacheFileBackend()
75-
)
76-
file.__repr__()
77-
assert signal.send.called is False
73+
with get_unique_image_file() as source_file:
74+
spec = TestSpec(source=source_file)
75+
file = ImageCacheFile(
76+
spec,
77+
cachefile_backend=DummyAsyncCacheFileBackend()
78+
)
79+
file.__repr__()
80+
assert signal.send.called is False
7881

7982

8083
def test_memcached_cache_key():
@@ -118,12 +121,13 @@ def test_lazyfile_stringification():
118121

119122

120123
def test_generate_file_already_exists(caplog):
121-
spec = TestSpec(source=get_unique_image_file())
122-
file_1 = ImageCacheFile(spec)
123-
file_1._generate()
124-
# generate another cache image with the same name
125-
file_2 = ImageCacheFile(spec, name=file_1.name)
126-
file_2._generate()
124+
with get_unique_image_file() as source_file:
125+
spec = TestSpec(source=source_file)
126+
file_1 = ImageCacheFile(spec)
127+
file_1._generate()
128+
# generate another cache image with the same name
129+
file_2 = ImageCacheFile(spec, name=file_1.name)
130+
file_2._generate()
127131

128132
assert len(caplog.records) == 1
129133
storage, name, actual_name, cachefile_backend = caplog.records[0].args

tests/test_optimistic_strategy.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def test_no_io_on_bool():
3333
perform IO operations.
3434
3535
"""
36-
file = get_image_cache_file()
37-
bool(file)
38-
assert not file.storage.exists.called
39-
assert not file.storage.open.called
36+
with get_image_cache_file() as file:
37+
bool(file)
38+
assert not file.storage.exists.called
39+
assert not file.storage.open.called
4040

4141

4242
def test_no_io_on_url():
@@ -45,7 +45,7 @@ def test_no_io_on_url():
4545
checked.
4646
4747
"""
48-
file = get_image_cache_file()
49-
file.url
50-
assert not file.storage.exists.called
51-
assert not file.storage.open.called
48+
with get_image_cache_file() as file:
49+
file.url
50+
assert not file.storage.exists.called
51+
assert not file.storage.open.called

tests/test_serialization.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ def test_circular_ref():
3636

3737
def test_cachefiles():
3838
clear_imagekit_cache()
39-
spec = TestSpec(source=get_unique_image_file())
40-
file = ImageCacheFile(spec)
41-
file.url
42-
# remove link to file from spec source generator
43-
# test __getstate__ of ImageCacheFile
44-
file.generator.source = None
45-
restored_file = pickleback(file)
46-
assert file is not restored_file
47-
# Assertion for #437 and #451
48-
assert file.storage is restored_file.storage
39+
with get_unique_image_file() as source_file:
40+
spec = TestSpec(source=source_file)
41+
file = ImageCacheFile(spec)
42+
file.url
43+
# remove link to file from spec source generator
44+
# test __getstate__ of ImageCacheFile
45+
file.generator.source = None
46+
restored_file = pickleback(file)
47+
assert file is not restored_file
48+
# Assertion for #437 and #451
49+
assert file.storage is restored_file.storage

0 commit comments

Comments
 (0)