summaryrefslogtreecommitdiff
path: root/parts/django/tests/regressiontests/model_forms_regress/tests.py
diff options
context:
space:
mode:
authorNishanth Amuluru2011-01-08 11:20:57 +0530
committerNishanth Amuluru2011-01-08 11:20:57 +0530
commit65411d01d448ff0cd4abd14eee14cf60b5f8fc20 (patch)
treeb4c404363c4c63a61d6e2f8bd26c5b057c1fb09d /parts/django/tests/regressiontests/model_forms_regress/tests.py
parent2e35094d43b4cc6974172e1febf76abb50f086ec (diff)
downloadpytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.gz
pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.bz2
pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.zip
Added buildout stuff and made changes accordingly
--HG-- rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/dependency_links.txt rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/not-zip-safe rename : profile/management/__init__.py => parts/django/Django.egg-info/dependency_links.txt rename : taskapp/models.py => parts/django/django/conf/app_template/models.py rename : taskapp/tests.py => parts/django/django/conf/app_template/tests.py rename : taskapp/views.py => parts/django/django/conf/app_template/views.py rename : taskapp/views.py => parts/django/django/contrib/gis/tests/geo3d/views.py rename : profile/management/__init__.py => parts/django/tests/modeltests/delete/__init__.py rename : profile/management/__init__.py => parts/django/tests/modeltests/files/__init__.py rename : profile/management/__init__.py => parts/django/tests/modeltests/invalid_models/__init__.py rename : profile/management/__init__.py => parts/django/tests/modeltests/m2m_signals/__init__.py rename : profile/management/__init__.py => parts/django/tests/modeltests/model_package/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/commands/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/models.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/delete_regress/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/file_storage/__init__.py rename : profile/management/__init__.py => parts/django/tests/regressiontests/max_lengths/__init__.py rename : profile/forms.py => pytask/profile/forms.py rename : profile/management/__init__.py => pytask/profile/management/__init__.py rename : profile/management/commands/seed_db.py => pytask/profile/management/commands/seed_db.py rename : profile/models.py => pytask/profile/models.py rename : profile/templatetags/user_tags.py => pytask/profile/templatetags/user_tags.py rename : taskapp/tests.py => pytask/profile/tests.py rename : profile/urls.py => pytask/profile/urls.py rename : profile/utils.py => pytask/profile/utils.py rename : profile/views.py => pytask/profile/views.py rename : static/css/base.css => pytask/static/css/base.css rename : taskapp/tests.py => pytask/taskapp/tests.py rename : taskapp/views.py => pytask/taskapp/views.py rename : templates/base.html => pytask/templates/base.html rename : templates/profile/browse_notifications.html => pytask/templates/profile/browse_notifications.html rename : templates/profile/edit.html => pytask/templates/profile/edit.html rename : templates/profile/view.html => pytask/templates/profile/view.html rename : templates/profile/view_notification.html => pytask/templates/profile/view_notification.html rename : templates/registration/activate.html => pytask/templates/registration/activate.html rename : templates/registration/activation_email.txt => pytask/templates/registration/activation_email.txt rename : templates/registration/activation_email_subject.txt => pytask/templates/registration/activation_email_subject.txt rename : templates/registration/logged_out.html => pytask/templates/registration/logged_out.html rename : templates/registration/login.html => pytask/templates/registration/login.html rename : templates/registration/logout.html => pytask/templates/registration/logout.html rename : templates/registration/password_change_done.html => pytask/templates/registration/password_change_done.html rename : templates/registration/password_change_form.html => pytask/templates/registration/password_change_form.html rename : templates/registration/password_reset_complete.html => pytask/templates/registration/password_reset_complete.html rename : templates/registration/password_reset_confirm.html => pytask/templates/registration/password_reset_confirm.html rename : templates/registration/password_reset_done.html => pytask/templates/registration/password_reset_done.html rename : templates/registration/password_reset_email.html => pytask/templates/registration/password_reset_email.html rename : templates/registration/password_reset_form.html => pytask/templates/registration/password_reset_form.html rename : templates/registration/registration_complete.html => pytask/templates/registration/registration_complete.html rename : templates/registration/registration_form.html => pytask/templates/registration/registration_form.html rename : utils.py => pytask/utils.py
Diffstat (limited to 'parts/django/tests/regressiontests/model_forms_regress/tests.py')
-rw-r--r--parts/django/tests/regressiontests/model_forms_regress/tests.py311
1 files changed, 311 insertions, 0 deletions
diff --git a/parts/django/tests/regressiontests/model_forms_regress/tests.py b/parts/django/tests/regressiontests/model_forms_regress/tests.py
new file mode 100644
index 0000000..1d0d6ed
--- /dev/null
+++ b/parts/django/tests/regressiontests/model_forms_regress/tests.py
@@ -0,0 +1,311 @@
+from datetime import date
+
+from django import db, forms
+from django.conf import settings
+from django.forms.models import modelform_factory, ModelChoiceField
+from django.test import TestCase
+
+from models import Person, RealPerson, Triple, FilePathModel, Article, \
+ Publication, CustomFF, Author, Author1, Homepage
+
+
+class ModelMultipleChoiceFieldTests(TestCase):
+
+ def setUp(self):
+ self.old_debug = settings.DEBUG
+ settings.DEBUG = True
+
+ def tearDown(self):
+ settings.DEBUG = self.old_debug
+
+ def test_model_multiple_choice_number_of_queries(self):
+ """
+ Test that ModelMultipleChoiceField does O(1) queries instead of
+ O(n) (#10156).
+ """
+ for i in range(30):
+ Person.objects.create(name="Person %s" % i)
+
+ db.reset_queries()
+ f = forms.ModelMultipleChoiceField(queryset=Person.objects.all())
+ selected = f.clean([1, 3, 5, 7, 9])
+ self.assertEquals(len(db.connection.queries), 1)
+
+ def test_model_multiple_choice_run_validators(self):
+ """
+ Test that ModelMultipleChoiceField run given validators (#14144).
+ """
+ for i in range(30):
+ Person.objects.create(name="Person %s" % i)
+
+ self._validator_run = False
+ def my_validator(value):
+ self._validator_run = True
+
+ f = forms.ModelMultipleChoiceField(queryset=Person.objects.all(),
+ validators=[my_validator])
+
+ f.clean([p.pk for p in Person.objects.all()[8:9]])
+ self.assertTrue(self._validator_run)
+
+class TripleForm(forms.ModelForm):
+ class Meta:
+ model = Triple
+
+class UniqueTogetherTests(TestCase):
+ def test_multiple_field_unique_together(self):
+ """
+ When the same field is involved in multiple unique_together
+ constraints, we need to make sure we don't remove the data for it
+ before doing all the validation checking (not just failing after
+ the first one).
+ """
+ Triple.objects.create(left=1, middle=2, right=3)
+
+ form = TripleForm({'left': '1', 'middle': '2', 'right': '3'})
+ self.assertFalse(form.is_valid())
+
+ form = TripleForm({'left': '1', 'middle': '3', 'right': '1'})
+ self.assertTrue(form.is_valid())
+
+class TripleFormWithCleanOverride(forms.ModelForm):
+ class Meta:
+ model = Triple
+
+ def clean(self):
+ if not self.cleaned_data['left'] == self.cleaned_data['right']:
+ raise forms.ValidationError('Left and right should be equal')
+ return self.cleaned_data
+
+class OverrideCleanTests(TestCase):
+ def test_override_clean(self):
+ """
+ Regression for #12596: Calling super from ModelForm.clean() should be
+ optional.
+ """
+ form = TripleFormWithCleanOverride({'left': 1, 'middle': 2, 'right': 1})
+ self.assertTrue(form.is_valid())
+ # form.instance.left will be None if the instance was not constructed
+ # by form.full_clean().
+ self.assertEquals(form.instance.left, 1)
+
+# Regression test for #12960.
+# Make sure the cleaned_data returned from ModelForm.clean() is applied to the
+# model instance.
+
+class PublicationForm(forms.ModelForm):
+ def clean(self):
+ self.cleaned_data['title'] = self.cleaned_data['title'].upper()
+ return self.cleaned_data
+
+ class Meta:
+ model = Publication
+
+class ModelFormCleanTest(TestCase):
+ def test_model_form_clean_applies_to_model(self):
+ data = {'title': 'test', 'date_published': '2010-2-25'}
+ form = PublicationForm(data)
+ publication = form.save()
+ self.assertEqual(publication.title, 'TEST')
+
+class FPForm(forms.ModelForm):
+ class Meta:
+ model = FilePathModel
+
+class FilePathFieldTests(TestCase):
+ def test_file_path_field_blank(self):
+ """
+ Regression test for #8842: FilePathField(blank=True)
+ """
+ form = FPForm()
+ names = [p[1] for p in form['path'].field.choices]
+ names.sort()
+ self.assertEqual(names, ['---------', '__init__.py', 'models.py', 'tests.py'])
+
+class ManyToManyCallableInitialTests(TestCase):
+ def test_callable(self):
+ "Regression for #10349: A callable can be provided as the initial value for an m2m field"
+
+ # Set up a callable initial value
+ def formfield_for_dbfield(db_field, **kwargs):
+ if db_field.name == 'publications':
+ kwargs['initial'] = lambda: Publication.objects.all().order_by('date_published')[:2]
+ return db_field.formfield(**kwargs)
+
+ # Set up some Publications to use as data
+ Publication(title="First Book", date_published=date(2007,1,1)).save()
+ Publication(title="Second Book", date_published=date(2008,1,1)).save()
+ Publication(title="Third Book", date_published=date(2009,1,1)).save()
+
+ # Create a ModelForm, instantiate it, and check that the output is as expected
+ ModelForm = modelform_factory(Article, formfield_callback=formfield_for_dbfield)
+ form = ModelForm()
+ self.assertEquals(form.as_ul(), u"""<li><label for="id_headline">Headline:</label> <input id="id_headline" type="text" name="headline" maxlength="100" /></li>
+<li><label for="id_publications">Publications:</label> <select multiple="multiple" name="publications" id="id_publications">
+<option value="1" selected="selected">First Book</option>
+<option value="2" selected="selected">Second Book</option>
+<option value="3">Third Book</option>
+</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>""")
+
+class CFFForm(forms.ModelForm):
+ class Meta:
+ model = CustomFF
+
+class CustomFieldSaveTests(TestCase):
+ def test_save(self):
+ "Regression for #11149: save_form_data should be called only once"
+
+ # It's enough that the form saves without error -- the custom save routine will
+ # generate an AssertionError if it is called more than once during save.
+ form = CFFForm(data = {'f': None})
+ form.save()
+
+class ModelChoiceIteratorTests(TestCase):
+ def test_len(self):
+ class Form(forms.ModelForm):
+ class Meta:
+ model = Article
+ fields = ["publications"]
+
+ Publication.objects.create(title="Pravda",
+ date_published=date(1991, 8, 22))
+ f = Form()
+ self.assertEqual(len(f.fields["publications"].choices), 1)
+
+class RealPersonForm(forms.ModelForm):
+ class Meta:
+ model = RealPerson
+
+class CustomModelFormSaveMethod(TestCase):
+ def test_string_message(self):
+ data = {'name': 'anonymous'}
+ form = RealPersonForm(data)
+ self.assertEqual(form.is_valid(), False)
+ self.assertEqual(form.errors['__all__'], ['Please specify a real name.'])
+
+class ModelClassTests(TestCase):
+ def test_no_model_class(self):
+ class NoModelModelForm(forms.ModelForm):
+ pass
+ self.assertRaises(ValueError, NoModelModelForm)
+
+class OneToOneFieldTests(TestCase):
+ def test_assignment_of_none(self):
+ class AuthorForm(forms.ModelForm):
+ class Meta:
+ model = Author
+ fields = ['publication', 'full_name']
+
+ publication = Publication.objects.create(title="Pravda",
+ date_published=date(1991, 8, 22))
+ author = Author.objects.create(publication=publication, full_name='John Doe')
+ form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
+ self.assert_(form.is_valid())
+ self.assertEqual(form.cleaned_data['publication'], None)
+ author = form.save()
+ # author object returned from form still retains original publication object
+ # that's why we need to retreive it from database again
+ new_author = Author.objects.get(pk=author.pk)
+ self.assertEqual(new_author.publication, None)
+
+ def test_assignment_of_none_null_false(self):
+ class AuthorForm(forms.ModelForm):
+ class Meta:
+ model = Author1
+ fields = ['publication', 'full_name']
+
+ publication = Publication.objects.create(title="Pravda",
+ date_published=date(1991, 8, 22))
+ author = Author1.objects.create(publication=publication, full_name='John Doe')
+ form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
+ self.assert_(not form.is_valid())
+
+
+class ModelChoiceForm(forms.Form):
+ person = ModelChoiceField(Person.objects.all())
+
+
+class TestTicket11183(TestCase):
+ def test_11183(self):
+ form1 = ModelChoiceForm()
+ field1 = form1.fields['person']
+ # To allow the widget to change the queryset of field1.widget.choices correctly,
+ # without affecting other forms, the following must hold:
+ self.assert_(field1 is not ModelChoiceForm.base_fields['person'])
+ self.assert_(field1.widget.choices.field is field1)
+
+class HomepageForm(forms.ModelForm):
+ class Meta:
+ model = Homepage
+
+class URLFieldTests(TestCase):
+ def test_url_on_modelform(self):
+ "Check basic URL field validation on model forms"
+ self.assertFalse(HomepageForm({'url': 'foo'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://example'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://example.'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://com.'}).is_valid())
+
+ self.assertTrue(HomepageForm({'url': 'http://localhost'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://example.com'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com/test'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000/test'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://example.com/foo/bar'}).is_valid())
+
+ def test_http_prefixing(self):
+ "If the http:// prefix is omitted on form input, the field adds it again. (Refs #13613)"
+ form = HomepageForm({'url': 'example.com'})
+ form.is_valid()
+ # self.assertTrue(form.is_valid())
+ # self.assertEquals(form.cleaned_data['url'], 'http://example.com/')
+
+ form = HomepageForm({'url': 'example.com/test'})
+ form.is_valid()
+ # self.assertTrue(form.is_valid())
+ # self.assertEquals(form.cleaned_data['url'], 'http://example.com/test')
+
+
+class FormFieldCallbackTests(TestCase):
+
+ def test_baseform_with_widgets_in_meta(self):
+ """Regression for #13095: Using base forms with widgets defined in Meta should not raise errors."""
+ widget = forms.Textarea()
+
+ class BaseForm(forms.ModelForm):
+ class Meta:
+ model = Person
+ widgets = {'name': widget}
+
+ Form = modelform_factory(Person, form=BaseForm)
+ self.assertTrue(Form.base_fields['name'].widget is widget)
+
+ def test_custom_callback(self):
+ """Test that a custom formfield_callback is used if provided"""
+
+ callback_args = []
+
+ def callback(db_field, **kwargs):
+ callback_args.append((db_field, kwargs))
+ return db_field.formfield(**kwargs)
+
+ widget = forms.Textarea()
+
+ class BaseForm(forms.ModelForm):
+ class Meta:
+ model = Person
+ widgets = {'name': widget}
+
+ _ = modelform_factory(Person, form=BaseForm,
+ formfield_callback=callback)
+ id_field, name_field = Person._meta.fields
+
+ self.assertEqual(callback_args,
+ [(id_field, {}), (name_field, {'widget': widget})])
+
+ def test_bad_callback(self):
+ # A bad callback provided by user still gives an error
+ self.assertRaises(TypeError, modelform_factory, Person,
+ formfield_callback='not a function or callable')