1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
from textwrap import dedent
from unittest import mock
import pytest
from dict2xml import Converter
class TestConverter:
class TestBuilding:
def test_creates_an_indenter_a_node_and_then_calls_serialize_on_the_node_with_the_indenter(
self,
):
wrap = mock.Mock("wrap")
indent = mock.Mock("indent")
newlines = mock.Mock("newlines")
converter = Converter(wrap, indent, newlines)
node = mock.Mock(name="node")
FakeNode = mock.Mock(name="Node", return_value=node)
serialized = mock.Mock(name="serialized")
node.serialize.return_value = serialized
indenter = mock.Mock(name="indenter")
make_indenter = mock.Mock(name="make_indenter", return_value=indenter)
mip = mock.patch.object(converter, "_make_indenter", make_indenter)
fnp = mock.patch("dict2xml.logic.Node", FakeNode)
data = mock.Mock(name="data")
with mip, fnp:
assert converter.build(data) is serialized
FakeNode.assert_called_once_with(
wrap=wrap,
data=data,
iterables_repeat_wrap=True,
closed_tags_for=None,
data_sorter=None,
)
node.serialize.assert_called_once_with(indenter)
def tes_does_not_repeat_the_wrap_of_iterables_repeat_wrap_is_false(self):
example = {
"array": [
{"item": {"string1": "string", "string2": "string"}},
{"item": {"string1": "other string", "string2": "other string"}},
]
}
result = Converter("").build(example, iterables_repeat_wrap=False)
assert (
result
== dedent(
"""
<array>
<item>
<string1>string</string1>
<string2>string</string2>
</item>
<item>
<string1>other string</string1>
<string2>other string</string2>
</item>
</array>
"""
).strip()
)
def test_can_produce_self_closing_tags(self):
example = {
"item1": None,
"item2": {"string1": "", "string2": None},
"item3": "special",
}
result = Converter("").build(example, closed_tags_for=[None])
assert (
result
== dedent(
"""
<item1/>
<item2>
<string1></string1>
<string2/>
</item2>
<item3>special</item3>
"""
).strip()
)
result = Converter("").build(example, closed_tags_for=[None, ""])
assert (
result
== dedent(
"""
<item1/>
<item2>
<string1/>
<string2/>
</item2>
<item3>special</item3>
"""
).strip()
)
result = Converter("").build(example, closed_tags_for=["special"])
print(result)
assert (
result
== dedent(
"""
<item1>None</item1>
<item2>
<string1></string1>
<string2>None</string2>
</item2>
<item3/>
"""
).strip()
)
class TestMakingIndentationFunction:
@pytest.fixture()
def V(self):
class V:
with_indent = Converter(indent=" ", newlines=True)
without_indent = Converter(indent="", newlines=True)
without_newlines = Converter(newlines=False)
def assertIndenter(self, indenter, nodes, wrap, expected):
result = "".join([wrap, indenter(nodes, wrap), wrap])
assert result == expected.strip()
return V()
class TestNoNewlines:
def test_joins_nodes_with_empty_string(self, V):
indenter = V.without_newlines._make_indenter()
assert indenter(["a", "b", "c"], True) == "abc"
assert indenter(["d", "e", "f"], False) == "def"
class TestWithNewlines:
class TestNoIndentation:
def test_joins_with_newlines_and_never_indents(self, V):
# Wrap is added to expected output via test_indenter
indenter = V.without_indent._make_indenter()
V.assertIndenter(
indenter,
["a", "b", "c"],
"<>",
dedent(
"""
<>
a
b
c
<>"""
),
)
class TestWithIndentation:
def test_joins_with_newlines_and_indents_if_there_is_a_wrapping_tag(self, V):
# Wrap is added to expected output via test_indenter
indenter = V.with_indent._make_indenter()
V.assertIndenter(
indenter,
["a", "b", "c"],
"<>",
dedent(
"""
<>
a
b
c
<>"""
),
)
def test_joins_with_newlines_but_does_not_indent_if_no_wrapping_tag(self, V):
indenter = V.with_indent._make_indenter()
V.assertIndenter(
indenter,
["a", "b", "c"],
"",
dedent(
"""
a
b
c"""
),
)
def test_it_reindents_each_new_line(self, V):
node1 = dedent(
"""
a
b
c
d
e
"""
).strip()
node2 = "f"
node3 = dedent(
"""
f
g
h
"""
).strip()
# Wrap is added to expected output via test_indenter
indenter = V.with_indent._make_indenter()
V.assertIndenter(
indenter,
[node1, node2, node3],
"<>",
dedent(
"""
<>
a
b
c
d
e
f
f
g
h
<>
"""
),
)
|