summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorJonny Park <jonnythebard9@gmail.com>2024-04-28 11:54:54 +0900
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-05-15 12:02:45 +0200
commitc0b0ce85ede1f1baf8826fb6c10c23f9adcbcca3 (patch)
treeb691d7613f4b63530cd696b477c79fadfc1baec2 /tests/model_fields
parentb691accea13da0f703728b1d62657cb7ba87da60 (diff)
Fixed #35384 -- Raised FieldError when saving a file without a name to FileField.
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/test_filefield.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py
index 2259c1e480..478e9edd36 100644
--- a/tests/model_fields/test_filefield.py
+++ b/tests/model_fields/test_filefield.py
@@ -5,13 +5,14 @@ import tempfile
import unittest
from pathlib import Path
-from django.core.exceptions import SuspiciousFileOperation
+from django.core.exceptions import FieldError, SuspiciousFileOperation
from django.core.files import File, temp
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.db import IntegrityError, models
from django.test import TestCase, override_settings
from django.test.utils import isolate_apps
+from django.utils.version import PY311
from .models import Document
@@ -72,6 +73,27 @@ class FileFieldTests(TestCase):
with self.assertRaisesMessage(SuspiciousFileOperation, msg):
document.save()
+ def test_save_content_file_without_name(self):
+ d = Document()
+ d.myfile = ContentFile(b"")
+ msg = "File for myfile must have the name attribute specified to be saved."
+ with self.assertRaisesMessage(FieldError, msg) as cm:
+ d.save()
+
+ if PY311:
+ self.assertEqual(
+ cm.exception.__notes__, ["Pass a 'name' argument to ContentFile."]
+ )
+
+ def test_delete_content_file(self):
+ file = ContentFile(b"", name="foo")
+ d = Document.objects.create(myfile=file)
+ d.myfile.delete()
+ self.assertIsNone(d.myfile.name)
+ msg = "The 'myfile' attribute has no file associated with it."
+ with self.assertRaisesMessage(ValueError, msg):
+ getattr(d.myfile, "file")
+
def test_defer(self):
Document.objects.create(myfile="something.txt")
self.assertEqual(Document.objects.defer("myfile")[0].myfile, "something.txt")