<s> from collections import OrderedDict <EOL> from dj . choices import Choices <EOL> from rest_framework . exceptions import ValidationError <EOL> from ralph . api . fields import ReversedChoiceField , StrField <EOL> from ralph . tests import RalphTestCase <EOL> from ralph . tests . models import Foo <EOL> class TestChoices ( Choices ) : <EOL> _ = Choices . Choice <EOL> foo = _ ( '<STR_LIT>' ) <EOL> bar = _ ( '<STR_LIT>' ) <EOL> class TestStrField ( RalphTestCase ) : <EOL> def setUp ( self ) : <EOL> self . foo = Foo ( bar = '<STR_LIT:abc>' ) <EOL> self . str_field = StrField ( ) <EOL> self . str_field_with_type = StrField ( show_type = True ) <EOL> def test_str_field_representation ( self ) : <EOL> self . assertEqual ( <EOL> self . str_field . to_representation ( self . foo ) , str ( self . foo ) <EOL> ) <EOL> def test_str_field_with_type ( self ) : <EOL> self . assertEqual ( <EOL> self . str_field_with_type . to_representation ( self . foo ) , <EOL> '<STR_LIT>' . format ( Foo . _meta . verbose_name , str ( self . foo ) ) <EOL> ) <EOL> class TestReversedChoiceField ( RalphTestCase ) : <EOL> def setUp ( self ) : <EOL> self . reversed_choice_field = ReversedChoiceField ( TestChoices ( ) ) <EOL> def test_reversed_choices ( self ) : <EOL> self . assertEqual ( <EOL> self . reversed_choice_field . reversed_choices , <EOL> OrderedDict ( [ <EOL> ( '<STR_LIT>' , TestChoices . foo . id ) , <EOL> ( '<STR_LIT>' , TestChoices . bar . id ) <EOL> ] ) <EOL> ) <EOL> def test_to_representation_should_return_choice_text ( self ) : <EOL> self . assertEqual ( <EOL> self . reversed_choice_field . to_representation ( TestChoices . foo . id ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_to_internal_value_should_map_choice_text_to_id ( self ) : <EOL> self . assertEqual ( <EOL> self . reversed_choice_field . to_internal_value ( '<STR_LIT>' ) , <EOL> TestChoices . foo . id , <EOL> ) <EOL> def test_to_internal_value_with_choice_not_found_should_raise_validation_error ( self ) : <EOL> with self . assertRaises ( ValidationError ) : <EOL> self . reversed_choice_field . to_internal_value ( '<STR_LIT>' ) </s>
