Code

Ticket #18515: 18515-2.diff

File 18515-2.diff, 4.2 KB (added by claudep, 2 years ago)

Regrouped FileField max_length tests

Line 
1diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
2index e0c0959..b0dce38 100644
3--- a/django/db/models/fields/files.py
4+++ b/django/db/models/fields/files.py
5@@ -242,7 +242,11 @@ class FileField(Field):
6         # (ie. upload_to='path/to/upload/dir'), the length of the generated
7         # name equals the length of the uploaded name plus a constant. Thus
8         # we can tell the user how much shorter the name should be (roughly).
9-        length = len(self.generate_filename(model_instance, value.name))
10+        if value and value._committed:
11+            filename = value.name
12+        else:
13+            filename = self.generate_filename(model_instance, value.name)
14+        length = len(filename)
15         if self.max_length and length > self.max_length:
16             error_values = {'extra': length - self.max_length}
17             raise ValidationError(self.error_messages['max_length'] % error_values)
18diff --git a/tests/modeltests/files/models.py b/tests/modeltests/files/models.py
19index 4134472..cefc7c7 100644
20--- a/tests/modeltests/files/models.py
21+++ b/tests/modeltests/files/models.py
22@@ -26,5 +26,5 @@ class Storage(models.Model):
23 
24     normal = models.FileField(storage=temp_storage, upload_to='tests')
25     custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
26-    random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
27+    random = models.FileField(storage=temp_storage, upload_to=random_upload_to, max_length=16)
28     default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
29diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
30index 3e256f7..565d4c8 100644
31--- a/tests/modeltests/files/tests.py
32+++ b/tests/modeltests/files/tests.py
33@@ -5,6 +5,7 @@ import shutil
34 import tempfile
35 
36 from django.core.cache import cache
37+from django.core.exceptions import ValidationError
38 from django.core.files import File
39 from django.core.files.base import ContentFile
40 from django.core.files.uploadedfile import SimpleUploadedFile
41@@ -102,11 +103,23 @@ class FileStorageTests(TestCase):
42         obj4.random.save("random_file", ContentFile(b"random content"))
43         self.assertTrue(obj4.random.name.endswith("/random_file"))
44 
45-        # Clean up the temporary files and dir.
46-        obj1.normal.delete()
47-        obj2.normal.delete()
48-        obj3.default.delete()
49-        obj4.random.delete()
50+    def test_max_length(self):
51+        """
52+        Test that FileField validates the length of the generated file name
53+        that will be stored in the database. Regression for #9893.
54+        """
55+        # upload_to = 'unused', so file names are saved as '456/xxxxx'.
56+        # max_length = 16, so names longer than 12 characters are rejected.
57+        s1 = Storage(random=SimpleUploadedFile(12 * 'x', b"content"))
58+        s1.full_clean()
59+        with self.assertRaises(ValidationError):
60+            Storage(random=SimpleUploadedFile(13 * 'x', b"content")).full_clean()
61+
62+        # Ticket #18515: validation for an already saved file should not check
63+        # against a regenerated file name (and potentially raise a ValidationError
64+        # if max_length is exceeded
65+        s1.save()
66+        s1.full_clean()
67 
68 
69 class FileTests(unittest.TestCase):
70diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
71index 5d3d42e..a89ffca 100644
72--- a/tests/regressiontests/model_fields/tests.py
73+++ b/tests/regressiontests/model_fields/tests.py
74@@ -365,15 +365,3 @@ class FileFieldTests(unittest.TestCase):
75         field = d._meta.get_field('myfile')
76         field.save_form_data(d, 'else.txt')
77         self.assertEqual(d.myfile, 'else.txt')
78-
79-    def test_max_length(self):
80-        """
81-        Test that FileField validates the length of the generated file name
82-        that will be stored in the database. Regression for #9893.
83-
84-        """
85-        # upload_to = 'unused', so file names are saved as 'unused/xxxxx'.
86-        # max_length = 100, so names longer than 93 characters are rejected.
87-        Document(myfile=93 * 'x').full_clean()
88-        with self.assertRaises(ValidationError):
89-            Document(myfile=94 * 'x').full_clean()