<s> from wtforms import ( <EOL> Form , <EOL> SelectField , <EOL> SelectFieldBase , <EOL> SelectMultipleField , <EOL> TextField , <EOL> validators , <EOL> widgets <EOL> ) <EOL> def test_select_field ( ) : <EOL> fixtures = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : True } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : False } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : True } , <EOL> ] <EOL> class MomForm ( Form ) : <EOL> name = TextField ( ) <EOL> childs = SelectField ( choices = ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , coerce = int ) <EOL> for fixture in fixtures : <EOL> result = fixture . pop ( '<STR_LIT>' ) <EOL> assert MomForm . from_json ( fixture ) . validate ( ) == result <EOL> def test_select_multiple_field ( ) : <EOL> fixtures = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : True } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ ] , '<STR_LIT>' : False } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ <NUM_LIT:4> ] , '<STR_LIT>' : True } , <EOL> ] <EOL> class AppleFanBoyForm ( Form ) : <EOL> name = TextField ( ) <EOL> gadgets = SelectMultipleField ( <EOL> choices = ( <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' ) <EOL> ) , <EOL> validators = [ validators . required ( ) ] , <EOL> coerce = int <EOL> ) <EOL> for fixture in fixtures : <EOL> result = fixture . pop ( '<STR_LIT>' ) <EOL> assert AppleFanBoyForm . from_json ( fixture ) . validate ( ) == result <EOL> def test_custom_field ( ) : <EOL> class SuperPowersField ( SelectFieldBase ) : <EOL> POWERS = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> widget = widgets . Select ( multiple = True ) <EOL> def iter_choices ( self ) : <EOL> if self . allow_blank : <EOL> yield ( u'<STR_LIT>' , self . blank_text , self . data is None ) <EOL> for item in self . POWERS : <EOL> selected = item [ <NUM_LIT:0> ] in self . data <EOL> yield ( item [ <NUM_LIT:0> ] , item [ <NUM_LIT:1> ] , selected ) <EOL> def process_formdata ( self , valuelist ) : <EOL> if valuelist : <EOL> if valuelist [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . data = None <EOL> else : <EOL> self . data = [ <EOL> item [ <NUM_LIT:0> ] for item in self . POWERS <EOL> if str ( item [ <NUM_LIT:0> ] ) in valuelist ] <EOL> if not len ( self . data ) : <EOL> self . data = None <EOL> def _is_selected ( self , item ) : <EOL> return item in self . data <EOL> class SuperHeroForm ( Form ) : <EOL> name = TextField ( ) <EOL> powers = SuperPowersField ( <EOL> validators = [ validators . required ( ) ] <EOL> ) <EOL> fixtures = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] , '<STR_LIT>' : True } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] , '<STR_LIT>' : True } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ ] , '<STR_LIT>' : False } <EOL> ] <EOL> for fixture in fixtures : <EOL> result = fixture . pop ( '<STR_LIT>' ) <EOL> assert SuperHeroForm . from_json ( fixture ) . validate ( ) == result </s>
