PageRenderTime 109ms CodeModel.GetById 56ms app.highlight 13ms RepoModel.GetById 37ms app.codeStats 0ms

/tests/modeltests/validation/tests.py

https://code.google.com/p/mango-py/
Python | 123 lines | 88 code | 22 blank | 13 comment | 0 complexity | 7459c582a14b065e9e5ba97d67deac8f MD5 | raw file
  1from django import forms
  2from django.test import TestCase
  3from django.core.exceptions import NON_FIELD_ERRORS
  4from modeltests.validation import ValidationTestCase
  5from modeltests.validation.models import Author, Article, ModelToValidate
  6
  7# Import other tests for this package.
  8from modeltests.validation.validators import TestModelsWithValidators
  9from modeltests.validation.test_unique import (GetUniqueCheckTests,
 10    PerformUniqueChecksTest)
 11from modeltests.validation.test_custom_messages import CustomMessagesTest
 12
 13
 14class BaseModelValidationTests(ValidationTestCase):
 15
 16    def test_missing_required_field_raises_error(self):
 17        mtv = ModelToValidate(f_with_custom_validator=42)
 18        self.assertFailsValidation(mtv.full_clean, ['name', 'number'])
 19
 20    def test_with_correct_value_model_validates(self):
 21        mtv = ModelToValidate(number=10, name='Some Name')
 22        self.assertEqual(None, mtv.full_clean())
 23
 24    def test_custom_validate_method(self):
 25        mtv = ModelToValidate(number=11)
 26        self.assertFailsValidation(mtv.full_clean, [NON_FIELD_ERRORS, 'name'])
 27
 28    def test_wrong_FK_value_raises_error(self):
 29        mtv=ModelToValidate(number=10, name='Some Name', parent_id=3)
 30        self.assertFailsValidation(mtv.full_clean, ['parent'])
 31
 32    def test_correct_FK_value_validates(self):
 33        parent = ModelToValidate.objects.create(number=10, name='Some Name')
 34        mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
 35        self.assertEqual(None, mtv.full_clean())
 36
 37    def test_limited_FK_raises_error(self):
 38        # The limit_choices_to on the parent field says that a parent object's
 39        # number attribute must be 10, so this should fail validation.
 40        parent = ModelToValidate.objects.create(number=11, name='Other Name')
 41        mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
 42        self.assertFailsValidation(mtv.full_clean, ['parent'])
 43
 44    def test_wrong_email_value_raises_error(self):
 45        mtv = ModelToValidate(number=10, name='Some Name', email='not-an-email')
 46        self.assertFailsValidation(mtv.full_clean, ['email'])
 47
 48    def test_correct_email_value_passes(self):
 49        mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
 50        self.assertEqual(None, mtv.full_clean())
 51
 52    def test_wrong_url_value_raises_error(self):
 53        mtv = ModelToValidate(number=10, name='Some Name', url='not a url')
 54        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'Enter a valid value.'])
 55
 56    #The tests below which use url_verify are deprecated
 57    def test_correct_url_but_nonexisting_gives_404(self):
 58        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404')
 59        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
 60
 61    def test_correct_url_value_passes(self):
 62        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
 63        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
 64
 65    def test_correct_url_with_redirect(self):
 66        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
 67        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
 68
 69    def test_correct_https_url_but_nonexisting(self):
 70        mtv = ModelToValidate(number=10, name='Some Name', url_verify='https://www.example.com/')
 71        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
 72
 73    def test_text_greater_that_charfields_max_length_raises_erros(self):
 74        mtv = ModelToValidate(number=10, name='Some Name'*100)
 75        self.assertFailsValidation(mtv.full_clean, ['name',])
 76
 77class ArticleForm(forms.ModelForm):
 78    class Meta:
 79        model = Article
 80        exclude = ['author']
 81
 82class ModelFormsTests(TestCase):
 83    def setUp(self):
 84        self.author = Author.objects.create(name='Joseph Kocherhans')
 85
 86    def test_partial_validation(self):
 87        # Make sure the "commit=False and set field values later" idiom still
 88        # works with model validation.
 89        data = {
 90            'title': 'The state of model validation',
 91            'pub_date': '2010-1-10 14:49:00'
 92        }
 93        form = ArticleForm(data)
 94        self.assertEqual(form.errors.keys(), [])
 95        article = form.save(commit=False)
 96        article.author = self.author
 97        article.save()
 98
 99    def test_validation_with_empty_blank_field(self):
100        # Since a value for pub_date wasn't provided and the field is
101        # blank=True, model-validation should pass.
102        # Also, Article.clean() should be run, so pub_date will be filled after
103        # validation, so the form should save cleanly even though pub_date is
104        # not allowed to be null.
105        data = {
106            'title': 'The state of model validation',
107        }
108        article = Article(author_id=self.author.id)
109        form = ArticleForm(data, instance=article)
110        self.assertEqual(form.errors.keys(), [])
111        self.assertNotEqual(form.instance.pub_date, None)
112        article = form.save()
113
114    def test_validation_with_invalid_blank_field(self):
115        # Even though pub_date is set to blank=True, an invalid value was
116        # provided, so it should fail validation.
117        data = {
118            'title': 'The state of model validation',
119            'pub_date': 'never'
120        }
121        article = Article(author_id=self.author.id)
122        form = ArticleForm(data, instance=article)
123        self.assertEqual(form.errors.keys(), ['pub_date'])