Skip to content

Commit

Permalink
Add LICENSE to packaged file, update desc, refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanand0 committed Sep 13, 2016
1 parent 9690e86 commit 91b7d8d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ environment:

git clone git@github.com:your_user_id/xmljson.git

3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development::
3. Install your local copy into a virtualenv. If you have `virtualenvwrapper <http://virtualenvwrapper.readthedocs.org/en/latest/install.html>`__ installed, this is how you set up your fork for local development::

$ mkvirtualenv xmljson
$ cd xmljson/
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
setup(
name='xmljson',
version=xmljson.__version__,
description="xmljson converts XML into Python dictionary structures "
"(trees, like in JSON) and vice-versa.",
description="Converts XML into JSON/Python dicts/arrays and vice-versa.",
long_description=readme + '\n\n' + history,
author="S Anand",
author_email='root.node@gmail.com',
Expand Down
36 changes: 18 additions & 18 deletions tests/test_xmljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import json
import unittest

from collections import OrderedDict as od
from collections import OrderedDict as Dict
from lxml.etree import tostring as tostring, fromstring
from lxml.doctestcompare import LXMLOutputChecker
import lxml.html
Expand All @@ -39,23 +39,23 @@ def check_etree(self, conv, tostring=tostring, fromstring=fromstring):
checker = LXMLOutputChecker()
eq = checker.compare_docs

def assertEqual(obj, *strings):
def compare(obj, *strings):
tree = conv.etree(obj)
self.assertEqual(len(tree), len(strings))
for left, right in zip(tree, strings):
if not eq(left, fromstring(right)):
raise AssertionError('%s != %s' % (decode(tostring(left)), right))

return assertEqual
return compare

def check_data(self, conv):
'Returns method(jsonstring, xmlstring) that unparses both and checks'
def assertEqual(jsonstring, xmlstring):
first = json.loads(jsonstring, object_pairs_hook=od)
def compare(jsonstring, xmlstring):
first = json.loads(jsonstring, object_pairs_hook=Dict)
second = conv.data(fromstring(xmlstring))
self.assertEqual(first, second)

return assertEqual
return compare


class TestBadgerFish(TestXmlJson):
Expand All @@ -71,7 +71,7 @@ def test_etree(self, converter=None):
eq({'animal': {'@name': 1}}, '<animal name="1"/>')
eq({'animal': {'@name': 'Deka', '$': 'is my cat'}},
'<animal name="Deka">is my cat</animal>')
eq({'animal': od([('dog', 'Charlie'), ('cat', 'Deka')])},
eq({'animal': Dict([('dog', 'Charlie'), ('cat', 'Deka')])},
'<animal><dog>Charlie</dog><cat>Deka</cat></animal>')
eq({'animal': {'dog': ['Charlie', 'Mad Max']}},
'<animal><dog>Charlie</dog><dog>Mad Max</dog></animal>')
Expand All @@ -85,7 +85,7 @@ def test_etree(self, converter=None):
# Test edge cases
eq('x', '<x/>') # Strings become elements
eq({}) # Empty objects become empty nodes
eq(od([ # Multiple keys become multiple nodes
eq(Dict([ # Multiple keys become multiple nodes
('x', {'@x': 1}),
('y', 'z')
]), '<x x="1"/>', '<y>z</y>')
Expand All @@ -100,7 +100,7 @@ def test_etree(self, converter=None):
eq({'alice': {'$': 'bob'}}, '<alice>bob</alice>')

# Nested elements become nested properties
eq({'alice': od([
eq({'alice': Dict([
('bob', {'$': 'charlie'}),
('david', {'$': 'edgar'})])},
'<alice><bob>charlie</bob><david>edgar</david></alice>')
Expand All @@ -122,7 +122,7 @@ def test_html(self):

eq = self.check_etree(html_converter, tostring=lxml.html.tostring,
fromstring=lxml.html.fromstring)
eq({'div': od([
eq({'div': Dict([
('p', {'$': 'paragraph'}),
('hr', {}),
('ul', {'li': [{'$': '1'}, {'$': '2'}]}),
Expand Down Expand Up @@ -210,9 +210,9 @@ def test_etree(self):
eq({'animal': {'name': 1}}, '<animal name="1"/>')
eq({'animal': {'$t': 'is my cat'}},
'<animal>is my cat</animal>')
eq({'animal': od([('dog', {'$t': 'Charlie'}), ('cat', {'$t': 'Deka'})])},
eq({'animal': Dict([('dog', {'$t': 'Charlie'}), ('cat', {'$t': 'Deka'})])},
'<animal><dog>Charlie</dog><cat>Deka</cat></animal>')
eq({'animal': od([('dog', 'Charlie'), ('cat', 'Deka')])},
eq({'animal': Dict([('dog', 'Charlie'), ('cat', 'Deka')])},
'<animal dog="Charlie" cat="Deka"/>')
eq({'animal': {'dog': ['Charlie', 'Mad Max']}},
'<animal><dog>Charlie</dog><dog>Mad Max</dog></animal>')
Expand All @@ -224,7 +224,7 @@ def test_etree(self):
# Test edge cases
eq('x', '<x/>') # Strings become elements
eq({}) # Empty objects become empty nodes
eq(od([ # Multiple keys become multiple nodes
eq(Dict([ # Multiple keys become multiple nodes
('x', {}),
('y', 'z')
]), '<x/>', '<y>z</y>')
Expand All @@ -238,7 +238,7 @@ def test_etree(self):
eq({'alice': {'$t': 'bob'}}, '<alice>bob</alice>')

# Nested elements become nested properties
eq({'alice': od([
eq({'alice': Dict([
('bob', {'$t': 'charlie'}),
('david', {'$t': 'edgar'})])},
'<alice><bob>charlie</bob><david>edgar</david></alice>')
Expand Down Expand Up @@ -312,23 +312,23 @@ def test_etree(self):
eq({'animal': {}}, '<animal/>')
eq({'animal': 'Deka'}, '<animal>Deka</animal>')
eq({'animal': 1}, '<animal>1</animal>')
eq({'animal': od([('dog', 'Charlie'), ('cat', 'Deka')])},
eq({'animal': Dict([('dog', 'Charlie'), ('cat', 'Deka')])},
'<animal><dog>Charlie</dog><cat>Deka</cat></animal>')
eq({'animal': {'dog': ['Charlie', 'Mad Max']}},
'<animal><dog>Charlie</dog><dog>Mad Max</dog></animal>')

# Test edge cases
eq('x', '<x/>') # Strings become elements
eq({}) # Empty objects become empty nodes
eq(od([ # Multiple keys become multiple nodes
eq(Dict([ # Multiple keys become multiple nodes
('x', 'a'),
('y', 'b')
]), '<x>a</x>', '<y>b</y>')
with self.assertRaises(Exception):
eq({'x': {'@x': 1}}, '<x x="1"/>')

# Nested elements
eq({'alice': od([
eq({'alice': Dict([
('bob', {'charlie': {}}),
('david', {'edgar': {}})])},
'<alice><bob><charlie/></bob><david><edgar/></david></alice>')
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_data(self):
}
]
}
}''', object_pairs_hook=od)
}''', object_pairs_hook=Dict)

eq(json.dumps(data), result)

Expand Down
2 changes: 1 addition & 1 deletion xmljson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _tostring(value):
value = 'true'
elif value is False:
value = 'false'
return unicode(value)
return unicode(value) # noqa: convert to whatever native unicode repr

@staticmethod
def _fromstring(value):
Expand Down

0 comments on commit 91b7d8d

Please sign in to comment.