Fixed #35658 -- Initialized InMemoryFileNode instances with a name.

This commit is contained in:
lucasesposito 2024-08-07 03:45:16 +02:00 committed by Sarah Boyce
parent 69aa13ffb9
commit f16a9a556f
2 changed files with 24 additions and 4 deletions

View File

@ -45,10 +45,9 @@ class InMemoryFileNode(ContentFile, TimingMixin):
modification, and access times.
"""
def __init__(self, content="", name=""):
self.file = None
def __init__(self, content="", name=None):
super().__init__(content, name)
self._content_type = type(content)
self._initialize_stream()
self._initialize_times()
def open(self, mode):
@ -142,7 +141,11 @@ class InMemoryDirNode(TimingMixin):
if create_if_missing:
self._update_accessed_time()
self._update_modified_time()
return self._children.setdefault(path_segment, child_cls())
if child_cls is InMemoryFileNode:
child = child_cls(name=path_segment)
else:
child = child_cls()
return self._children.setdefault(path_segment, child)
return self._children.get(path_segment)
def listdir(self):

View File

@ -1024,6 +1024,23 @@ class FileFieldStorageTests(TestCase):
with temp_storage.open("tests/stringio") as f:
self.assertEqual(f.read(), b"content")
@override_settings(
STORAGES={
DEFAULT_STORAGE_ALIAS: {
"BACKEND": "django.core.files.storage.InMemoryStorage"
}
}
)
def test_create_file_field_from_another_file_field_in_memory_storage(self):
f = ContentFile("content", "file.txt")
obj = Storage.objects.create(storage_callable_default=f)
new_obj = Storage.objects.create(
storage_callable_default=obj.storage_callable_default.file
)
storage = callable_default_storage()
with storage.open(new_obj.storage_callable_default.name) as f:
self.assertEqual(f.read(), b"content")
class FieldCallableFileStorageTests(SimpleTestCase):
def setUp(self):