<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . core . exceptions import NON_FIELD_ERRORS <EOL> from django . test import TestCase <EOL> from django . utils . functional import lazy <EOL> from . import ValidationTestCase <EOL> from . models import ( <EOL> Article , Author , GenericIPAddressTestModel , GenericIPAddrUnpackUniqueTest , <EOL> ModelToValidate , <EOL> ) <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 . assertIsNone ( 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 . assertFieldFailsValidationWithMessage ( <EOL> mtv . full_clean , '<STR_LIT>' , <EOL> [ '<STR_LIT>' % mtv . parent_id ] <EOL> ) <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , ufm_id = '<STR_LIT>' ) <EOL> self . assertFieldFailsValidationWithMessage ( <EOL> mtv . full_clean , '<STR_LIT>' , <EOL> [ "<STR_LIT>" % mtv . name ] <EOL> ) <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 . assertIsNone ( mtv . full_clean ( ) ) <EOL> def test_limited_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 . assertIsNone ( 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>' , [ '<STR_LIT>' ] ) <EOL> def test_text_greater_that_charfields_max_length_raises_errors ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' * <NUM_LIT:100> ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT:name>' ] ) <EOL> def test_malformed_slug_raises_error ( self ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , slug = '<STR_LIT>' ) <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_full_clean_does_not_mutate_exclude ( self ) : <EOL> mtv = ModelToValidate ( f_with_custom_validator = <NUM_LIT> ) <EOL> exclude = [ '<STR_LIT>' ] <EOL> self . assertFailsValidation ( mtv . full_clean , [ '<STR_LIT:name>' ] , exclude = exclude ) <EOL> self . assertEqual ( len ( exclude ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( exclude [ <NUM_LIT:0> ] , '<STR_LIT>' ) <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 ( list ( form . errors ) , [ ] ) <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 ( list ( form . errors ) , [ ] ) <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 ( list ( form . errors ) , [ '<STR_LIT>' ] ) <EOL> class GenericIPAddressFieldTests ( ValidationTestCase ) : <EOL> def test_correct_generic_ip_passes ( self ) : <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> def test_invalid_generic_ip_raises_error ( self ) : <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = <NUM_LIT:1> ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = lazy ( lambda : <NUM_LIT:1> , int ) ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_correct_v4_ip_passes ( self ) : <EOL> giptm = GenericIPAddressTestModel ( v4_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> def test_invalid_v4_ip_raises_error ( self ) : <EOL> giptm = GenericIPAddressTestModel ( v4_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> giptm = GenericIPAddressTestModel ( v4_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_correct_v6_ip_passes ( self ) : <EOL> giptm = GenericIPAddressTestModel ( v6_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> def test_invalid_v6_ip_raises_error ( self ) : <EOL> giptm = GenericIPAddressTestModel ( v6_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> giptm = GenericIPAddressTestModel ( v6_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_v6_uniqueness_detection ( self ) : <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> giptm . save ( ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_v4_unpack_uniqueness_detection ( self ) : <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> giptm . save ( ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> giptm = GenericIPAddrUnpackUniqueTest ( generic_v4unpack_ip = "<STR_LIT>" ) <EOL> giptm . save ( ) <EOL> giptm = GenericIPAddrUnpackUniqueTest ( generic_v4unpack_ip = "<STR_LIT>" ) <EOL> self . assertFailsValidation ( giptm . full_clean , [ '<STR_LIT>' ] ) <EOL> def test_empty_generic_ip_passes ( self ) : <EOL> giptm = GenericIPAddressTestModel ( generic_ip = "<STR_LIT>" ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) <EOL> giptm = GenericIPAddressTestModel ( generic_ip = None ) <EOL> self . assertIsNone ( giptm . full_clean ( ) ) </s>
