<s> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from django . utils import timezone <EOL> from rest_framework_json_api . utils import format_resource_type <EOL> from rest_framework_json_api . serializers import ResourceIdentifierObjectSerializer <EOL> from example . models import Blog , Entry , Author <EOL> import pytest <EOL> from example . tests . utils import dump_json , redump_json <EOL> pytestmark = pytest . mark . django_db <EOL> class TestResourceIdentifierObjectSerializer ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . blog = Blog . objects . create ( name = '<STR_LIT>' , tagline = "<STR_LIT>" ) <EOL> self . entry = Entry . objects . create ( <EOL> blog = self . blog , <EOL> headline = '<STR_LIT>' , <EOL> body_text = '<STR_LIT>' , <EOL> pub_date = timezone . now ( ) , <EOL> mod_date = timezone . now ( ) , <EOL> n_comments = <NUM_LIT:0> , <EOL> n_pingbacks = <NUM_LIT:0> , <EOL> rating = <NUM_LIT:3> <EOL> ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) : <EOL> name = '<STR_LIT>' . format ( i ) <EOL> self . entry . authors . add ( <EOL> Author . objects . create ( name = name , email = '<STR_LIT>' . format ( name ) ) <EOL> ) <EOL> def test_data_in_correct_format_when_instantiated_with_blog_object ( self ) : <EOL> serializer = ResourceIdentifierObjectSerializer ( instance = self . blog ) <EOL> expected_data = { '<STR_LIT:type>' : format_resource_type ( '<STR_LIT>' ) , '<STR_LIT:id>' : str ( self . blog . id ) } <EOL> assert serializer . data == expected_data <EOL> def test_data_in_correct_format_when_instantiated_with_entry_object ( self ) : <EOL> serializer = ResourceIdentifierObjectSerializer ( instance = self . entry ) <EOL> expected_data = { '<STR_LIT:type>' : format_resource_type ( '<STR_LIT>' ) , '<STR_LIT:id>' : str ( self . entry . id ) } <EOL> assert serializer . data == expected_data <EOL> def test_deserialize_primitive_data_blog ( self ) : <EOL> initial_data = { <EOL> '<STR_LIT:type>' : format_resource_type ( '<STR_LIT>' ) , <EOL> '<STR_LIT:id>' : str ( self . blog . id ) <EOL> } <EOL> serializer = ResourceIdentifierObjectSerializer ( data = initial_data , model_class = Blog ) <EOL> self . assertTrue ( serializer . is_valid ( ) , msg = serializer . errors ) <EOL> assert serializer . validated_data == self . blog <EOL> def test_data_in_correct_format_when_instantiated_with_queryset ( self ) : <EOL> qs = Author . objects . all ( ) <EOL> serializer = ResourceIdentifierObjectSerializer ( instance = qs , many = True ) <EOL> type_string = format_resource_type ( '<STR_LIT>' ) <EOL> author_pks = Author . objects . values_list ( '<STR_LIT>' , flat = True ) <EOL> expected_data = [ { '<STR_LIT:type>' : type_string , '<STR_LIT:id>' : str ( pk ) } for pk in author_pks ] <EOL> assert serializer . data == expected_data <EOL> def test_deserialize_many ( self ) : <EOL> type_string = format_resource_type ( '<STR_LIT>' ) <EOL> author_pks = Author . objects . values_list ( '<STR_LIT>' , flat = True ) <EOL> initial_data = [ { '<STR_LIT:type>' : type_string , '<STR_LIT:id>' : str ( pk ) } for pk in author_pks ] <EOL> serializer = ResourceIdentifierObjectSerializer ( data = initial_data , model_class = Author , many = True ) <EOL> self . assertTrue ( serializer . is_valid ( ) , msg = serializer . errors ) <EOL> print ( serializer . data ) <EOL> class TestModelSerializer ( object ) : <EOL> def test_model_serializer_with_implicit_fields ( self , comment , client ) : <EOL> expected = { <EOL> "<STR_LIT:data>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : str ( comment . pk ) , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:body>" : comment . body <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:data>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : str ( comment . entry . pk ) <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:data>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : str ( comment . author . pk ) <EOL> } <EOL> } , <EOL> } <EOL> } <EOL> } <EOL> response = client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : comment . pk } ) ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> actual = redump_json ( response . content ) <EOL> expected_json = dump_json ( expected ) <EOL> assert actual == expected_json </s>
