Skip to content

Commit

Permalink
updating code to pass the travis ci test and adding more test cases t…
Browse files Browse the repository at this point in the history
…o validate new changes
  • Loading branch information
mukultaneja authored and sanand0 committed Aug 1, 2017
1 parent 16b2c0b commit 5e69488
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 10 additions & 1 deletion tests/test_xmljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,18 @@ def test_data(self):
'<alice charlie="david">bob</alice>')

# Nested elements with more than one children
eq('{"Data": {"attributes": {"version": 9.0,"{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation": "comp.xsd"},"children": [{"Airport": {"attributes": {"country": "Samoa","city": "Apia","name": "Faleolo Intl","lat": -13.8296668231487,"lon": -171.997166723013,"alt": "17.678M","ident": "NSFA"},"children": [{"Services": {"Fuel": {"attributes": {"type": "JETA","availability": "YES"}}}},{"Tower": {"attributes": {"lat": -13.8320958986878,"lon": -171.998676359653,"alt": "0.0M"}},"Runway": {"attributes": {"lat": -13.8300792127848,"lon": -172.008545994759,"alt": "17.678M","surface": "ASPHALT","heading": 89.3199996948242,"length": "2999.23M","width": "45.11M","number": 8,"designator": "NONE"}}}]}}]}}',
eq('{"Data":{"attributes":{"version":9.0,"{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation":"comp.xsd"},"children":[{"Airport":{"attributes":{"country":"Samoa","city":"Apia","name":"Faleolo Intl","lat":-13.8296668231487,"lon":-171.997166723013,"alt":"17.678M","ident":"NSFA"},"children":[{"Services":{"Fuel":{"attributes":{"type":"JETA","availability":"YES"}}}},{"Tower":{"attributes":{"lat":-13.8320958986878,"lon":-171.998676359653,"alt":"0.0M"}}},{"Runway":{"attributes":{"lat":-13.8300792127848,"lon":-172.008545994759,"alt":"17.678M","surface":"ASPHALT","heading":89.3199996948242,"length":"2999.23M","width":"45.11M","number":8,"designator":"NONE"}}}]}}]}}',
'<Data version="9.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="comp.xsd"><Airport country="Samoa" city="Apia" name="Faleolo Intl" lat="-13.8296668231487" lon="-171.997166723013" alt="17.678M" ident="NSFA"><Services><Fuel type="JETA" availability="YES"/></Services><Tower lat="-13.8320958986878" lon="-171.998676359653" alt="0.0M"></Tower><Runway lat="-13.8300792127848" lon="-172.008545994759" alt="17.678M" surface="ASPHALT" heading="89.3199996948242" length="2999.23M" width="45.11M" number="08" designator="NONE"></Runway></Airport></Data>')

eq('{"root":{"children":[{"a":{"attributes":{"x": 1},"children":[{"b":{"c":{}}}]}},{"d":{"attributes":{"x": 1},"children":[{"e":{"f":{}}}]}},{"g":{"attributes":{"x": 1},"children":[{"h":{"i":{}}}]}}]}}',
'<root><a x="1"><b><c/></b></a><d x="1"><e><f/></e></d><g x="1"><h><i/></h></g></root>')

eq('{"root": {"children": [{"a": {"attributes": {"x": 1}, "children": [{"b": {"c": {"d": {"attributes": {"x": 1}}}}}]}}, {"e": {"attributes": {"x": 1}, "children": [{"f": {"g": {"h": {"attributes": {"x": 1}}}}}]}}, {"i": {"attributes": {"x": 1}, "children": [{"j": {"k": {"l": {"attributes": {"x": 1}}}}}]}}]}}',
'<root><a x="1"><b><c><d x="1"/></c></b></a><e x="1"><f><g><h x="1"/></g></f></e><i x="1"><j><k><l x="1"/></k></j></i></root>')

eq('{"root": {"a": {"attributes": {"x": 1}, "children": [{"b": {"attributes": {"x": 1}, "children": [{"c": {"attributes": {"x": 1}, "children": [{"d": {}}]}}]}}]}}}',
'<root><a x="1"><b x="1"><c x="1"><d/></c></b></a></root>')


class TestCobra(TestXmlJson):
@unittest.skip('To be written')
Expand Down
12 changes: 3 additions & 9 deletions xmljson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,10 @@ def data(self, root):
children_list = [self._fromstring(text), ]

count = Counter(child.tag for child in children)

for child in children:
child_data = self.data(child)
if (count[child.tag] == 1
and len(children_list) > 0
and isinstance(children_list[-1], dict)):
# Merge keys to existing dictionary
children_list[-1].update(child_data)
else:
# Add additional text
children_list.append(self.data(child))
children_list.append(self.data(child))

# Flatten children
if len(root.attrib) == 0 and len(children_list) == 1:
Expand Down Expand Up @@ -281,7 +275,7 @@ def data(self, root):
for child in children:
child_data = self.data(child)
if (count[child.tag] == 1
and len(children_list) > 0
and len(children_list) > 1
and isinstance(children_list[-1], dict)):
# Merge keys to existing dictionary
children_list[-1].update(child_data)
Expand Down

0 comments on commit 5e69488

Please sign in to comment.