Selaa lähdekoodia

Refs #28428 -- Added test for a callable FileField.upload_to that returns pathlib.Path.

Claude Paroz 5 vuotta sitten
vanhempi
commit
af69842dbd
2 muutettua tiedostoa jossa 11 lisäystä ja 0 poistoa
  1. 5 0
      tests/file_storage/models.py
  2. 6 0
      tests/file_storage/tests.py

+ 5 - 0
tests/file_storage/models.py

@@ -7,6 +7,7 @@ and where files should be stored.
 
 import random
 import tempfile
+from pathlib import Path
 
 from django.core.files.storage import FileSystemStorage
 from django.db import models
@@ -31,8 +32,12 @@ class Storage(models.Model):
         # to make sure it only gets called once.
         return '%s/%s' % (random.randint(100, 999), filename)
 
+    def pathlib_upload_to(self, filename):
+        return Path('bar') / filename
+
     normal = models.FileField(storage=temp_storage, upload_to='tests')
     custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
+    pathlib_callable = models.FileField(storage=temp_storage, upload_to=pathlib_upload_to)
     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
     custom_valid_name = models.FileField(
         storage=CustomValidNameStorage(location=temp_storage_location),

+ 6 - 0
tests/file_storage/tests.py

@@ -792,6 +792,12 @@ class FileFieldStorageTests(TestCase):
         self.assertEqual(obj.empty.read(), b"more content")
         obj.empty.close()
 
+    def test_pathlib_upload_to(self):
+        obj = Storage()
+        obj.pathlib_callable.save('some_file1.txt', ContentFile('some content'))
+        self.assertEqual(obj.pathlib_callable.name, 'bar/some_file1.txt')
+        obj.random.close()
+
     def test_random_upload_to(self):
         # Verify the fix for #5655, making sure the directory is only
         # determined once.