PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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