<s> from datetime import datetime <EOL> import unittest <EOL> from django . db import models <EOL> from django . http import HttpResponse <EOL> from django . utils import simplejson <EOL> from dynamicresponse . json_response import JsonResponse <EOL> class ModelWithSerializeFields ( models . Model ) : <EOL> title = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:200> ) <EOL> text = models . TextField ( '<STR_LIT>' ) <EOL> _password = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:100> ) <EOL> def serialize_fields ( self ) : <EOL> return [ <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT:title>' <EOL> ] <EOL> class ModelWithoutSerializeFields ( models . Model ) : <EOL> title = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:200> ) <EOL> text = models . TextField ( '<STR_LIT>' ) <EOL> _password = models . CharField ( '<STR_LIT>' , max_length = <NUM_LIT:100> ) <EOL> class JsonResponseTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . testObj = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) } <EOL> self . jsonres = JsonResponse ( self . testObj ) <EOL> self . modelWithSerializeFields = JsonResponse ( ModelWithSerializeFields ( title = '<STR_LIT>' , <EOL> text = '<STR_LIT>' , <EOL> _password = '<STR_LIT>' ) ) <EOL> self . modelbaseWithoutSerializeFields = ModelWithoutSerializeFields ( title = '<STR_LIT>' , <EOL> text = '<STR_LIT>' , <EOL> _password = '<STR_LIT>' ) <EOL> self . modelWithoutSerializeFields = JsonResponse ( self . modelbaseWithoutSerializeFields ) <EOL> def testIsInstanceOfHttpResponse ( self ) : <EOL> self . assertTrue ( isinstance ( self . jsonres , HttpResponse ) , '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( self . modelWithSerializeFields , HttpResponse ) , '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( self . modelWithoutSerializeFields , HttpResponse ) , '<STR_LIT>' ) <EOL> def testSetsCorrectMimetype ( self ) : <EOL> self . assertEqual ( self . jsonres [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( self . modelWithSerializeFields [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( self . modelWithoutSerializeFields [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> def testDictContentConvertsToJson ( self ) : <EOL> result = simplejson . loads ( self . jsonres . content ) <EOL> for key , value in result . items ( ) : <EOL> self . assertEqual ( self . testObj . get ( key ) . __str__ ( ) , value . __str__ ( ) ) <EOL> def testModelWithSerializeFieldsConvertsToJson ( self ) : <EOL> to_equal = { u'<STR_LIT:id>' : None , u'<STR_LIT:title>' : u'<STR_LIT>' } <EOL> result = simplejson . loads ( self . modelWithSerializeFields . content ) <EOL> for key , value in result . items ( ) : <EOL> self . assertEqual ( to_equal . get ( key ) . __str__ ( ) , value . __str__ ( ) ) <EOL> def testModelWithoutSerializeFieldsConvertsToJson ( self ) : <EOL> to_equal = { u'<STR_LIT:text>' : u'<STR_LIT>' , u'<STR_LIT:title>' : u'<STR_LIT>' , u'<STR_LIT:id>' : None } <EOL> result = simplejson . loads ( self . modelWithoutSerializeFields . content ) <EOL> for key , value in result . items ( ) : <EOL> self . assertEqual ( to_equal . get ( key ) . __str__ ( ) , value . __str__ ( ) ) <EOL> def testModelsWithDynamiclyAddedFieldsConvertsToJson ( self ) : <EOL> to_equal = { u'<STR_LIT:text>' : u'<STR_LIT>' , u'<STR_LIT:title>' : u'<STR_LIT>' , u'<STR_LIT:id>' : None , u'<STR_LIT>' : u'<STR_LIT>' } <EOL> self . modelbaseWithoutSerializeFields . dummy = "<STR_LIT>" <EOL> self . modelbaseWithoutSerializeFields . _dummy = "<STR_LIT>" <EOL> result = simplejson . loads ( JsonResponse ( self . modelbaseWithoutSerializeFields ) . content ) <EOL> for key , value in result . items ( ) : <EOL> self . assertEqual ( to_equal . get ( key ) . __str__ ( ) , value . __str__ ( ) ) </s>
