<s> from __future__ import unicode_literals <EOL> from django . test import TestCase <EOL> from rest_framework import serializers <EOL> from django_countries . tests . models import Person <EOL> from django_countries . serializer_fields import CountryField <EOL> class PersonSerializer ( serializers . ModelSerializer ) : <EOL> country = CountryField ( ) <EOL> other_country = CountryField ( country_dict = True , required = False ) <EOL> class Meta : <EOL> model = Person <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestDRF ( TestCase ) : <EOL> def test_serialize ( self ) : <EOL> person = Person ( name = '<STR_LIT>' , country = '<STR_LIT>' ) <EOL> serializer = PersonSerializer ( person ) <EOL> self . assertEqual ( <EOL> serializer . data , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_serialize_country_dict ( self ) : <EOL> person = Person ( name = '<STR_LIT>' , other_country = '<STR_LIT>' ) <EOL> serializer = PersonSerializer ( person ) <EOL> self . assertEqual ( <EOL> serializer . data , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:code>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> } ) <EOL> def test_deserialize ( self ) : <EOL> serializer = PersonSerializer ( data = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> self . assertEqual ( serializer . validated_data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_deserialize_country_dict ( self ) : <EOL> serializer = PersonSerializer ( data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : { '<STR_LIT:code>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> self . assertEqual ( serializer . validated_data [ '<STR_LIT>' ] , '<STR_LIT>' ) </s>
