|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2012 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# pylint: disable=missing-docstring, invalid-name |
|
6
|
|
|
from __future__ import absolute_import |
|
7
|
|
|
|
|
8
|
|
|
import copy |
|
9
|
|
|
import os.path |
|
10
|
|
|
import unittest |
|
11
|
|
|
|
|
12
|
|
|
import anyconfig.backend.xml as TT |
|
13
|
|
|
import anyconfig.backend.tests.ini |
|
14
|
|
|
import anyconfig.tests.common |
|
15
|
|
|
|
|
16
|
|
|
from anyconfig.tests.common import dicts_equal, to_bytes |
|
17
|
|
|
from anyconfig.mdicts import to_container |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
CNF_0_S = """<?xml version="1.0" encoding="UTF-8"?> |
|
21
|
|
|
<config name='foo'> |
|
22
|
|
|
<a>0</a> |
|
23
|
|
|
<b id="b0">bbb</b> |
|
24
|
|
|
<sect0> |
|
25
|
|
|
<c>x, y, z</c> |
|
26
|
|
|
</sect0> |
|
27
|
|
|
</config> |
|
28
|
|
|
""" |
|
29
|
|
|
|
|
30
|
|
|
CNF_0 = {'config': {'@attrs': {'name': 'foo'}, |
|
31
|
|
|
'@children': [{'a': {'@text': '0'}}, |
|
32
|
|
|
{'b': {'@attrs': {'id': 'b0'}, |
|
33
|
|
|
'@text': 'bbb'}}, |
|
34
|
|
|
{'sect0': { |
|
35
|
|
|
'@children': [{'c': { |
|
36
|
|
|
'@text': 'x, y, z' |
|
37
|
|
|
}}]}}]}} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class Test00(unittest.TestCase): |
|
41
|
|
|
|
|
42
|
|
|
def setUp(self): |
|
43
|
|
|
self.root = TT.ET.Element("config", attrib=dict(name="foo")) |
|
44
|
|
|
celm0 = TT.ET.SubElement(self.root, "a") |
|
45
|
|
|
celm0.text = '0' |
|
46
|
|
|
celm1 = TT.ET.SubElement(self.root, "b", attrib=dict(id="b0", )) |
|
47
|
|
|
celm1.text = "bbb" |
|
48
|
|
|
self.tree = TT.ET.ElementTree(self.root) |
|
49
|
|
|
|
|
50
|
|
|
self.cnf = copy.deepcopy(CNF_0) |
|
51
|
|
|
del self.cnf["config"]["@children"][-1] |
|
52
|
|
|
|
|
53
|
|
|
self.cnf_s = to_bytes('<config name="foo"><a>0</a>' |
|
54
|
|
|
'<b id="b0">bbb</b></config>') |
|
55
|
|
|
|
|
56
|
|
|
def test_10_etree_to_container__empty(self): |
|
57
|
|
|
self.assertEqual(TT.etree_to_container(None, dict), {}) |
|
58
|
|
|
|
|
59
|
|
|
def test_12_etree_to_container(self): |
|
60
|
|
|
cnf = TT.etree_to_container(self.root, to_container) |
|
61
|
|
|
self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) |
|
62
|
|
|
|
|
63
|
|
|
def test_20_container_to_etree__empty(self): |
|
64
|
|
|
# This should not happen but just in case. |
|
65
|
|
|
self.assertTrue(TT.container_to_etree("aaa", dict) is None) |
|
66
|
|
|
|
|
67
|
|
|
def test_22_container_to_etree(self): |
|
68
|
|
|
tree = TT.container_to_etree(self.cnf, to_container) |
|
69
|
|
|
buf = TT.BytesIO() |
|
70
|
|
|
tree.write(buf) |
|
71
|
|
|
cnf_s = buf.getvalue() |
|
72
|
|
|
self.assertEqual(cnf_s, self.cnf_s, cnf_s) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class Test10(anyconfig.backend.tests.ini.Test10): |
|
76
|
|
|
|
|
77
|
|
|
cnf = CNF_0 |
|
78
|
|
|
cnf_s = CNF_0_S |
|
79
|
|
|
load_options = dump_options = dict(dummy="this will be ignored") |
|
80
|
|
|
|
|
81
|
|
|
def setUp(self): |
|
82
|
|
|
self.psr = TT.Parser() |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
class Test20(anyconfig.backend.tests.ini.Test20): |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
psr_cls = TT.Parser |
|
88
|
|
|
cnf = CNF_0 |
|
89
|
|
|
cnf_s = CNF_0_S |
|
90
|
|
|
cnf_fn = "conf0.xml" |
|
91
|
|
|
|
|
92
|
|
|
def setUp(self): |
|
93
|
|
|
self.psr = TT.Parser() |
|
94
|
|
|
self.workdir = anyconfig.tests.common.setup_workdir() |
|
95
|
|
|
self.cpath = os.path.join(self.workdir, self.cnf_fn) |
|
96
|
|
|
open(self.cpath, 'w').write(self.cnf_s) |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
# vim:sw=4:ts=4:et: |
|
100
|
|
|
|