Refs #21238 -- Added more tests for pickling FileField and ImageField.
This commit is contained in:
parent
66e16dcc99
commit
aaea9deac4
|
@ -1,10 +1,11 @@
|
||||||
import os
|
import os
|
||||||
|
import pickle
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from django.core.files import temp
|
from django.core.files import File, temp
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.core.files.uploadedfile import TemporaryUploadedFile
|
from django.core.files.uploadedfile import TemporaryUploadedFile
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
|
@ -103,3 +104,21 @@ class FileFieldTests(TestCase):
|
||||||
with TemporaryUploadedFile('foo.txt', 'text/plain', 1, 'utf-8') as tmp_file:
|
with TemporaryUploadedFile('foo.txt', 'text/plain', 1, 'utf-8') as tmp_file:
|
||||||
Document.objects.create(myfile=tmp_file)
|
Document.objects.create(myfile=tmp_file)
|
||||||
self.assertTrue(os.path.exists(os.path.join(tmp_dir, 'unused', 'foo.txt')))
|
self.assertTrue(os.path.exists(os.path.join(tmp_dir, 'unused', 'foo.txt')))
|
||||||
|
|
||||||
|
def test_pickle(self):
|
||||||
|
with open(__file__, 'rb') as fp:
|
||||||
|
file1 = File(fp, name='test_file.py')
|
||||||
|
document = Document(myfile='test_file.py')
|
||||||
|
document.myfile.save('test_file.py', file1)
|
||||||
|
|
||||||
|
dump = pickle.dumps(document)
|
||||||
|
loaded_document = pickle.loads(dump)
|
||||||
|
self.assertEqual(document.myfile, loaded_document.myfile)
|
||||||
|
self.assertEqual(document.myfile.url, loaded_document.myfile.url)
|
||||||
|
self.assertEqual(document.myfile.storage, loaded_document.myfile.storage)
|
||||||
|
self.assertEqual(document.myfile.instance, loaded_document.myfile.instance)
|
||||||
|
self.assertEqual(document.myfile.field, loaded_document.myfile.field)
|
||||||
|
|
||||||
|
myfile_dump = pickle.dumps(document.myfile)
|
||||||
|
loaded_myfile = pickle.loads(myfile_dump)
|
||||||
|
self.assertEqual(document.myfile, loaded_myfile)
|
||||||
|
|
|
@ -180,6 +180,14 @@ class ImageFieldTests(ImageFieldTestMixin, TestCase):
|
||||||
|
|
||||||
loaded_p = pickle.loads(dump)
|
loaded_p = pickle.loads(dump)
|
||||||
self.assertEqual(p.mugshot, loaded_p.mugshot)
|
self.assertEqual(p.mugshot, loaded_p.mugshot)
|
||||||
|
self.assertEqual(p.mugshot.url, loaded_p.mugshot.url)
|
||||||
|
self.assertEqual(p.mugshot.storage, loaded_p.mugshot.storage)
|
||||||
|
self.assertEqual(p.mugshot.instance, loaded_p.mugshot.instance)
|
||||||
|
self.assertEqual(p.mugshot.field, loaded_p.mugshot.field)
|
||||||
|
|
||||||
|
mugshot_dump = pickle.dumps(p.mugshot)
|
||||||
|
loaded_mugshot = pickle.loads(mugshot_dump)
|
||||||
|
self.assertEqual(p.mugshot, loaded_mugshot)
|
||||||
|
|
||||||
def test_defer(self):
|
def test_defer(self):
|
||||||
self.PersonModel.objects.create(name='Joe', mugshot=self.file1)
|
self.PersonModel.objects.create(name='Joe', mugshot=self.file1)
|
||||||
|
|
Loading…
Reference in New Issue