<s> from __future__ import unicode_literals <EOL> import decimal <EOL> from django . forms import TypedChoiceField , ValidationError <EOL> from django . test import SimpleTestCase <EOL> from django . utils import six <EOL> class TypedChoiceFieldTest ( SimpleTestCase ) : <EOL> def test_typedchoicefield_1 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = int ) <EOL> self . assertEqual ( <NUM_LIT:1> , f . clean ( '<STR_LIT:1>' ) ) <EOL> msg = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( ValidationError , msg ) : <EOL> f . clean ( '<STR_LIT:2>' ) <EOL> def test_typedchoicefield_2 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = float ) <EOL> self . assertEqual ( <NUM_LIT:1.0> , f . clean ( '<STR_LIT:1>' ) ) <EOL> def test_typedchoicefield_3 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = bool ) <EOL> self . assertTrue ( f . clean ( '<STR_LIT>' ) ) <EOL> def test_typedchoicefield_4 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( '<STR_LIT:A>' , '<STR_LIT:A>' ) , ( '<STR_LIT:B>' , '<STR_LIT:B>' ) ] , coerce = int ) <EOL> msg = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( ValidationError , msg ) : <EOL> f . clean ( '<STR_LIT:B>' ) <EOL> with self . assertRaisesMessage ( ValidationError , "<STR_LIT>" ) : <EOL> f . clean ( '<STR_LIT>' ) <EOL> def test_typedchoicefield_5 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = int , required = False ) <EOL> self . assertEqual ( '<STR_LIT>' , f . clean ( '<STR_LIT>' ) ) <EOL> def test_typedchoicefield_6 ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = int , required = False , empty_value = None ) <EOL> self . assertIsNone ( f . clean ( '<STR_LIT>' ) ) <EOL> def test_typedchoicefield_has_changed ( self ) : <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT>" ) , ( - <NUM_LIT:1> , "<STR_LIT>" ) ] , coerce = int , required = True ) <EOL> self . assertFalse ( f . has_changed ( None , '<STR_LIT>' ) ) <EOL> self . assertFalse ( f . has_changed ( <NUM_LIT:1> , '<STR_LIT:1>' ) ) <EOL> self . assertFalse ( f . has_changed ( '<STR_LIT:1>' , '<STR_LIT:1>' ) ) <EOL> f = TypedChoiceField ( <EOL> choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:a>' , "<STR_LIT:a>" ) , ( '<STR_LIT:b>' , "<STR_LIT:b>" ) ] , coerce = six . text_type , <EOL> required = False , initial = None , empty_value = None , <EOL> ) <EOL> self . assertFalse ( f . has_changed ( None , '<STR_LIT>' ) ) <EOL> self . assertTrue ( f . has_changed ( '<STR_LIT>' , '<STR_LIT:a>' ) ) <EOL> self . assertFalse ( f . has_changed ( '<STR_LIT:a>' , '<STR_LIT:a>' ) ) <EOL> def test_typedchoicefield_special_coerce ( self ) : <EOL> """<STR_LIT>""" <EOL> def coerce_func ( val ) : <EOL> return decimal . Decimal ( '<STR_LIT>' % val ) <EOL> f = TypedChoiceField ( choices = [ ( <NUM_LIT:1> , "<STR_LIT:1>" ) , ( <NUM_LIT:2> , "<STR_LIT:2>" ) ] , coerce = coerce_func , required = True ) <EOL> self . assertEqual ( decimal . Decimal ( '<STR_LIT>' ) , f . clean ( '<STR_LIT:2>' ) ) <EOL> with self . assertRaisesMessage ( ValidationError , "<STR_LIT>" ) : <EOL> f . clean ( '<STR_LIT>' ) <EOL> msg = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( ValidationError , msg ) : <EOL> f . clean ( '<STR_LIT:3>' ) </s>
