Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_exception_raised_if_bad_data():
"""Verifies SerializationException is raised when trying to serialize bad data"""
data = "We can't serialize this"
serializer = XMLSerializer()
with pytest.raises(SerializationException):
serializer.serialize(data=data)
def test_serialization_exception():
"""Test error string is formatted correctly"""
error = SerializationException("Something wrong with serialization")
assert str(error) == "Serialization exception. Something wrong with serialization"
def serialize(self, data):
"""
Serializes data to XML so that it can be
sent to backend, if data is not a dictionary
raises a SerializationException
Params:
`data`: A dictionary containing the data to serialize
Return:
`xml`: Returns a string containing data serialized to XML
"""
if not isinstance(data, dict):
raise SerializationException("Can't serialize data, must be a dictionary.")
stream = StringIO()
self.generator = XMLGenerator(stream, "utf-8")
self.generator.startDocument()
self.generator.startElement("request", {})
self._parse(data)
self.generator.endElement("request")
self.generator.endDocument()
return stream.getvalue()