<s> from django . test import TestCase <EOL> from rest_framework import serializers <EOL> from tests . models import ( <EOL> ForeignKeySource , ForeignKeyTarget , NullableForeignKeySource <EOL> ) <EOL> class ForeignKeyTargetSerializer ( serializers . ModelSerializer ) : <EOL> sources = serializers . SlugRelatedField ( <EOL> slug_field = '<STR_LIT:name>' , <EOL> queryset = ForeignKeySource . objects . all ( ) , <EOL> many = True <EOL> ) <EOL> class Meta : <EOL> model = ForeignKeyTarget <EOL> class ForeignKeySourceSerializer ( serializers . ModelSerializer ) : <EOL> target = serializers . SlugRelatedField ( <EOL> slug_field = '<STR_LIT:name>' , <EOL> queryset = ForeignKeyTarget . objects . all ( ) <EOL> ) <EOL> class Meta : <EOL> model = ForeignKeySource <EOL> class NullableForeignKeySourceSerializer ( serializers . ModelSerializer ) : <EOL> target = serializers . SlugRelatedField ( <EOL> slug_field = '<STR_LIT:name>' , <EOL> queryset = ForeignKeyTarget . objects . all ( ) , <EOL> allow_null = True <EOL> ) <EOL> class Meta : <EOL> model = NullableForeignKeySource <EOL> class SlugForeignKeyTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> target = ForeignKeyTarget ( name = '<STR_LIT>' ) <EOL> target . save ( ) <EOL> new_target = ForeignKeyTarget ( name = '<STR_LIT>' ) <EOL> new_target . save ( ) <EOL> for idx in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> source = ForeignKeySource ( name = '<STR_LIT>' % idx , target = target ) <EOL> source . save ( ) <EOL> def test_foreign_key_retrieve ( self ) : <EOL> queryset = ForeignKeySource . objects . all ( ) <EOL> serializer = ForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> ] <EOL> with self . assertNumQueries ( <NUM_LIT:4> ) : <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_retrieve_select_related ( self ) : <EOL> queryset = ForeignKeySource . objects . all ( ) . select_related ( '<STR_LIT:target>' ) <EOL> serializer = ForeignKeySourceSerializer ( queryset , many = True ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> serializer . data <EOL> def test_reverse_foreign_key_retrieve ( self ) : <EOL> queryset = ForeignKeyTarget . objects . all ( ) <EOL> serializer = ForeignKeyTargetSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } , <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_reverse_foreign_key_retrieve_prefetch_related ( self ) : <EOL> queryset = ForeignKeyTarget . objects . all ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> serializer = ForeignKeyTargetSerializer ( queryset , many = True ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> serializer . data <EOL> def test_foreign_key_update ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> instance = ForeignKeySource . objects . get ( pk = <NUM_LIT:1> ) <EOL> serializer = ForeignKeySourceSerializer ( instance , data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> queryset = ForeignKeySource . objects . all ( ) <EOL> serializer = ForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_update_incorrect_type ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : <NUM_LIT> } <EOL> instance = ForeignKeySource . objects . get ( pk = <NUM_LIT:1> ) <EOL> serializer = ForeignKeySourceSerializer ( instance , data = data ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> self . assertEqual ( serializer . errors , { '<STR_LIT:target>' : [ '<STR_LIT>' ] } ) <EOL> def test_reverse_foreign_key_update ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> instance = ForeignKeyTarget . objects . get ( pk = <NUM_LIT:2> ) <EOL> serializer = ForeignKeyTargetSerializer ( instance , data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> queryset = ForeignKeyTarget . objects . all ( ) <EOL> new_serializer = ForeignKeyTargetSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } , <EOL> ] <EOL> self . assertEqual ( new_serializer . data , expected ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> queryset = ForeignKeyTarget . objects . all ( ) <EOL> serializer = ForeignKeyTargetSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_create ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> serializer = ForeignKeySourceSerializer ( data = data ) <EOL> serializer . is_valid ( ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> obj = serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> self . assertEqual ( obj . name , '<STR_LIT>' ) <EOL> queryset = ForeignKeySource . objects . all ( ) <EOL> serializer = ForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_reverse_foreign_key_create ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> serializer = ForeignKeyTargetSerializer ( data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> obj = serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> self . assertEqual ( obj . name , '<STR_LIT>' ) <EOL> queryset = ForeignKeyTarget . objects . all ( ) <EOL> serializer = ForeignKeyTargetSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_update_with_invalid_null ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> instance = ForeignKeySource . objects . get ( pk = <NUM_LIT:1> ) <EOL> serializer = ForeignKeySourceSerializer ( instance , data = data ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> self . assertEqual ( serializer . errors , { '<STR_LIT:target>' : [ '<STR_LIT>' ] } ) <EOL> class SlugNullableForeignKeyTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> target = ForeignKeyTarget ( name = '<STR_LIT>' ) <EOL> target . save ( ) <EOL> for idx in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> if idx == <NUM_LIT:3> : <EOL> target = None <EOL> source = NullableForeignKeySource ( name = '<STR_LIT>' % idx , target = target ) <EOL> source . save ( ) <EOL> def test_foreign_key_retrieve_with_null ( self ) : <EOL> queryset = NullableForeignKeySource . objects . all ( ) <EOL> serializer = NullableForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } , <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_create_with_valid_null ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> serializer = NullableForeignKeySourceSerializer ( data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> obj = serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> self . assertEqual ( obj . name , '<STR_LIT>' ) <EOL> queryset = NullableForeignKeySource . objects . all ( ) <EOL> serializer = NullableForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_create_with_valid_emptystring ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> expected_data = { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> serializer = NullableForeignKeySourceSerializer ( data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> obj = serializer . save ( ) <EOL> self . assertEqual ( serializer . data , expected_data ) <EOL> self . assertEqual ( obj . name , '<STR_LIT>' ) <EOL> queryset = NullableForeignKeySource . objects . all ( ) <EOL> serializer = NullableForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:4> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_update_with_valid_null ( self ) : <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> instance = NullableForeignKeySource . objects . get ( pk = <NUM_LIT:1> ) <EOL> serializer = NullableForeignKeySourceSerializer ( instance , data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( serializer . data , data ) <EOL> queryset = NullableForeignKeySource . objects . all ( ) <EOL> serializer = NullableForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) <EOL> def test_foreign_key_update_with_valid_emptystring ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } <EOL> expected_data = { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> instance = NullableForeignKeySource . objects . get ( pk = <NUM_LIT:1> ) <EOL> serializer = NullableForeignKeySourceSerializer ( instance , data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> serializer . save ( ) <EOL> self . assertEqual ( serializer . data , expected_data ) <EOL> queryset = NullableForeignKeySource . objects . all ( ) <EOL> serializer = NullableForeignKeySourceSerializer ( queryset , many = True ) <EOL> expected = [ <EOL> { '<STR_LIT:id>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : <NUM_LIT:3> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:target>' : None } <EOL> ] <EOL> self . assertEqual ( serializer . data , expected ) </s>
