<s> from dockit . core . serializers . python import Serializer , Deserializer <EOL> from django . utils import unittest <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from dockit . tests . serializers . common import ParentDocument , ChildDocument , ChildSchema <EOL> class PythonSerializerTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . serializer = Serializer ( ) <EOL> def test_serialize ( self ) : <EOL> child = ChildDocument ( charfield = '<STR_LIT:bar>' ) <EOL> child . save ( ) <EOL> parent = ParentDocument ( title = '<STR_LIT:foo>' , subdocument = child , subschema = ChildSchema ( ct = ContentType . objects . all ( ) [ <NUM_LIT:0> ] ) ) <EOL> assert parent . natural_key <EOL> assert '<STR_LIT>' in parent . _primitive_data <EOL> data = parent . to_portable_primitive ( parent ) <EOL> self . assertTrue ( isinstance ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , tuple ) , str ( data ) ) <EOL> self . assertTrue ( isinstance ( data [ '<STR_LIT>' ] , dict ) , "<STR_LIT>" % data [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( '<STR_LIT>' in data , str ( data ) ) <EOL> result = self . serializer . serialize ( [ child , parent ] ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:2> ) <EOL> entry = result [ <NUM_LIT:1> ] <EOL> self . assertTrue ( '<STR_LIT>' in entry ) <EOL> self . assertEqual ( len ( entry [ '<STR_LIT>' ] ) , <NUM_LIT:3> , str ( entry ) ) <EOL> del data [ '<STR_LIT>' ] <EOL> del data [ '<STR_LIT>' ] <EOL> self . assertEqual ( entry [ '<STR_LIT>' ] , data ) <EOL> def test_deserialize ( self ) : <EOL> payload = [ { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:bar>' } , '<STR_LIT>' : ChildDocument . _meta . collection , '<STR_LIT>' : { '<STR_LIT>' : u'<STR_LIT:bar>' } } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' : ParentDocument . _meta . collection , '<STR_LIT>' : { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:bar>' } , '<STR_LIT:title>' : u'<STR_LIT:foo>' } } ] <EOL> objects = list ( Deserializer ( payload ) ) <EOL> self . assertEqual ( len ( objects ) , <NUM_LIT:2> ) <EOL> obj = objects [ <NUM_LIT:1> ] <EOL> self . assertEqual ( obj . object [ '<STR_LIT:title>' ] , '<STR_LIT:foo>' ) <EOL> saved_objects = list ( ) <EOL> for obj in objects : <EOL> saved_obj = obj . save ( ) <EOL> self . assertTrue ( '<STR_LIT>' in saved_obj . _primitive_data ) <EOL> self . assertTrue ( saved_obj . pk ) <EOL> saved_objects . append ( saved_obj ) <EOL> for obj in saved_objects : <EOL> obj . normalize_portable_primitives ( ) <EOL> obj . save ( ) <EOL> self . assertEqual ( saved_objects [ <NUM_LIT:1> ] . subdocument , saved_objects [ <NUM_LIT:0> ] ) <EOL> subdoc = saved_objects [ <NUM_LIT:1> ] . _primitive_data [ '<STR_LIT>' ] <EOL> self . assertEqual ( subdoc , saved_objects [ <NUM_LIT:0> ] . pk ) </s>
