diff options
| author | Michael Scott <michael.scott250@gmail.com> | 2016-09-20 22:31:23 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-28 20:11:03 -0400 |
| commit | ec9ed07488aa46fa9d9fd70ed99e82fdf184632a (patch) | |
| tree | c080eab116a5788deb0cdb93b82739f7ddf92d0c /tests/model_fields | |
| parent | 625cd5bcb315154ed28061fefaf639aff54da7c2 (diff) | |
Fixed #27188 -- Allowed using unique=True with FileField.
Thanks Tim Graham for the initial patch.
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/models.py | 2 | ||||
| -rw-r--r-- | tests/model_fields/test_filefield.py | 10 |
2 files changed, 11 insertions, 1 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index aeeb6e88da..faa7d08189 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -216,7 +216,7 @@ class DataModel(models.Model): class Document(models.Model): - myfile = models.FileField(upload_to='unused') + myfile = models.FileField(upload_to='unused', unique=True) ############################################################################### # ImageField diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py index e67b6dd515..3fefcd1fb6 100644 --- a/tests/model_fields/test_filefield.py +++ b/tests/model_fields/test_filefield.py @@ -4,6 +4,7 @@ import unittest from django.core.files import temp from django.core.files.uploadedfile import TemporaryUploadedFile +from django.db.utils import IntegrityError from django.test import TestCase, override_settings from .models import Document @@ -61,6 +62,15 @@ class FileFieldTests(TestCase): Document.objects.create(myfile='something.txt') self.assertEqual(Document.objects.defer('myfile')[0].myfile, 'something.txt') + def test_unique_when_same_filename(self): + """ + A FileField with unique=True shouldn't allow two instances with the + same name to be saved. + """ + Document.objects.create(myfile='something.txt') + with self.assertRaises(IntegrityError): + Document.objects.create(myfile='something.txt') + @unittest.skipIf(sys.platform.startswith('win'), "Windows doesn't support moving open files.") # The file's source and destination must be on the same filesystem. @override_settings(MEDIA_ROOT=temp.gettempdir()) |
