<s> from copy import copy <EOL> import pytest <EOL> from schematics . models import Model <EOL> from schematics . transforms import convert <EOL> from schematics . types import * <EOL> from schematics . types . compound import * <EOL> from schematics . exceptions import * <EOL> from schematics . undefined import Undefined <EOL> def autofail ( value , context ) : <EOL> if value != [ <NUM_LIT> ] : <EOL> raise ValidationError ( "<STR_LIT>" , info = <NUM_LIT> ) <EOL> class M ( Model ) : <EOL> intfield = IntType ( max_value = <NUM_LIT:2> ) <EOL> reqfield = StringType ( required = True ) <EOL> matrixfield = ListType ( ListType ( IntType ( max_value = <NUM_LIT:2> ) ) ) <EOL> listfield = ListType ( IntType ( ) , max_size = <NUM_LIT:3> , validators = [ autofail ] ) <EOL> modelfield = ModelType ( '<STR_LIT:M>' ) <EOL> def get_input_dict ( variant ) : <EOL> inputdict = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ '<STR_LIT:1>' , '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:0>' ] ] , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : None } } } } <EOL> if variant == '<STR_LIT>' : <EOL> del inputdict [ '<STR_LIT>' ] <EOL> del inputdict [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> del inputdict [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> inputdict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ <NUM_LIT> ] <EOL> inputdict [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return inputdict <EOL> def get_input_instance ( input_init , variant ) : <EOL> inputinstance = M ( init = input_init ) <EOL> inputinstance . intfield = '<STR_LIT:1>' <EOL> inputinstance . reqfield = '<STR_LIT:foo>' <EOL> inputinstance . listfield = [ ] <EOL> inputinstance . modelfield = M ( init = input_init ) <EOL> inputinstance . modelfield . reqfield = '<STR_LIT:bar>' <EOL> inputinstance . modelfield . listfield = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> inputinstance . modelfield . modelfield = M ( init = input_init ) <EOL> inputinstance . modelfield . modelfield . intfield = '<STR_LIT:3>' <EOL> inputinstance . modelfield . modelfield . matrixfield = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ '<STR_LIT:1>' , '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:0>' ] ] <EOL> inputinstance . modelfield . modelfield . listfield = None <EOL> inputinstance . modelfield . modelfield . modelfield = M ( init = input_init ) <EOL> inputinstance . modelfield . modelfield . modelfield . intfield = '<STR_LIT:0>' <EOL> inputinstance . modelfield . modelfield . modelfield . reqfield = '<STR_LIT:foo>' <EOL> inputinstance . modelfield . modelfield . modelfield . listfield = None <EOL> if variant == '<STR_LIT>' : <EOL> del inputinstance . listfield <EOL> del inputinstance . modelfield . listfield <EOL> del inputinstance . modelfield . modelfield . intfield <EOL> del inputinstance . modelfield . modelfield . matrixfield <EOL> inputinstance . modelfield . listfield = [ <NUM_LIT> ] <EOL> inputinstance . modelfield . modelfield . reqfield = '<STR_LIT>' <EOL> return inputinstance <EOL> @ pytest . fixture <EOL> def input ( input_instance , input_init , variant ) : <EOL> if input_instance : <EOL> return get_input_instance ( input_init , variant ) <EOL> else : <EOL> return get_input_dict ( variant ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , ( None , ) ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , <EOL> [ ( False , None , True , None ) , <EOL> ( False , None , False , Undefined ) , <EOL> ( True , False , True , None ) , <EOL> ( True , False , False , Undefined ) , <EOL> ( True , True , True , None ) , <EOL> ( True , True , False , None ) ] ) <EOL> def test_conversion ( input , input_instance , init , missing_obj ) : <EOL> orig_input = copy ( input ) <EOL> m = M ( input , init = init ) <EOL> assert type ( m . intfield ) is int <EOL> assert type ( m . modelfield . modelfield . intfield ) is int <EOL> assert type ( m . modelfield . modelfield . matrixfield [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] ) is int <EOL> assert type ( m . listfield ) is list <EOL> assert type ( m . modelfield ) is M <EOL> assert type ( m . modelfield . modelfield ) is M <EOL> assert type ( m . modelfield . modelfield . modelfield ) is M <EOL> assert type ( m . modelfield . listfield ) is list <EOL> assert type ( m . modelfield . modelfield . matrixfield ) is list <EOL> assert type ( m . modelfield . modelfield . matrixfield [ <NUM_LIT:2> ] ) is list <EOL> assert m . _data [ '<STR_LIT>' ] == [ ] <EOL> assert m . modelfield . _data [ '<STR_LIT>' ] is missing_obj <EOL> assert m . modelfield . modelfield . _data [ '<STR_LIT>' ] is None <EOL> assert m . modelfield . modelfield . _data [ '<STR_LIT>' ] is missing_obj <EOL> assert input == orig_input <EOL> if input_instance : <EOL> assert m . modelfield is not input . modelfield <EOL> assert m . _data [ '<STR_LIT>' ] is not input . _data [ '<STR_LIT>' ] <EOL> assert m . modelfield . listfield is not input . modelfield . listfield <EOL> else : <EOL> assert m . modelfield . listfield is not input [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , ( None , ) ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , <EOL> [ ( False , None , True , None ) , <EOL> ( False , None , False , Undefined ) , <EOL> ( True , False , True , None ) , <EOL> ( True , False , False , Undefined ) , <EOL> ( True , True , True , None ) , <EOL> ( True , True , False , None ) ] ) <EOL> def test_conversion_to_dictl ( input , input_instance , init , missing_obj ) : <EOL> orig_input = copy ( input ) <EOL> m = convert ( M , input , init_values = init , partial = True ) <EOL> assert type ( m ) is dict <EOL> assert type ( m [ '<STR_LIT>' ] ) is int <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) is int <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] ) is int <EOL> assert type ( m [ '<STR_LIT>' ] ) is list <EOL> assert type ( m [ '<STR_LIT>' ] ) is dict <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) is dict <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) is dict <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) is list <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) is list <EOL> assert type ( m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:2> ] ) is list <EOL> assert m [ '<STR_LIT>' ] == [ ] <EOL> assert m [ '<STR_LIT>' ] . get ( '<STR_LIT>' , Undefined ) is missing_obj <EOL> assert m [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] is None <EOL> assert m [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , Undefined ) is missing_obj <EOL> assert input == orig_input <EOL> if input_instance : <EOL> assert m [ '<STR_LIT>' ] is not input [ '<STR_LIT>' ] <EOL> assert m [ '<STR_LIT>' ] [ '<STR_LIT>' ] is not input [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , ( None , '<STR_LIT>' ) ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , ( True , False ) ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , <EOL> [ ( True , False , False , None , True , None ) , <EOL> ( True , False , False , None , False , Undefined ) , <EOL> ( True , False , True , False , True , None ) , <EOL> ( True , False , True , False , False , Undefined ) , <EOL> ( True , False , True , True , True , None ) , <EOL> ( True , False , True , True , False , None ) , <EOL> ( True , True , False , None , True , None ) , <EOL> ( True , True , False , None , False , Undefined ) , <EOL> ( True , True , True , False , True , None ) , <EOL> ( True , True , True , False , False , Undefined ) , <EOL> ( True , True , True , True , True , None ) , <EOL> ( True , True , True , True , False , None ) , <EOL> ( False , None , True , False , True , None ) , <EOL> ( False , None , True , False , False , Undefined ) , <EOL> ( False , None , True , True , True , None ) , <EOL> ( False , None , True , True , False , None ) ] ) <EOL> def test_conversion_with_validation ( input , import_ , two_pass , input_instance , init , missing_obj , <EOL> partial , variant ) : <EOL> if variant == '<STR_LIT>' : <EOL> orig_input = copy ( input ) <EOL> if input_instance : <EOL> assert input . modelfield is orig_input . modelfield <EOL> if import_ : <EOL> if two_pass : <EOL> m = M ( input , init = init ) <EOL> m . validate ( partial = partial ) <EOL> else : <EOL> m = M ( input , init = init , partial = partial , validate = True ) <EOL> else : <EOL> input . validate ( init_values = init , partial = partial ) <EOL> m = input <EOL> assert input == orig_input <EOL> if input_instance : <EOL> if import_ : <EOL> assert m . modelfield is not input . modelfield <EOL> assert m . _data [ '<STR_LIT>' ] is not input . _data [ '<STR_LIT>' ] <EOL> assert m . modelfield . listfield is not input . modelfield . listfield <EOL> else : <EOL> assert m . modelfield is input . modelfield <EOL> assert m . _data [ '<STR_LIT>' ] is input . _data [ '<STR_LIT>' ] <EOL> assert m . modelfield . listfield is input . modelfield . listfield <EOL> return <EOL> if missing_obj is None : <EOL> partial_data = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : u'<STR_LIT:foo>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : u'<STR_LIT:bar>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : M ( { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : u'<STR_LIT:foo>' , <EOL> '<STR_LIT>' : None } ) } } } <EOL> else : <EOL> partial_data = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : u'<STR_LIT:foo>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : u'<STR_LIT:bar>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : M ( { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : u'<STR_LIT:foo>' , <EOL> '<STR_LIT>' : None } , init = False ) } } } <EOL> with pytest . raises ( DataError ) as excinfo : <EOL> if import_ : <EOL> if two_pass : <EOL> m = M ( input , init = init ) <EOL> m . validate ( partial = partial ) <EOL> else : <EOL> M ( input , init = init , partial = partial , validate = True ) <EOL> else : <EOL> input . validate ( init_values = init , partial = partial ) <EOL> messages = excinfo . value . messages <EOL> err_list = messages . pop ( '<STR_LIT>' ) <EOL> assert err_list . pop ( ) . type == ValidationError <EOL> assert err_list == [ ] <EOL> err_list = messages [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> assert err_list . pop ( ) . type == ValidationError <EOL> assert err_list . pop ( ) . type == ValidationError <EOL> assert err_list == [ ] <EOL> err_list = messages [ '<STR_LIT>' ] [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> err_msg = err_list . pop ( ) <EOL> assert err_list == [ ] <EOL> assert err_msg . type == ValidationError <EOL> if not partial : <EOL> err_list = messages [ '<STR_LIT>' ] [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> err_msg = err_list . pop ( ) <EOL> assert err_list == [ ] <EOL> assert err_msg . type == ConversionError <EOL> if missing_obj is None : <EOL> partial_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> err_dict = messages [ '<STR_LIT>' ] [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> sub_err_dict = err_dict . pop ( <NUM_LIT:1> ) <EOL> err_list_1 = sub_err_dict . pop ( <NUM_LIT:2> ) <EOL> err_list_2 = sub_err_dict . pop ( <NUM_LIT:3> ) <EOL> assert err_list_1 . pop ( ) . type == ValidationError <EOL> assert err_list_2 . pop ( ) . type == ValidationError <EOL> assert err_list_1 == err_list_2 == [ ] <EOL> assert err_dict == sub_err_dict == { } <EOL> assert messages [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) == { } <EOL> assert messages . pop ( '<STR_LIT>' ) == { } <EOL> assert messages == { } <EOL> assert excinfo . value . partial_data == partial_data </s>
