Skip to content

Commit

Permalink
API - to validate XML, serialized and deserialized
Browse files Browse the repository at this point in the history
  • Loading branch information
jamilatta committed Nov 13, 2012
1 parent 2870c75 commit 8da33e7
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
40 changes: 40 additions & 0 deletions schema.py
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# coding: utf-8

import xmltodict
from StringIO import StringIO
from lxml import etree


class Schema:

def __init__(self, xsd_input=None):
"""
Parse the XSD and attribute
"""
if xsd_input:
xmlschema_doc = etree.parse(StringIO(xsd_input))
self.xmlschema = etree.XMLSchema(xmlschema_doc)

def validate(self, xml_input):
"""
Validate XML
"""
parsed_xml = etree.parse(StringIO(xml_input))
if self.xmlschema.validate(parsed_xml):
return True
else:
return False

def deserialized(self, xml_input, *args, **kwargs):
"""
Convert XML to dict object
"""
if self.validate(xml_input):
return xmltodict.parse(xml_input, *args, **kwargs)

def serialized(self, dict_input, **kwargs):
"""
Convert dict to XML
"""
return xmltodict.unparse(dict_input, **kwargs)
45 changes: 45 additions & 0 deletions test.py
@@ -0,0 +1,45 @@
# coding: utf-8

import unittest
import test_asserts
from schema import Schema


class SchemaTestCase(unittest.TestCase):

def setUp(self):

self.xsd_string = test_asserts.XSD
self.xml_string = test_asserts.XML
self.xml_string_invalid = test_asserts.XML_INVALID

def test_instance(self):
sch = Schema(self.xsd_string)
self.assertIsInstance(sch, Schema)

def test_validate(self):
sch = Schema(self.xsd_string)
validated = sch.validate(self.xml_string)
self.assertTrue(validated)

def test_deserialized(self):
sch = Schema(self.xsd_string)
des = sch.deserialized(self.xml_string)
self.assertIsInstance(des, dict)
self.assertIn('wizard', des)

def test_serialized(self):
sch = Schema()
ser = sch.serialized(test_asserts.DICT_OBJ)
self.assertEqual(ser,
'<?xml version="1.0" encoding="utf-8"?>\n<a><c>2</c><b>1</b></a>')

def test_invalid_xml(self):
sch = Schema(self.xsd_string)
invalid = sch.validate(self.xml_string_invalid)
self.assertFalse(invalid)

def test_deserialized_invalid_xml(self):
sch = Schema(self.xsd_string)
des = sch.deserialized(self.xml_string_invalid)
self.assertNotIsInstance(des, dict)
46 changes: 46 additions & 0 deletions test_asserts.py
@@ -0,0 +1,46 @@
# coding: utf-8

DICT_OBJ = dict({'a': {'b': '1', 'c': '2'}})

XSD = '''<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/wizard"
xmlns:tns="http://www.example.org/wizard"
elementFormDefault="qualified">
<complexType name="Wizard">
<sequence>
<element name="startpage" type="IDREF" />
<element name="name" type="string" />
<element name="welcometext" type="string" />
<element name="choicepage" type="tns:ChoicePage" />
</sequence>
</complexType>
<complexType name="ChoicePage">
<sequence>
<element name="title" type="string" />
</sequence>
<attribute name="id" type="ID" />
</complexType>
<element name="wizard" type="tns:Wizard" />
</schema>'''


XML = '''<?xml version="1.0" encoding="UTF-8"?>
<wizard xmlns="http://www.example.org/wizard">
<startpage>start</startpage>
<name>My Example Setup</name>
<welcometext>Welcome to this little demo application.</welcometext>
<choicepage id="start">
<title>Wizard Page One</title>
</choicepage>
</wizard>'''

XML_INVALID = '''<?xml version="1.0" encoding="UTF-8"?>
<wizard>
<startpage>start</startpage>
<name>My Example Setup</name>
<welcometext>Welcome to this little demo application.</welcometext>
<choicepage id="start">
<title>Wizard Page One</title>
</choicepage>
</wizard>'''

0 comments on commit 8da33e7

Please sign in to comment.