~ubuntu-branches/ubuntu/oneiric/python-django/oneiric

« back to all changes in this revision

Viewing changes to tests/regressiontests/model_forms_regress/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.1.10 upstream) (4.4.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100521075255-i1zpeyc0k8512pd7
Tags: 1.2-1
New upstream stable release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
from django import db
4
4
from django import forms
5
 
from django.forms.models import modelform_factory
 
5
from django.forms.models import modelform_factory, ModelChoiceField
6
6
from django.conf import settings
7
7
from django.test import TestCase
8
8
 
9
 
from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
 
9
from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF, Author, Author1
 
10
 
10
11
 
11
12
class ModelMultipleChoiceFieldTests(TestCase):
12
13
 
50
51
        form = TripleForm({'left': '1', 'middle': '3', 'right': '1'})
51
52
        self.failUnless(form.is_valid())
52
53
 
 
54
class TripleFormWithCleanOverride(forms.ModelForm):
 
55
    class Meta:
 
56
        model = Triple
 
57
 
 
58
    def clean(self):
 
59
        if not self.cleaned_data['left'] == self.cleaned_data['right']:
 
60
            raise forms.ValidationError('Left and right should be equal')
 
61
        return self.cleaned_data
 
62
 
 
63
class OverrideCleanTests(TestCase):
 
64
    def test_override_clean(self):
 
65
        """
 
66
        Regression for #12596: Calling super from ModelForm.clean() should be
 
67
        optional.
 
68
        """
 
69
        form = TripleFormWithCleanOverride({'left': 1, 'middle': 2, 'right': 1})
 
70
        self.failUnless(form.is_valid())
 
71
        # form.instance.left will be None if the instance was not constructed
 
72
        # by form.full_clean().
 
73
        self.assertEquals(form.instance.left, 1)
 
74
 
 
75
# Regression test for #12960.
 
76
# Make sure the cleaned_data returned from ModelForm.clean() is applied to the
 
77
# model instance.
 
78
 
 
79
class PublicationForm(forms.ModelForm):
 
80
    def clean(self):
 
81
        self.cleaned_data['title'] = self.cleaned_data['title'].upper()
 
82
        return self.cleaned_data
 
83
 
 
84
    class Meta:
 
85
        model = Publication
 
86
 
 
87
class ModelFormCleanTest(TestCase):
 
88
    def test_model_form_clean_applies_to_model(self):
 
89
        data = {'title': 'test', 'date_published': '2010-2-25'}
 
90
        form = PublicationForm(data)
 
91
        publication = form.save()
 
92
        self.assertEqual(publication.title, 'TEST')
 
93
 
53
94
class FPForm(forms.ModelForm):
54
95
    class Meta:
55
96
        model = FilePathModel
96
137
class CustomFieldSaveTests(TestCase):
97
138
    def test_save(self):
98
139
        "Regression for #11149: save_form_data should be called only once"
99
 
        
 
140
 
100
141
        # It's enough that the form saves without error -- the custom save routine will
101
142
        # generate an AssertionError if it is called more than once during save.
102
143
        form = CFFForm(data = {'f': None})
103
 
        form.save()
 
 
b'\\ No newline at end of file'
 
144
        form.save()
 
145
 
 
146
class ModelChoiceIteratorTests(TestCase):
 
147
    def test_len(self):
 
148
        class Form(forms.ModelForm):
 
149
            class Meta:
 
150
                model = Article
 
151
                fields = ["publications"]
 
152
 
 
153
        Publication.objects.create(title="Pravda",
 
154
            date_published=date(1991, 8, 22))
 
155
        f = Form()
 
156
        self.assertEqual(len(f.fields["publications"].choices), 1)
 
157
 
 
158
class RealPersonForm(forms.ModelForm):
 
159
    class Meta:
 
160
        model = RealPerson
 
161
 
 
162
class CustomModelFormSaveMethod(TestCase):
 
163
    def test_string_message(self):
 
164
        data = {'name': 'anonymous'}
 
165
        form = RealPersonForm(data)
 
166
        self.assertEqual(form.is_valid(), False)
 
167
        self.assertEqual(form.errors['__all__'], ['Please specify a real name.'])
 
168
 
 
169
class ModelClassTests(TestCase):
 
170
    def test_no_model_class(self):
 
171
        class NoModelModelForm(forms.ModelForm):
 
172
            pass
 
173
        self.assertRaises(ValueError, NoModelModelForm)
 
174
 
 
175
class OneToOneFieldTests(TestCase):
 
176
    def test_assignment_of_none(self):
 
177
        class AuthorForm(forms.ModelForm):
 
178
            class Meta:
 
179
                model = Author
 
180
                fields = ['publication', 'full_name']
 
181
 
 
182
        publication = Publication.objects.create(title="Pravda",
 
183
            date_published=date(1991, 8, 22))
 
184
        author = Author.objects.create(publication=publication, full_name='John Doe')
 
185
        form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
 
186
        self.assert_(form.is_valid())
 
187
        self.assertEqual(form.cleaned_data['publication'], None)
 
188
        author = form.save()
 
189
        # author object returned from form still retains original publication object
 
190
        # that's why we need to retreive it from database again
 
191
        new_author = Author.objects.get(pk=author.pk)
 
192
        self.assertEqual(new_author.publication, None)
 
193
 
 
194
    def test_assignment_of_none_null_false(self):
 
195
        class AuthorForm(forms.ModelForm):
 
196
            class Meta:
 
197
                model = Author1
 
198
                fields = ['publication', 'full_name']
 
199
 
 
200
        publication = Publication.objects.create(title="Pravda",
 
201
            date_published=date(1991, 8, 22))
 
202
        author = Author1.objects.create(publication=publication, full_name='John Doe')
 
203
        form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
 
204
        self.assert_(not form.is_valid())
 
205
 
 
206
 
 
207
class ModelChoiceForm(forms.Form):
 
208
    person = ModelChoiceField(Person.objects.all())
 
209
 
 
210
 
 
211
class TestTicket11183(TestCase):
 
212
    def test_11183(self):
 
213
        form1 = ModelChoiceForm()
 
214
        field1 = form1.fields['person']
 
215
        # To allow the widget to change the queryset of field1.widget.choices correctly, 
 
216
        # without affecting other forms, the following must hold:
 
217
        self.assert_(field1 is not ModelChoiceForm.base_fields['person'])
 
218
        self.assert_(field1.widget.choices.field is field1)