mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #27777 -- Improved docs/added test for File context manager change.
This commit is contained in:
@@ -3,6 +3,7 @@ import sys
|
||||
import unittest
|
||||
|
||||
from django.core.files import temp
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.uploadedfile import TemporaryUploadedFile
|
||||
from django.db.utils import IntegrityError
|
||||
from django.test import TestCase, override_settings
|
||||
@@ -83,3 +84,13 @@ class FileFieldTests(TestCase):
|
||||
tmp_file_path = tmp_file.temporary_file_path()
|
||||
Document.objects.create(myfile=tmp_file)
|
||||
self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists')
|
||||
|
||||
def test_open_returns_self(self):
|
||||
"""
|
||||
FieldField.open() returns self so it can be used as a context manager.
|
||||
"""
|
||||
d = Document.objects.create(myfile='something.txt')
|
||||
# Replace the FileField's file with an in-memory ContentFile, so that
|
||||
# open() doesn't write to disk.
|
||||
d.myfile.file = ContentFile(b'', name='bla')
|
||||
self.assertEqual(d.myfile, d.myfile.open())
|
||||
|
||||
Reference in New Issue
Block a user