Example #1
0
def test_node_to_dict2():
    """
    should return dict of node double nested
    """

    doc = Document()

    outer = doc.createElement("outer")
    doc.appendChild(outer)

    mid1 = doc.createElement("mid1")
    outer.appendChild(mid1)
    mid2 = doc.createElement("mid2")
    outer.appendChild(mid2)

    inner1 = doc.createElement("inner1")
    inner2 = doc.createElement("inner2")
    mid1.appendChild(inner1)
    mid2.appendChild(inner2)

    inner1_text = doc.createTextNode("one")
    inner2_text = doc.createTextNode("two")
    inner1.appendChild(inner1_text)
    inner2.appendChild(inner2_text)

    expected_dict = {'outer': {'mid2': {'inner2': 'two'}, 'mid1': {'inner1': 'one'}}}
    xml_dict = xml.node_to_dict(doc)

    assert xml_dict == expected_dict
Example #2
0
def test_node_to_dict3():
    """
    should return empty dict when sent empty doc
    """
    expected_dict = {}
    xml_dict = xml.node_to_dict(Document())

    assert xml_dict == expected_dict
Example #3
0
def test_node_to_dict1():
    """
    should return dict of node.
    """

    doc = Document()

    outer = doc.createElement("outer")
    doc.appendChild(outer)

    inner1 = doc.createElement("inner1")
    inner2 = doc.createElement("inner2")
    outer.appendChild(inner1)
    outer.appendChild(inner2)

    inner1_text = doc.createTextNode("one")
    inner2_text = doc.createTextNode("two")
    inner1.appendChild(inner1_text)
    inner2.appendChild(inner2_text)

    expected_dict = {'outer': {'inner2': 'two', 'inner1': 'one'}}
    xml_dict = xml.node_to_dict(doc)

    assert xml_dict == expected_dict