2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
from django import forms
|
|
|
|
from django.core.exceptions import NON_FIELD_ERRORS
|
2011-10-13 18:04:12 +00:00
|
|
|
from django.test import TestCase
|
2015-04-27 16:22:48 +00:00
|
|
|
from django.utils.functional import lazy
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from . import ValidationTestCase
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import (
|
|
|
|
Article, Author, GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest,
|
|
|
|
ModelToValidate,
|
|
|
|
)
|
2011-10-31 11:09:10 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
class BaseModelValidationTests(ValidationTestCase):
|
|
|
|
|
|
|
|
def test_missing_required_field_raises_error(self):
|
|
|
|
mtv = ModelToValidate(f_with_custom_validator=42)
|
2010-01-12 02:29:45 +00:00
|
|
|
self.assertFailsValidation(mtv.full_clean, ['name', 'number'])
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_with_correct_value_model_validates(self):
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIsNone(mtv.full_clean())
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
def test_custom_validate_method(self):
|
2010-01-05 03:56:19 +00:00
|
|
|
mtv = ModelToValidate(number=11)
|
2010-01-12 02:29:45 +00:00
|
|
|
self.assertFailsValidation(mtv.full_clean, [NON_FIELD_ERRORS, 'name'])
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_wrong_FK_value_raises_error(self):
|
2013-10-23 10:09:29 +00:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', parent_id=3)
|
2014-07-02 04:43:07 +00:00
|
|
|
self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'parent',
|
|
|
|
['model to validate instance with id %r does not exist.' % mtv.parent_id])
|
|
|
|
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', ufm_id='Some Name')
|
|
|
|
self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'ufm',
|
|
|
|
["unique fields model instance with unique_charfield %r does not exist." % mtv.name])
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_correct_FK_value_validates(self):
|
|
|
|
parent = ModelToValidate.objects.create(number=10, name='Some Name')
|
2010-01-21 03:26:14 +00:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIsNone(mtv.full_clean())
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2010-09-13 05:08:21 +00:00
|
|
|
def test_limited_FK_raises_error(self):
|
2010-01-21 03:26:14 +00:00
|
|
|
# The limit_choices_to on the parent field says that a parent object's
|
|
|
|
# number attribute must be 10, so this should fail validation.
|
|
|
|
parent = ModelToValidate.objects.create(number=11, name='Other Name')
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
|
|
|
|
self.assertFailsValidation(mtv.full_clean, ['parent'])
|
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
def test_wrong_email_value_raises_error(self):
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', email='not-an-email')
|
2010-01-12 02:29:45 +00:00
|
|
|
self.assertFailsValidation(mtv.full_clean, ['email'])
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_correct_email_value_passes(self):
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIsNone(mtv.full_clean())
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_wrong_url_value_raises_error(self):
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', url='not a url')
|
2013-03-14 14:25:27 +00:00
|
|
|
self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', ['Enter a valid URL.'])
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2014-11-03 22:48:03 +00:00
|
|
|
def test_text_greater_that_charfields_max_length_raises_errors(self):
|
2013-11-02 21:34:05 +00:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name' * 100)
|
2013-06-13 16:37:08 +00:00
|
|
|
self.assertFailsValidation(mtv.full_clean, ['name'])
|
|
|
|
|
|
|
|
def test_malformed_slug_raises_error(self):
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', slug='##invalid##')
|
|
|
|
self.assertFailsValidation(mtv.full_clean, ['slug'])
|
2010-01-12 02:29:45 +00:00
|
|
|
|
2014-05-01 09:55:52 +00:00
|
|
|
def test_full_clean_does_not_mutate_exclude(self):
|
|
|
|
mtv = ModelToValidate(f_with_custom_validator=42)
|
|
|
|
exclude = ['number']
|
|
|
|
self.assertFailsValidation(mtv.full_clean, ['name'], exclude=exclude)
|
|
|
|
self.assertEqual(len(exclude), 1)
|
|
|
|
self.assertEqual(exclude[0], 'number')
|
|
|
|
|
2011-06-11 13:48:24 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
class ArticleForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
exclude = ['author']
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
class ModelFormsTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.author = Author.objects.create(name='Joseph Kocherhans')
|
|
|
|
|
|
|
|
def test_partial_validation(self):
|
|
|
|
# Make sure the "commit=False and set field values later" idiom still
|
|
|
|
# works with model validation.
|
|
|
|
data = {
|
|
|
|
'title': 'The state of model validation',
|
|
|
|
'pub_date': '2010-1-10 14:49:00'
|
|
|
|
}
|
|
|
|
form = ArticleForm(data)
|
2012-08-15 10:02:32 +00:00
|
|
|
self.assertEqual(list(form.errors), [])
|
2010-01-12 02:29:45 +00:00
|
|
|
article = form.save(commit=False)
|
|
|
|
article.author = self.author
|
|
|
|
article.save()
|
|
|
|
|
|
|
|
def test_validation_with_empty_blank_field(self):
|
|
|
|
# Since a value for pub_date wasn't provided and the field is
|
|
|
|
# blank=True, model-validation should pass.
|
|
|
|
# Also, Article.clean() should be run, so pub_date will be filled after
|
|
|
|
# validation, so the form should save cleanly even though pub_date is
|
|
|
|
# not allowed to be null.
|
|
|
|
data = {
|
|
|
|
'title': 'The state of model validation',
|
|
|
|
}
|
|
|
|
article = Article(author_id=self.author.id)
|
|
|
|
form = ArticleForm(data, instance=article)
|
2012-08-15 10:02:32 +00:00
|
|
|
self.assertEqual(list(form.errors), [])
|
2010-01-12 02:29:45 +00:00
|
|
|
self.assertNotEqual(form.instance.pub_date, None)
|
|
|
|
article = form.save()
|
|
|
|
|
|
|
|
def test_validation_with_invalid_blank_field(self):
|
|
|
|
# Even though pub_date is set to blank=True, an invalid value was
|
|
|
|
# provided, so it should fail validation.
|
|
|
|
data = {
|
|
|
|
'title': 'The state of model validation',
|
|
|
|
'pub_date': 'never'
|
|
|
|
}
|
|
|
|
article = Article(author_id=self.author.id)
|
|
|
|
form = ArticleForm(data, instance=article)
|
2012-08-15 10:02:32 +00:00
|
|
|
self.assertEqual(list(form.errors), ['pub_date'])
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenericIPAddressFieldTests(ValidationTestCase):
|
|
|
|
|
|
|
|
def test_correct_generic_ip_passes(self):
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="1.2.3.4")
|
2013-05-28 19:11:41 +00:00
|
|
|
self.assertIsNone(giptm.full_clean())
|
2015-04-27 16:22:48 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip=" 1.2.3.4 ")
|
|
|
|
self.assertIsNone(giptm.full_clean())
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="1.2.3.4\n")
|
|
|
|
self.assertIsNone(giptm.full_clean())
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="2001::2")
|
2013-05-28 19:11:41 +00:00
|
|
|
self.assertIsNone(giptm.full_clean())
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_invalid_generic_ip_raises_error(self):
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="294.4.2.1")
|
2013-10-26 19:15:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="1:2")
|
2013-10-26 19:15:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_ip'])
|
2015-04-27 16:22:48 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip=1)
|
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_ip'])
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip=lazy(lambda: 1, int))
|
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_correct_v4_ip_passes(self):
|
|
|
|
giptm = GenericIPAddressTestModel(v4_ip="1.2.3.4")
|
2013-05-28 19:11:41 +00:00
|
|
|
self.assertIsNone(giptm.full_clean())
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_invalid_v4_ip_raises_error(self):
|
|
|
|
giptm = GenericIPAddressTestModel(v4_ip="294.4.2.1")
|
2013-10-24 17:30:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['v4_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm = GenericIPAddressTestModel(v4_ip="2001::2")
|
2013-10-24 17:30:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['v4_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_correct_v6_ip_passes(self):
|
|
|
|
giptm = GenericIPAddressTestModel(v6_ip="2001::2")
|
2013-05-28 19:11:41 +00:00
|
|
|
self.assertIsNone(giptm.full_clean())
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_invalid_v6_ip_raises_error(self):
|
|
|
|
giptm = GenericIPAddressTestModel(v6_ip="1.2.3.4")
|
2013-10-24 17:30:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['v6_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm = GenericIPAddressTestModel(v6_ip="1:2")
|
2013-10-24 17:30:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['v6_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_v6_uniqueness_detection(self):
|
|
|
|
# These two addresses are the same with different syntax
|
2013-02-23 17:42:53 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="2001::1:0:0:0:0:2")
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm.save()
|
2013-02-23 17:42:53 +00:00
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2")
|
2013-10-24 17:30:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_ip'])
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
def test_v4_unpack_uniqueness_detection(self):
|
|
|
|
# These two are different, because we are not doing IPv4 unpacking
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="::ffff:10.10.10.10")
|
|
|
|
giptm.save()
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="10.10.10.10")
|
2013-05-28 19:11:41 +00:00
|
|
|
self.assertIsNone(giptm.full_clean())
|
2011-06-11 13:48:24 +00:00
|
|
|
|
|
|
|
# These two are the same, because we are doing IPv4 unpacking
|
2011-06-13 17:48:02 +00:00
|
|
|
giptm = GenericIPAddrUnpackUniqueTest(generic_v4unpack_ip="::ffff:18.52.18.52")
|
2011-06-11 13:48:24 +00:00
|
|
|
giptm.save()
|
2011-06-13 17:48:02 +00:00
|
|
|
giptm = GenericIPAddrUnpackUniqueTest(generic_v4unpack_ip="18.52.18.52")
|
2013-10-26 19:15:03 +00:00
|
|
|
self.assertFailsValidation(giptm.full_clean, ['generic_v4unpack_ip'])
|
2013-05-28 19:11:41 +00:00
|
|
|
|
|
|
|
def test_empty_generic_ip_passes(self):
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip="")
|
|
|
|
self.assertIsNone(giptm.full_clean())
|
|
|
|
giptm = GenericIPAddressTestModel(generic_ip=None)
|
|
|
|
self.assertIsNone(giptm.full_clean())
|