<s> from django import forms <EOL> from django . test import TestCase <EOL> from django . core . exceptions import NON_FIELD_ERRORS <EOL> from modeltests . validation import ValidationTestCase <EOL> from modeltests . validation . models import Author , Article , ModelToValidate <EOL> from modeltests . validation . validators import TestModelsWithValidators <EOL> from modeltests . validation . test_unique import GetUniqueCheckTests , PerformUniqueChecksTest <EOL> from modeltests . validation . test_custom_messages import CustomMessagesTest <EOL> class BaseModelValidationTests ( ValidationTestCase ) : <EOL> def test_missing_required_field_raises_error ( self ) : <EOL> mtv = ModelToValidate ( f_with_custom_validator = <NUM_LIT> ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> def test_with_correct_value_model_validates ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' ) <EOL> self . assertEqual ( None , mtv . full_clean ( ) ) <EOL> def test_custom_validate_method ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:11> ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ NON_FIELD_ERRORS , '<STR_LIT:name>' ] ) <EOL> def test_wrong_FK_value_raises_error ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , parent_id = <NUM_LIT:3> ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_correct_FK_value_validates ( self ) : <EOL> parent = ModelToValidate . objects . create ( number = <NUM_LIT:10> , name = '<STR_LIT>' ) <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , parent_id = parent . pk ) <EOL> self . assertEqual ( None , mtv . full_clean ( ) ) <EOL> def test_limitted_FK_raises_error ( self ) : <EOL> parent = ModelToValidate . objects . create ( number = <NUM_LIT:11> , name = '<STR_LIT>' ) <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , parent_id = parent . pk ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_wrong_email_value_raises_error ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT:email>' ] ) <EOL> def test_correct_email_value_passes ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> self . assertEqual ( None , mtv . full_clean ( ) ) <EOL> def test_wrong_url_value_raises_error ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertFieldFailsValidationWithMessage ( mtv . full_clean , '<STR_LIT:url>' , [ u'<STR_LIT>' ] ) <EOL> def test_correct_url_but_nonexisting_gives_404 ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertFieldFailsValidationWithMessage ( mtv . full_clean , '<STR_LIT:url>' , [ u'<STR_LIT>' ] ) <EOL> def test_correct_url_value_passes ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertEqual ( None , mtv . full_clean ( ) ) <EOL> def test_text_greater_that_charfields_max_length_eaises_erros ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' * <NUM_LIT:100> ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT:name>' , ] ) <EOL> class ArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> exclude = [ '<STR_LIT>' ] <EOL> class ModelFormsTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . author = Author . objects . create ( name = '<STR_LIT>' ) <EOL> def test_partial_validation ( self ) : <EOL> data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> form = ArticleForm ( data ) <EOL> self . assertEqual ( form . errors . keys ( ) , [ ] ) <EOL> article = form . save ( commit = False ) <EOL> article . author = self . author <EOL> article . save ( ) <EOL> def test_validation_with_empty_blank_field ( self ) : <EOL> data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } <EOL> article = Article ( author_id = self . author . id ) <EOL> form = ArticleForm ( data , instance = article ) <EOL> self . assertEqual ( form . errors . keys ( ) , [ ] ) <EOL> self . assertNotEqual ( form . instance . pub_date , None ) <EOL> article = form . save ( ) <EOL> def test_validation_with_invalid_blank_field ( self ) : <EOL> data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> article = Article ( author_id = self . author . id ) <EOL> form = ArticleForm ( data , instance = article ) <EOL> self . assertEqual ( form . errors . keys ( ) , [ '<STR_LIT>' ] ) </s>
