Skip to content

Commit

Permalink
[s3] fix S3Boto3StorageFile.closed (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordancrane committed May 23, 2023
1 parent 155f85d commit af91e2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions storages/backends/s3boto3.py
Expand Up @@ -132,6 +132,10 @@ def __init__(self, name, mode, storage, buffer_size=None):
def size(self):
return self.obj.content_length

@property
def closed(self):
return not self._file or self._file.closed

def _get_file(self):
if self._file is None:
self._file = SpooledTemporaryFile(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_s3boto3.py
Expand Up @@ -787,3 +787,24 @@ def test_querystring_auth(self):

def test_save(self):
self.storage.save('x.txt', ContentFile(b'abc'))


class S3Boto3StorageFileTests(TestCase):
def setUp(self) -> None:
self.storage = s3boto3.S3Boto3Storage()
self.storage._connections.connection = mock.MagicMock()

def test_closed(self):
f = s3boto3.S3Boto3StorageFile('test', 'wb', self.storage)

with self.subTest("is True after init"):
self.assertTrue(f.closed)

with self.subTest("is False after file access"):
# Ensure _get_file has been called
f.file
self.assertFalse(f.closed)

with self.subTest("is True after close"):
f.close()
self.assertTrue(f.closed)

0 comments on commit af91e2c

Please sign in to comment.