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

This commit is contained in:
Claude Paroz 2019-08-18 11:10:49 +02:00 committed by Mariusz Felisiak
parent 0468159763
commit af69842dbd
2 changed files with 11 additions and 0 deletions

View File

@ -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),

View File

@ -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.