PageRenderTime 364ms CodeModel.GetById 95ms app.highlight 160ms RepoModel.GetById 67ms app.codeStats 1ms

/tests/regressiontests/forms/tests/models.py

https://code.google.com/p/mango-py/
Python | 161 lines | 148 code | 10 blank | 3 comment | 3 complexity | 4661a4199e39432daa89eeac0e682b0a MD5 | raw file
  1# -*- coding: utf-8 -*-
  2import datetime
  3from django.core.files.uploadedfile import SimpleUploadedFile
  4from django.forms import Form, ModelForm, FileField, ModelChoiceField
  5from django.test import TestCase
  6from regressiontests.forms.models import ChoiceModel, ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, BoundaryModel, Defaults
  7
  8
  9class ChoiceFieldForm(ModelForm):
 10    class Meta:
 11        model = ChoiceFieldModel
 12
 13
 14class FileForm(Form):
 15    file1 = FileField()
 16
 17
 18class TestTicket12510(TestCase):
 19    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
 20    def setUp(self):
 21        self.groups = [Group.objects.create(name=name) for name in 'abc']
 22
 23    def test_choices_not_fetched_when_not_rendering(self):
 24        def test():
 25            field = ModelChoiceField(Group.objects.order_by('-name'))
 26            self.assertEqual('a', field.clean(self.groups[0].pk).name)
 27        # only one query is required to pull the model from DB
 28        self.assertNumQueries(1, test)
 29
 30class ModelFormCallableModelDefault(TestCase):
 31    def test_no_empty_option(self):
 32        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
 33        option = ChoiceOptionModel.objects.create(name='default')
 34
 35        choices = list(ChoiceFieldForm().fields['choice'].choices)
 36        self.assertEqual(len(choices), 1)
 37        self.assertEqual(choices[0], (option.pk, unicode(option)))
 38
 39    def test_callable_initial_value(self):
 40        "The initial value for a callable default returning a queryset is the pk (refs #13769)"
 41        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
 42        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
 43        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
 44        self.assertEqual(ChoiceFieldForm().as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
 45<option value="1" selected="selected">ChoiceOption 1</option>
 46<option value="2">ChoiceOption 2</option>
 47<option value="3">ChoiceOption 3</option>
 48</select><input type="hidden" name="initial-choice" value="1" id="initial-id_choice" /></p>
 49<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
 50<option value="1" selected="selected">ChoiceOption 1</option>
 51<option value="2">ChoiceOption 2</option>
 52<option value="3">ChoiceOption 3</option>
 53</select><input type="hidden" name="initial-choice_int" value="1" id="initial-id_choice_int" /></p>
 54<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
 55<option value="1" selected="selected">ChoiceOption 1</option>
 56<option value="2">ChoiceOption 2</option>
 57<option value="3">ChoiceOption 3</option>
 58</select><input type="hidden" name="initial-multi_choice" value="1" id="initial-id_multi_choice_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
 59<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
 60<option value="1" selected="selected">ChoiceOption 1</option>
 61<option value="2">ChoiceOption 2</option>
 62<option value="3">ChoiceOption 3</option>
 63</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
 64
 65    def test_initial_instance_value(self):
 66        "Initial instances for model fields may also be instances (refs #7287)"
 67        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
 68        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
 69        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
 70        self.assertEqual(ChoiceFieldForm(initial={
 71                'choice': obj2,
 72                'choice_int': obj2,
 73                'multi_choice': [obj2,obj3],
 74                'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
 75            }).as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
 76<option value="1">ChoiceOption 1</option>
 77<option value="2" selected="selected">ChoiceOption 2</option>
 78<option value="3">ChoiceOption 3</option>
 79</select><input type="hidden" name="initial-choice" value="2" id="initial-id_choice" /></p>
 80<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
 81<option value="1">ChoiceOption 1</option>
 82<option value="2" selected="selected">ChoiceOption 2</option>
 83<option value="3">ChoiceOption 3</option>
 84</select><input type="hidden" name="initial-choice_int" value="2" id="initial-id_choice_int" /></p>
 85<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
 86<option value="1">ChoiceOption 1</option>
 87<option value="2" selected="selected">ChoiceOption 2</option>
 88<option value="3" selected="selected">ChoiceOption 3</option>
 89</select><input type="hidden" name="initial-multi_choice" value="2" id="initial-id_multi_choice_0" />
 90<input type="hidden" name="initial-multi_choice" value="3" id="initial-id_multi_choice_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
 91<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
 92<option value="1">ChoiceOption 1</option>
 93<option value="2" selected="selected">ChoiceOption 2</option>
 94<option value="3" selected="selected">ChoiceOption 3</option>
 95</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
 96<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
 97
 98
 99
100class FormsModelTestCase(TestCase):
101    def test_unicode_filename(self):
102        # FileModel with unicode filename and data #########################
103        f = FileForm(data={}, files={'file1': SimpleUploadedFile('?????????.txt', '???? ??????? ???? ??? ????????? ?? ??? ?')}, auto_id=False)
104        self.assertTrue(f.is_valid())
105        self.assertTrue('file1' in f.cleaned_data)
106        m = FileModel.objects.create(file=f.cleaned_data['file1'])
107        self.assertEqual(m.file.name, u'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
108        m.delete()
109
110    def test_boundary_conditions(self):
111        # Boundary conditions on a PostitiveIntegerField #########################
112        class BoundaryForm(ModelForm):
113            class Meta:
114                model = BoundaryModel
115
116        f = BoundaryForm({'positive_integer': 100})
117        self.assertTrue(f.is_valid())
118        f = BoundaryForm({'positive_integer': 0})
119        self.assertTrue(f.is_valid())
120        f = BoundaryForm({'positive_integer': -100})
121        self.assertFalse(f.is_valid())
122
123    def test_formfield_initial(self):
124        # Formfield initial values ########
125        # If the model has default values for some fields, they are used as the formfield
126        # initial values.
127        class DefaultsForm(ModelForm):
128            class Meta:
129                model = Defaults
130
131        self.assertEqual(DefaultsForm().fields['name'].initial, u'class default value')
132        self.assertEqual(DefaultsForm().fields['def_date'].initial, datetime.date(1980, 1, 1))
133        self.assertEqual(DefaultsForm().fields['value'].initial, 42)
134        r1 = DefaultsForm()['callable_default'].as_widget()
135        r2 = DefaultsForm()['callable_default'].as_widget()
136        self.assertNotEqual(r1, r2)
137
138        # In a ModelForm that is passed an instance, the initial values come from the
139        # instance's values, not the model's defaults.
140        foo_instance = Defaults(name=u'instance value', def_date=datetime.date(1969, 4, 4), value=12)
141        instance_form = DefaultsForm(instance=foo_instance)
142        self.assertEqual(instance_form.initial['name'], u'instance value')
143        self.assertEqual(instance_form.initial['def_date'], datetime.date(1969, 4, 4))
144        self.assertEqual(instance_form.initial['value'], 12)
145
146        from django.forms import CharField
147
148        class ExcludingForm(ModelForm):
149            name = CharField(max_length=255)
150
151            class Meta:
152                model = Defaults
153                exclude = ['name', 'callable_default']
154
155        f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
156        self.assertTrue(f.is_valid())
157        self.assertEqual(f.cleaned_data['name'], u'Hello')
158        obj = f.save()
159        self.assertEqual(obj.name, u'class default value')
160        self.assertEqual(obj.value, 99)
161        self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))