diff options
Diffstat (limited to 'tests/regressiontests/model_fields')
| -rw-r--r-- | tests/regressiontests/model_fields/models.py | 6 | ||||
| -rw-r--r-- | tests/regressiontests/model_fields/tests.py | 39 |
2 files changed, 44 insertions, 1 deletions
diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py index 45cd223142..1dc1649f13 100644 --- a/tests/regressiontests/model_fields/models.py +++ b/tests/regressiontests/model_fields/models.py @@ -67,6 +67,12 @@ class BooleanModel(models.Model): string = models.CharField(max_length=10, default='abc') ############################################################################### +# FileField + +class Document(models.Model): + myfile = models.FileField(upload_to='unused') + +############################################################################### # ImageField # If PIL available, do these tests. diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py index 72a7d4d657..58c32b5c0a 100644 --- a/tests/regressiontests/model_fields/tests.py +++ b/tests/regressiontests/model_fields/tests.py @@ -6,8 +6,9 @@ import django.test from django import forms from django.db import models from django.core.exceptions import ValidationError +from django.db.models.fields.files import FieldFile -from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel +from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document # If PIL available, do these tests. if Image: @@ -311,3 +312,39 @@ class TypeCoercionTests(django.test.TestCase): def test_lookup_integer_in_textfield(self): self.assertEquals(Post.objects.filter(body=24).count(), 0) +class FileFieldTests(unittest.TestCase): + def test_clearable(self): + """ + Test that FileField.save_form_data will clear its instance attribute + value if passed False. + + """ + d = Document(myfile='something.txt') + self.assertEqual(d.myfile, 'something.txt') + field = d._meta.get_field('myfile') + field.save_form_data(d, False) + self.assertEqual(d.myfile, '') + + def test_unchanged(self): + """ + Test that FileField.save_form_data considers None to mean "no change" + rather than "clear". + + """ + d = Document(myfile='something.txt') + self.assertEqual(d.myfile, 'something.txt') + field = d._meta.get_field('myfile') + field.save_form_data(d, None) + self.assertEqual(d.myfile, 'something.txt') + + def test_changed(self): + """ + Test that FileField.save_form_data, if passed a truthy value, updates + its instance attribute. + + """ + d = Document(myfile='something.txt') + self.assertEqual(d.myfile, 'something.txt') + field = d._meta.get_field('myfile') + field.save_form_data(d, 'else.txt') + self.assertEqual(d.myfile, 'else.txt') |
