App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / modeltests / validation / tests.py
blob5c94f439d16ddb00a45b0abf840d429f70f3b5da
1 from __future__ import absolute_import
3 import warnings
5 from django import forms
6 from django.core.exceptions import NON_FIELD_ERRORS
7 from django.test import TestCase
9 # Import the verify_exists_urls from the 'forms' test app
10 from regressiontests.forms.tests.fields import verify_exists_urls
12 from . import ValidationTestCase
13 from .models import (Author, Article, ModelToValidate,
14 GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
16 # Import other tests for this package.
17 from .test_custom_messages import CustomMessagesTest
18 from .test_error_messages import ValidationMessagesTest
19 from .test_unique import GetUniqueCheckTests, PerformUniqueChecksTest
20 from .validators import TestModelsWithValidators
23 class BaseModelValidationTests(ValidationTestCase):
25 def setUp(self):
26 self.save_warnings_state()
27 warnings.filterwarnings('ignore', category=DeprecationWarning,
28 module='django.core.validators')
30 def tearDown(self):
31 self.restore_warnings_state()
33 def test_missing_required_field_raises_error(self):
34 mtv = ModelToValidate(f_with_custom_validator=42)
35 self.assertFailsValidation(mtv.full_clean, ['name', 'number'])
37 def test_with_correct_value_model_validates(self):
38 mtv = ModelToValidate(number=10, name='Some Name')
39 self.assertEqual(None, mtv.full_clean())
41 def test_custom_validate_method(self):
42 mtv = ModelToValidate(number=11)
43 self.assertFailsValidation(mtv.full_clean, [NON_FIELD_ERRORS, 'name'])
45 def test_wrong_FK_value_raises_error(self):
46 mtv=ModelToValidate(number=10, name='Some Name', parent_id=3)
47 self.assertFailsValidation(mtv.full_clean, ['parent'])
49 def test_correct_FK_value_validates(self):
50 parent = ModelToValidate.objects.create(number=10, name='Some Name')
51 mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
52 self.assertEqual(None, mtv.full_clean())
54 def test_limited_FK_raises_error(self):
55 # The limit_choices_to on the parent field says that a parent object's
56 # number attribute must be 10, so this should fail validation.
57 parent = ModelToValidate.objects.create(number=11, name='Other Name')
58 mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
59 self.assertFailsValidation(mtv.full_clean, ['parent'])
61 def test_wrong_email_value_raises_error(self):
62 mtv = ModelToValidate(number=10, name='Some Name', email='not-an-email')
63 self.assertFailsValidation(mtv.full_clean, ['email'])
65 def test_correct_email_value_passes(self):
66 mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
67 self.assertEqual(None, mtv.full_clean())
69 def test_wrong_url_value_raises_error(self):
70 mtv = ModelToValidate(number=10, name='Some Name', url='not a url')
71 self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'Enter a valid value.'])
73 #The tests below which use url_verify are deprecated
74 def test_correct_url_but_nonexisting_gives_404(self):
75 mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404')
76 self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
78 @verify_exists_urls(existing_urls=('http://www.google.com/',))
79 def test_correct_url_value_passes(self):
80 mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
81 self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
83 @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',))
84 def test_correct_url_with_redirect(self):
85 mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=301') #example.com is a redirect to iana.org now
86 self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
88 def test_correct_https_url_but_nonexisting(self):
89 mtv = ModelToValidate(number=10, name='Some Name', url_verify='https://www.example.com/')
90 self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
92 def test_text_greater_that_charfields_max_length_raises_erros(self):
93 mtv = ModelToValidate(number=10, name='Some Name'*100)
94 self.assertFailsValidation(mtv.full_clean, ['name',])
97 class ArticleForm(forms.ModelForm):
98 class Meta:
99 model = Article
100 exclude = ['author']
102 class ModelFormsTests(TestCase):
103 def setUp(self):
104 self.author = Author.objects.create(name='Joseph Kocherhans')
106 def test_partial_validation(self):
107 # Make sure the "commit=False and set field values later" idiom still
108 # works with model validation.
109 data = {
110 'title': 'The state of model validation',
111 'pub_date': '2010-1-10 14:49:00'
113 form = ArticleForm(data)
114 self.assertEqual(form.errors.keys(), [])
115 article = form.save(commit=False)
116 article.author = self.author
117 article.save()
119 def test_validation_with_empty_blank_field(self):
120 # Since a value for pub_date wasn't provided and the field is
121 # blank=True, model-validation should pass.
122 # Also, Article.clean() should be run, so pub_date will be filled after
123 # validation, so the form should save cleanly even though pub_date is
124 # not allowed to be null.
125 data = {
126 'title': 'The state of model validation',
128 article = Article(author_id=self.author.id)
129 form = ArticleForm(data, instance=article)
130 self.assertEqual(form.errors.keys(), [])
131 self.assertNotEqual(form.instance.pub_date, None)
132 article = form.save()
134 def test_validation_with_invalid_blank_field(self):
135 # Even though pub_date is set to blank=True, an invalid value was
136 # provided, so it should fail validation.
137 data = {
138 'title': 'The state of model validation',
139 'pub_date': 'never'
141 article = Article(author_id=self.author.id)
142 form = ArticleForm(data, instance=article)
143 self.assertEqual(form.errors.keys(), ['pub_date'])
146 class GenericIPAddressFieldTests(ValidationTestCase):
148 def test_correct_generic_ip_passes(self):
149 giptm = GenericIPAddressTestModel(generic_ip="1.2.3.4")
150 self.assertEqual(None, giptm.full_clean())
151 giptm = GenericIPAddressTestModel(generic_ip="2001::2")
152 self.assertEqual(None, giptm.full_clean())
154 def test_invalid_generic_ip_raises_error(self):
155 giptm = GenericIPAddressTestModel(generic_ip="294.4.2.1")
156 self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
157 giptm = GenericIPAddressTestModel(generic_ip="1:2")
158 self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
160 def test_correct_v4_ip_passes(self):
161 giptm = GenericIPAddressTestModel(v4_ip="1.2.3.4")
162 self.assertEqual(None, giptm.full_clean())
164 def test_invalid_v4_ip_raises_error(self):
165 giptm = GenericIPAddressTestModel(v4_ip="294.4.2.1")
166 self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
167 giptm = GenericIPAddressTestModel(v4_ip="2001::2")
168 self.assertFailsValidation(giptm.full_clean, ['v4_ip',])
170 def test_correct_v6_ip_passes(self):
171 giptm = GenericIPAddressTestModel(v6_ip="2001::2")
172 self.assertEqual(None, giptm.full_clean())
174 def test_invalid_v6_ip_raises_error(self):
175 giptm = GenericIPAddressTestModel(v6_ip="1.2.3.4")
176 self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
177 giptm = GenericIPAddressTestModel(v6_ip="1:2")
178 self.assertFailsValidation(giptm.full_clean, ['v6_ip',])
180 def test_v6_uniqueness_detection(self):
181 # These two addresses are the same with different syntax
182 giptm = GenericIPAddressTestModel(generic_ip="2001::1:0:0:0:0:2")
183 giptm.save()
184 giptm = GenericIPAddressTestModel(generic_ip="2001:0:1:2")
185 self.assertFailsValidation(giptm.full_clean, ['generic_ip',])
187 def test_v4_unpack_uniqueness_detection(self):
188 # These two are different, because we are not doing IPv4 unpacking
189 giptm = GenericIPAddressTestModel(generic_ip="::ffff:10.10.10.10")
190 giptm.save()
191 giptm = GenericIPAddressTestModel(generic_ip="10.10.10.10")
192 self.assertEqual(None, giptm.full_clean())
194 # These two are the same, because we are doing IPv4 unpacking
195 giptm = GenericIPAddrUnpackUniqueTest(generic_v4unpack_ip="::ffff:18.52.18.52")
196 giptm.save()
197 giptm = GenericIPAddrUnpackUniqueTest(generic_v4unpack_ip="18.52.18.52")
198 self.assertFailsValidation(giptm.full_clean, ['generic_v4unpack_ip',])