2016-04-10 01:17:34 +08:00
|
|
|
import pickle
|
|
|
|
|
2020-02-12 21:48:49 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-04-10 01:17:34 +08:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2020-02-12 21:48:49 +08:00
|
|
|
from django.forms import FileField
|
2016-04-10 01:17:34 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class FileFieldTest(SimpleTestCase):
|
|
|
|
def test_filefield_1(self):
|
|
|
|
f = FileField()
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean("")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean("", "")
|
|
|
|
self.assertEqual("files/test1.pdf", f.clean("", "files/test1.pdf"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(None)
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(None, "")
|
|
|
|
self.assertEqual("files/test2.pdf", f.clean(None, "files/test2.pdf"))
|
|
|
|
no_file_msg = "'No file was submitted. Check the encoding type on the form.'"
|
2021-04-15 00:23:44 +08:00
|
|
|
file = SimpleUploadedFile(None, b"")
|
|
|
|
file._name = ""
|
2016-04-10 01:17:34 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, no_file_msg):
|
2021-04-15 00:23:44 +08:00
|
|
|
f.clean(file)
|
2016-04-10 01:17:34 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, no_file_msg):
|
2021-04-15 00:23:44 +08:00
|
|
|
f.clean(file, "")
|
2016-04-10 01:17:34 +08:00
|
|
|
self.assertEqual("files/test3.pdf", f.clean(None, "files/test3.pdf"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, no_file_msg):
|
|
|
|
f.clean("some content that is not a file")
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "'The submitted file is empty.'"
|
|
|
|
):
|
|
|
|
f.clean(SimpleUploadedFile("name", None))
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "'The submitted file is empty.'"
|
|
|
|
):
|
|
|
|
f.clean(SimpleUploadedFile("name", b""))
|
|
|
|
self.assertEqual(
|
|
|
|
SimpleUploadedFile,
|
|
|
|
type(f.clean(SimpleUploadedFile("name", b"Some File Content"))),
|
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
2017-02-08 01:05:47 +08:00
|
|
|
f.clean(
|
|
|
|
SimpleUploadedFile(
|
|
|
|
"我隻氣墊船裝滿晒鱔.txt", "मेरी मँडराने वाली नाव सर्पमीनों से भरी ह".encode()
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2017-02-08 01:05:47 +08:00
|
|
|
),
|
2016-04-10 01:17:34 +08:00
|
|
|
SimpleUploadedFile,
|
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
|
|
|
f.clean(
|
|
|
|
SimpleUploadedFile("name", b"Some File Content"), "files/test4.pdf"
|
|
|
|
),
|
|
|
|
SimpleUploadedFile,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filefield_2(self):
|
|
|
|
f = FileField(max_length=5)
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError,
|
|
|
|
"'Ensure this filename has at most 5 characters (it has 18).'",
|
|
|
|
):
|
|
|
|
f.clean(SimpleUploadedFile("test_maxlength.txt", b"hello world"))
|
|
|
|
self.assertEqual("files/test1.pdf", f.clean("", "files/test1.pdf"))
|
|
|
|
self.assertEqual("files/test2.pdf", f.clean(None, "files/test2.pdf"))
|
|
|
|
self.assertIsInstance(
|
|
|
|
f.clean(SimpleUploadedFile("name", b"Some File Content")),
|
|
|
|
SimpleUploadedFile,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filefield_3(self):
|
|
|
|
f = FileField(allow_empty_file=True)
|
|
|
|
self.assertIsInstance(
|
|
|
|
f.clean(SimpleUploadedFile("name", b"")), SimpleUploadedFile
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filefield_changed(self):
|
|
|
|
"""
|
|
|
|
The value of data will more than likely come from request.FILES. The
|
|
|
|
value of initial data will likely be a filename stored in the database.
|
|
|
|
Since its value is of no use to a FileField it is ignored.
|
|
|
|
"""
|
|
|
|
f = FileField()
|
|
|
|
|
|
|
|
# No file was uploaded and no initial data.
|
|
|
|
self.assertFalse(f.has_changed("", None))
|
|
|
|
|
|
|
|
# A file was uploaded and no initial data.
|
|
|
|
self.assertTrue(
|
|
|
|
f.has_changed("", {"filename": "resume.txt", "content": "My resume"})
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
# A file was not uploaded, but there is initial data
|
|
|
|
self.assertFalse(f.has_changed("resume.txt", None))
|
|
|
|
|
|
|
|
# A file was uploaded and there is initial data (file identity is not dealt
|
|
|
|
# with here)
|
|
|
|
self.assertTrue(
|
|
|
|
f.has_changed(
|
|
|
|
"resume.txt", {"filename": "resume.txt", "content": "My resume"}
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-10 01:17:34 +08:00
|
|
|
|
2017-07-13 22:55:32 +08:00
|
|
|
def test_disabled_has_changed(self):
|
|
|
|
f = FileField(disabled=True)
|
|
|
|
self.assertIs(f.has_changed("x", "y"), False)
|
|
|
|
|
2016-04-10 01:17:34 +08:00
|
|
|
def test_file_picklable(self):
|
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(FileField())), FileField)
|