py34: heat.tests.test_common_serializers

* Convert to bytes where necessary
* Fix ordering of response to be consistent
* convert xml/json strings to dicts before
  comparing/asserting

partial blueprint heat-python34-support

Change-Id: I10cdcea7a06f3917839801d630995cd63608e46c
This commit is contained in:
Sirushti Murugesan 2015-08-12 00:25:39 +05:30
parent 1db54fd3de
commit d8304bdc79
3 changed files with 38 additions and 17 deletions

View File

@ -44,7 +44,7 @@ class JSONResponseSerializer(object):
def default(self, response, result):
response.content_type = 'application/json'
response.body = self.to_json(result)
response.body = six.b(self.to_json(result))
# Escape XML serialization for these keys, as the AWS API defines them as

View File

@ -17,6 +17,9 @@
import collections
import datetime
from lxml import etree
from oslo_serialization import jsonutils as json
import six
import webob
from heat.common import serializers
@ -46,7 +49,7 @@ class JSONResponseSerializerTest(common.HeatTestCase):
])
expected = '{"is_public": true, "name": [{"name1": "test"}]}'
actual = serializers.JSONResponseSerializer().to_json(fixture)
self.assertEqual(expected, actual)
self.assertEqual(json.loads(expected), json.loads(actual))
def test_to_json_with_objects(self):
fixture = collections.OrderedDict([
@ -55,7 +58,7 @@ class JSONResponseSerializerTest(common.HeatTestCase):
])
expected = '{"is_public": true, "value": "(1+2j)"}'
actual = serializers.JSONResponseSerializer().to_json(fixture)
self.assertEqual(expected, actual)
self.assertEqual(json.loads(expected), json.loads(actual))
def test_default(self):
fixture = {"key": "value"}
@ -66,28 +69,36 @@ class JSONResponseSerializerTest(common.HeatTestCase):
response.headerlist))
self.assertEqual(1, len(content_types))
self.assertEqual('application/json', response.content_type)
self.assertEqual('{"key": "value"}', response.body)
self.assertEqual(b'{"key": "value"}', response.body)
class XMLResponseSerializerTest(common.HeatTestCase):
def _recursive_dict(self, element):
return element.tag, dict(
map(self._recursive_dict, element)) or element.text
def test_to_xml(self):
fixture = {"key": "value"}
expected = '<key>value</key>'
expected = b'<key>value</key>'
actual = serializers.XMLResponseSerializer().to_xml(fixture)
self.assertEqual(expected, actual)
def test_to_xml_with_date_format_value(self):
fixture = {"date": datetime.datetime(1, 3, 8, 2)}
expected = '<date>0001-03-08 02:00:00</date>'
expected = b'<date>0001-03-08 02:00:00</date>'
actual = serializers.XMLResponseSerializer().to_xml(fixture)
self.assertEqual(expected, actual)
def test_to_xml_with_list(self):
fixture = {"name": ["1", "2"]}
expected = '<name><member>1</member><member>2</member></name>'
expected = b'<name><member>1</member><member>2</member></name>'
actual = serializers.XMLResponseSerializer().to_xml(fixture)
self.assertEqual(expected, actual)
actual_xml_tree = etree.XML(actual)
actual_xml_dict = self._recursive_dict(actual_xml_tree)
expected_xml_tree = etree.XML(expected)
expected_xml_dict = self._recursive_dict(expected_xml_tree)
self.assertEqual(expected_xml_dict, actual_xml_dict)
def test_to_xml_with_more_deep_format(self):
# Note we expect tree traversal from one root key, which is compatible
@ -100,11 +111,16 @@ class XMLResponseSerializerTest(common.HeatTestCase):
])])
]))
])
expected = ('<aresponse><is_public>True</is_public>'
'<name><member><name1>test</name1></member></name>'
'</aresponse>')
expected = six.b('<aresponse><is_public>True</is_public>'
'<name><member><name1>test</name1></member></name>'
'</aresponse>')
actual = serializers.XMLResponseSerializer().to_xml(fixture)
self.assertEqual(expected, actual)
actual_xml_tree = etree.XML(actual)
actual_xml_dict = self._recursive_dict(actual_xml_tree)
expected_xml_tree = etree.XML(expected)
expected_xml_dict = self._recursive_dict(expected_xml_tree)
self.assertEqual(expected_xml_dict, actual_xml_dict)
def test_to_xml_with_json_only_keys(self):
# Certain keys are excluded from serialization because CFN
@ -116,11 +132,15 @@ class XMLResponseSerializerTest(common.HeatTestCase):
('Metadata', {"name2": "test2"}),
]))
])
expected = ('<aresponse><is_public>True</is_public>'
'<TemplateBody>{"name1": "test"}</TemplateBody>'
'<Metadata>{"name2": "test2"}</Metadata></aresponse>')
expected = six.b('<aresponse><is_public>True</is_public>'
'<TemplateBody>{"name1": "test"}</TemplateBody>'
'<Metadata>{"name2": "test2"}</Metadata></aresponse>')
actual = serializers.XMLResponseSerializer().to_xml(fixture)
self.assertEqual(expected, actual)
actual_xml_tree = etree.XML(actual)
actual_xml_dict = self._recursive_dict(actual_xml_tree)
expected_xml_tree = etree.XML(expected)
expected_xml_dict = self._recursive_dict(expected_xml_tree)
self.assertEqual(expected_xml_dict, actual_xml_dict)
def test_default(self):
fixture = {"key": "value"}
@ -131,4 +151,4 @@ class XMLResponseSerializerTest(common.HeatTestCase):
response.headerlist))
self.assertEqual(1, len(content_types))
self.assertEqual('application/xml', response.content_type)
self.assertEqual('<key>value</key>', response.body)
self.assertEqual(b'<key>value</key>', response.body)

View File

@ -93,6 +93,7 @@ heat.tests.test_cloudwatch
heat.tests.test_common_context
heat.tests.test_common_param_utils
heat.tests.test_common_policy
heat.tests.test_common_serializers
heat.tests.test_common_service_utils
heat.tests.test_constraints
heat.tests.test_crypt