def test_to_xml_multirepr(self): serializer = Serializer() binary_xml = serializer.to_xml(self.obj_list) unicode_xml = binary_xml.decode('utf-8') self.assertEqual( unicode_xml, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object><object><content>The dog ate my cat today. He looks seriously uncomfortable.</content><created>2010-03-31T20:05:00</created><id type="integer">2</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>another-post</slug><title>Another Post</title><updated>2010-03-31T20:05:00</updated></object><object><content>My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.</content><created>2010-04-01T20:05:00</created><id type="integer">4</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>recent-volcanic-activity</slug><title>Recent Volcanic Activity.</title><updated>2010-04-01T20:05:00</updated></object><object><content>Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!</content><created>2010-04-02T10:05:00</created><id type="integer">6</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>grannys-gone</slug><title>Granny\'s Gone</title><updated>2010-04-02T10:05:00</updated></object></objects>' )
def test_to_xml_single(self): serializer = Serializer() resource = self.obj_list[0] binary_xml = serializer.to_xml(resource) unicode_xml = binary_xml.decode('utf-8') self.assertEqual( unicode_xml, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object>' )
def test_round_trip_xml(self): serializer = Serializer() sample_data = self.get_sample2() serialized = serializer.to_xml(sample_data) # "response" tags need to be changed to "request" to deserialize properly. # A string substitution works here. serialized = serialized.decode('utf-8').replace('response', 'request') unserialized = serializer.from_xml(serialized) self.assertEqual(sample_data, unserialized)
def test_to_xml2(self): serializer = Serializer() sample_2 = self.get_sample2() binary_xml = serializer.to_xml(sample_2) unicode_xml = binary_xml.decode('utf-8') self.assertEqual( unicode_xml, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><false type="boolean">False</false><somehash type="hash"><foo>bar</foo><pi type="float">3.14</pi></somehash><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somestring>hello</somestring><true type="boolean">True</true></response>' )
def test_to_xml(self): serializer = Serializer() sample_1 = self.get_sample1() # This needs a little explanation. # From http://lxml.de/parsing.html, what comes out of ``tostring`` # (despite encoding as UTF-8) is a bytestring. This is because that's # what other libraries expect (& will do the decode). We decode here # so we can make extra special sure it looks right. binary_xml = serializer.to_xml(sample_1) unicode_xml = binary_xml.decode('utf-8') self.assertEqual( unicode_xml, u'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><date_joined>2010-03-27</date_joined><name>Daniel</name><snowman>☃</snowman></response>' )
def test_to_xml_nested(self): serializer = Serializer() resource = self.obj_list[0] data = { 'stuff': { 'foo': 'bar', 'object': resource, } } binary_xml = serializer.to_xml(data) unicode_xml = binary_xml.decode('utf-8') self.assertEqual( unicode_xml, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object></stuff></response>' )