<s> from schematics . models import Model <EOL> from schematics . types import IntType , StringType <EOL> from schematics . validate import validate <EOL> from schematics . exceptions import ConversionError , ValidationError , DataError <EOL> def test_validate_simple_dict ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> validate ( Player , { '<STR_LIT:id>' : <NUM_LIT:4> } ) <EOL> def test_validate_keep_trusted_data ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> name = StringType ( ) <EOL> p1 = Player ( { '<STR_LIT:id>' : <NUM_LIT:4> } ) <EOL> data = validate ( Player , { '<STR_LIT:name>' : '<STR_LIT>' } , trusted_data = p1 . _data ) <EOL> assert data == { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> assert data != p1 . _data <EOL> def test_validate_override_trusted_data ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> p1 = Player ( { '<STR_LIT:id>' : <NUM_LIT:4> } ) <EOL> data = validate ( Player , { '<STR_LIT:id>' : <NUM_LIT:3> } , trusted_data = p1 . _data ) <EOL> assert data == { '<STR_LIT:id>' : <NUM_LIT:3> } <EOL> def test_validate_ignore_extra_trusted_data ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> data = validate ( Player , { '<STR_LIT:id>' : <NUM_LIT:4> } , trusted_data = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> assert data == { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> def test_validate_strict_with_trusted_data ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> try : <EOL> validate ( Player , { '<STR_LIT:id>' : <NUM_LIT:4> } , strict = True , trusted_data = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> except ValidationError as e : <EOL> assert '<STR_LIT:name>' in e . messages <EOL> def test_validate_partial_with_trusted_data ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> name = StringType ( required = True ) <EOL> data = validate ( Player , { '<STR_LIT:id>' : <NUM_LIT:4> } , partial = False , trusted_data = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> assert data == { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> def test_validate_with_instance_level_validators ( ) : <EOL> class Player ( Model ) : <EOL> id = IntType ( ) <EOL> def validate_id ( self , data , value , context ) : <EOL> if p1 . _initial [ '<STR_LIT:id>' ] != value : <EOL> p1 . _data [ '<STR_LIT:id>' ] = p1 . _initial [ '<STR_LIT:id>' ] <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> p1 = Player ( { '<STR_LIT:id>' : <NUM_LIT:4> } ) <EOL> p1 . id = <NUM_LIT:3> <EOL> try : <EOL> validate ( Player , p1 ) <EOL> except DataError as e : <EOL> assert '<STR_LIT:id>' in e . messages <EOL> assert '<STR_LIT>' in e . messages [ '<STR_LIT:id>' ] <EOL> assert p1 . id == <NUM_LIT:4> </s>
