1
|
|
|
# |
2
|
|
|
# Copyright (C) 2012 - 2017 Satoru SATOH <ssato @ redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=missing-docstring, invalid-name, protected-access |
6
|
|
|
from __future__ import absolute_import |
7
|
|
|
import os.path |
8
|
|
|
import unittest |
9
|
|
|
|
10
|
|
|
import anyconfig.backend.xml as TT |
11
|
|
|
import anyconfig.backend.tests.ini |
12
|
|
|
import anyconfig.tests.common |
13
|
|
|
import anyconfig.compat |
14
|
|
|
|
15
|
|
|
from anyconfig.tests.common import dicts_equal, to_bytes |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
XML_W_NS_S = """ |
19
|
|
|
<a xmlns="http://example.com/ns/config" |
20
|
|
|
xmlns:val="http://example.com/ns/config/val"> |
21
|
|
|
<b>1</b> |
22
|
|
|
<val:c>C</val:c> |
23
|
|
|
</a> |
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
CNF_0 = {'config': {'@attrs': {'name': 'foo'}, |
27
|
|
|
'@children': [{'a': '0'}, |
28
|
|
|
{'b': {'@attrs': {'id': 'b0'}, |
29
|
|
|
'@text': 'bbb'}}, |
30
|
|
|
{'sect0': {'c': 'x, y, z'}}]}} |
31
|
|
|
CNF_0_S = """\ |
32
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
33
|
|
|
<config name='foo'> |
34
|
|
|
<a>0</a> |
35
|
|
|
<b id="b0">bbb</b> |
36
|
|
|
<sect0> |
37
|
|
|
<c>x, y, z</c> |
38
|
|
|
</sect0> |
39
|
|
|
</config> |
40
|
|
|
""" |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
class Test_00(unittest.TestCase): |
44
|
|
|
|
45
|
|
|
def test__namespaces_from_file(self): |
46
|
|
|
ref = {"http://example.com/ns/config": '', |
47
|
|
|
"http://example.com/ns/config/val": "val"} |
48
|
|
|
xmlfile = anyconfig.compat.StringIO(XML_W_NS_S) |
49
|
|
|
self.assertTrue(dicts_equal(TT._namespaces_from_file(xmlfile), ref)) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class Test_10(unittest.TestCase): |
53
|
|
|
|
54
|
|
|
def test_10_elem_to_container__None(self): |
55
|
|
|
self.assertEqual(TT.elem_to_container(None, dict, {}), dict()) |
56
|
|
|
|
57
|
|
|
def test_10_root_to_container__None(self): |
58
|
|
|
self.assertEqual(TT.root_to_container(None, dict, {}), dict()) |
59
|
|
|
|
60
|
|
|
def test_20_elem_to_container__attrs(self): |
61
|
|
|
ref = dict(a={"@attrs": dict(x='1', y='y')}) |
62
|
|
|
root = TT.ET.XML("<a x='1' y='y'/>") |
63
|
|
|
self.assertEqual(TT.elem_to_container(root, dict, {}), ref) |
64
|
|
|
|
65
|
|
|
def test_30_elem_to_container__child(self): |
66
|
|
|
ref = dict(a=dict(b="b")) |
67
|
|
|
root = TT.ET.XML("<a><b>b</b></a>") |
68
|
|
|
self.assertEqual(TT.elem_to_container(root, dict, {}), ref) |
69
|
|
|
|
70
|
|
|
def test_32_elem_to_container__children(self): |
71
|
|
|
ref = {'a': {'@children': [{'b': 'b'}, {'c': 'c'}]}} |
72
|
|
|
root = TT.ET.XML("<a><b>b</b><c>c</c></a>") |
73
|
|
|
self.assertEqual(TT.elem_to_container(root, dict, {}), ref) |
74
|
|
|
|
75
|
|
|
def test_40_elem_to_container__text(self): |
76
|
|
|
root = TT.ET.XML("<a>A</a>") |
77
|
|
|
self.assertEqual(TT.elem_to_container(root, dict, {}), {'a': 'A'}) |
78
|
|
|
|
79
|
|
|
def test_42_elem_to_container__text_attrs(self): |
80
|
|
|
ref = dict(a={"@attrs": {'x': 'X'}, "@text": "A"}) |
81
|
|
|
root = TT.ET.XML("<a x='X'>A</a>") |
82
|
|
|
self.assertEqual(TT.elem_to_container(root, dict, {}), ref) |
83
|
|
|
|
84
|
|
|
def test_50_root_to_container__text_attrs_pprefix(self): |
85
|
|
|
ref = dict(a={"_attrs": {'x': 'X'}, "_text": "A"}) |
86
|
|
|
root = TT.ET.XML("<a x='X'>A</a>") |
87
|
|
|
self.assertEqual(TT.root_to_container(root, dict, {}, pprefix='_'), |
88
|
|
|
ref) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def tree_to_string(tree): |
92
|
|
|
return TT.ET.tostring(tree.getroot()) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
class Test_20(unittest.TestCase): |
96
|
|
|
|
97
|
|
|
def test_00_container_to_etree__None(self): |
98
|
|
|
self.assertTrue(TT.container_to_etree(None) is None) |
99
|
|
|
|
100
|
|
|
def test_10_container_to_etree__text_attrs(self): |
101
|
|
|
ref = to_bytes('<a x="X" y="Y">A</a>') |
102
|
|
|
obj = dict(a={"@attrs": {'x': 'X', 'y': 'Y'}, "@text": "A"}) |
103
|
|
|
res = TT.container_to_etree(obj) |
104
|
|
|
self.assertEqual(tree_to_string(res), ref) |
105
|
|
|
|
106
|
|
|
def test_12_container_to_etree__text_attrs_pprefix(self): |
107
|
|
|
ref = to_bytes('<a x="X" y="Y">A</a>') |
108
|
|
|
obj = dict(a={"_attrs": {'x': 'X', 'y': 'Y'}, "_text": "A"}) |
109
|
|
|
res = TT.container_to_etree(obj, pprefix='_') |
110
|
|
|
self.assertEqual(tree_to_string(res), ref) |
111
|
|
|
|
112
|
|
|
def test_20_container_to_etree__child(self): |
113
|
|
|
ref = to_bytes("<a><b>b</b></a>") |
114
|
|
|
obj = dict(a=dict(b="b")) |
115
|
|
|
res = TT.container_to_etree(obj) |
116
|
|
|
self.assertEqual(tree_to_string(res), ref) |
117
|
|
|
|
118
|
|
|
def test_22_container_to_etree__children(self): |
119
|
|
|
ref = to_bytes("<a><b>b</b><c>c</c></a>") |
120
|
|
|
obj = {'a': {'@children': [{'b': 'b'}, {'c': 'c'}]}} |
121
|
|
|
res = TT.container_to_etree(obj) |
122
|
|
|
self.assertEqual(tree_to_string(res), ref) |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
class Test10(anyconfig.backend.tests.ini.Test10): |
126
|
|
|
|
127
|
|
|
cnf = CNF_0 |
128
|
|
|
cnf_s = CNF_0_S.encode("utf-8") |
129
|
|
|
load_options = dump_options = dict(pprefix='@') |
130
|
|
|
|
131
|
|
|
def setUp(self): |
132
|
|
|
self.psr = TT.Parser() |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
class Test20(anyconfig.backend.tests.ini.Test20): |
136
|
|
|
|
137
|
|
|
psr_cls = TT.Parser |
138
|
|
|
cnf = CNF_0 |
139
|
|
|
cnf_s = CNF_0_S |
140
|
|
|
cnf_fn = "conf0.xml" |
141
|
|
|
|
142
|
|
|
def setUp(self): |
143
|
|
|
self.psr = self.psr_cls() |
144
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
145
|
|
|
self.cpath = os.path.join(self.workdir, self.cnf_fn) |
146
|
|
|
with self.psr.wopen(self.cpath) as out: |
147
|
|
|
if anyconfig.compat.IS_PYTHON_3: |
148
|
|
|
out.write(self.cnf_s.encode("utf-8")) |
149
|
|
|
else: |
150
|
|
|
out.write(self.cnf_s) |
151
|
|
|
|
152
|
|
|
def test_12_load__w_options(self): |
153
|
|
|
cnf = self.psr.load(self.cpath, parse_int=None) |
154
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
155
|
|
|
|
156
|
|
|
def test_22_dump__w_special_option(self): |
157
|
|
|
self.psr.dump(self.cnf, self.cpath, parse_int=None, indent=3) |
158
|
|
|
cnf = self.psr.load(self.cpath) |
159
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
160
|
|
|
|
161
|
|
|
# vim:sw=4:ts=4:et: |
162
|
|
|
|