Coverage for gws-app/gws/lib/xmlx/_test/element_test.py: 100%
106 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Tests for the element module"""
3import gws
5import gws.lib.xmlx as xmlx
8def test_iter():
9 xml = '''
10 <root>
11 <a/>
12 <b/>
13 <c/>
14 </root>
15 '''
16 root = xmlx.from_string(xml)
17 tags = [e.tag for e in root]
18 assert tags == ['a', 'b', 'c']
21def test_find_with_namespaces():
22 xml = '''
23 <root
24 xmlns:wmts="http://www.opengis.net/wmts"
25 xmlns:other="foobar"
26 >
27 <wmts:a test="attr1"/>
28 <a test="just"/>
29 <other:a test="another"/>
30 <wmts:a test="attr2"/>
31 </root>
32 '''
33 root = xmlx.from_string(xml)
35 atts = [a.get('test') for a in root.findall('wmts:a', {'wmts': 'http://www.opengis.net/wmts'})]
36 assert atts == ['attr1', 'attr2']
39def test_to_dict():
40 xml = '''
41 <root>
42 <a>
43 <b/>
44 </a>
45 </root>
46 '''
48 d = {'attrib': {},
49 'children': ([{
51 'attrib': {},
52 'children': ([{
54 'attrib': {},
55 'children': [],
56 'tag': 'b',
57 'tail': '\n ',
58 'text': ''}]),
60 'tag': 'a',
61 'tail': '\n ',
62 'text': '\n '}]),
64 'tag': 'root',
65 'tail': '',
66 'text': '\n '
67 }
68 root = xmlx.from_string(xml)
69 assert root.to_dict() == d
72def test_to_string():
73 xml = '''<?xml version="1.0" encoding="UTF-8"?>
74 <root
75 xmlns:wmts="http://www.opengis.net/wmts"
76 >
77 <wmts:a test="attr1"/>
78 <a test="just"/>
79 <wmts:a test="attr2"/>
80 <a>
81 <b/>
82 </a>
83 </root>'''
84 xml_str = '''<root>
85 <wmts:a test="attr1"/>
86 <a test="just"/>
87 <wmts:a test="attr2"/>
88 <a>
89 <b/>
90 </a>
91 </root>'''
92 root = xmlx.from_string(xml)
93 assert root.to_string() == xml_str
96def test_to_string_compact_whitespace():
97 xml = ''' <root>
98 <a>
99 <b test="just"/>
100 </a>
101 </root>'''
102 root = xmlx.from_string(xml)
103 assert root.to_string(compact_whitespace=True) == '<root><a><b test="just"/></a></root>'
106def test_to_string_remove_namespaces():
107 xml = '''<root
108 xmlns:wmts="http://www.opengis.net/wmts"
109 >
110 <wmts:a test="attr1"/>
111 <a test="just"/>
112 <wmts:a test="attr2"/>
113 <a>
114 <b/>
115 </a>
116 </root>'''
118 xml_no_nspace = '''<root>
119 <a test="attr1"/>
120 <a test="just"/>
121 <a test="attr2"/>
122 <a>
123 <b/>
124 </a>
125 </root>'''
126 root = xmlx.from_string(xml)
127 assert root.to_string(remove_namespaces=True) == xml_no_nspace
130def test_to_string_with_namespace_declarations():
131 xml = '''<root
132 xmlns:wmts="http://www.opengis.net/wmts"
133 >
134 <wmts:a test="attr1"/>
135 <a test="just"/>
136 <wmts:a test="attr2"/>
137 <a>
138 <b/>
139 </a>
140 </root>'''
142 xml_with_namespace_declarations = '''<root xmlns:wmts="http://www.opengis.net/wmts/1.0">
143 <wmts:a test="attr1"/>
144 <a test="just"/>
145 <wmts:a test="attr2"/>
146 <a>
147 <b/>
148 </a>
149 </root>'''
151 root = xmlx.from_string(xml)
152 assert root.to_string(with_namespace_declarations=True) == xml_with_namespace_declarations
155def test_to_string_with_schema_locations():
156 xml = '''<root
157 xmlns:wmts="http://www.opengis.net/wmts"
158 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
159 xsi:schemaLocation="test"
160 >
161 <wmts:a test="attr1"/>
162 <a test="just"/>
163 <a test="attr2"/>
164 <a>
165 <b/>
166 </a>
167 </root>'''
169 xml_with_schema_locations = '''<root xsi:schemaLocation="test">
170 <wmts:a test="attr1"/>
171 <a test="just"/>
172 <a test="attr2"/>
173 <a>
174 <b/>
175 </a>
176 </root>'''
177 root = xmlx.from_string(xml)
178 assert root.to_string(with_schema_locations=True) == xml_with_schema_locations
181def test_to_string_with_xml_declaration():
182 xml = '''<?xml version="1.0" encoding="UTF-8"?>
183 <root>
184 <a>
185 <b/>
186 </a>
187 </root>'''
189 xml_with_xml_declaration = '''<?xml version="1.0" encoding="UTF-8"?><root>
190 <a>
191 <b/>
192 </a>
193 </root>'''
195 root = xmlx.from_string(xml)
196 assert root.to_string(with_xml_declaration=True) == xml_with_xml_declaration
199def test_add():
200 xml = '''
201 <Root>
202 </Root>
203 '''
204 test = xmlx.parser.from_string(xml)
205 test.add('a', {'foo': 'bar'})
206 assert test.to_string(compact_whitespace=True) == '<Root><a foo="bar"/></Root>'
207 assert test.children()[0].to_string(compact_whitespace=True) == '<a foo="bar"/>'
210def test_attr():
211 xml = '''
212 <Root attr= "val">
213 <a bar="foo"/>
214 <b test ="attr"/>
215 <c><deep>test2="attr2"</deep></c>
216 </Root>
217 '''
218 test = xmlx.parser.from_string(xml)
219 assert test.attr('attr') == 'val'
220 assert test.attr('attr2') == None
223def test_children():
224 xml = '''
225 <root>
226 <a>
227 <b/>
228 </a>
229 <c/>
230 </root>
231 '''
232 test = xmlx.parser.from_string(xml)
233 assert test.children()[0].to_string(compact_whitespace=True) == '<a><b/></a>'
234 assert test.children()[1].to_string(compact_whitespace=True) == '<c/>'
235 assert test.children()[0].children()[0].to_string(compact_whitespace=True) == '<b/>'
238def test_findfirst():
239 xml = '''
240 <root a1="A1" a2="A2">
241 text
242 <nested>
243 <deep>text2</deep>
244 text3
245 <single b="B1"/>
246 </nested>
247 </root>
248 '''
249 test = xmlx.parser.from_string(xml)
250 assert test.findfirst().__dict__ == {'caseInsensitive': False, 'lcName': 'nested', 'name': 'nested'}
253def test_textof():
254 xml = '''
255 <container>
256 text
257 <tag1><deep>xxx</deep></tag1>
258 <tag2>yyy</tag2>
259 <tag3>zzz</tag3>
260 <tag4></tag4>
261 </container>'''
262 test = xmlx.parser.from_string(xml)
263 assert not test.textof()
264 assert test.textof('tag1/deep') == 'xxx'
265 assert test.textof('tag2') == 'yyy'
266 assert not test.textof('tag4')
269def test_textlist():
270 xml = '''
271 <container>
272 <tag1> xxx </tag1>
273 <tag2> yyy </tag2>
274 <tag3> zzz </tag3>
275 </container>'''
276 test = xmlx.parser.from_string(xml)
277 assert test.textlist() == ["xxx", "yyy", "zzz"]
280def test_textlist_nested():
281 xml = '''
282 <root a1="A1" a2="A2">
283 text
284 <nested><deep>text3</deep></nested>
285 </root>
286 '''
287 test = xmlx.parser.from_string(xml)
288 assert test.textlist('nested/deep') == ['text3']
291def test_textlist_empty():
292 xml = '''
293 <root>
294 </root>
295 '''
296 test = xmlx.parser.from_string(xml)
297 assert test.textlist() == []
300def test_textlist_deep():
301 xml = '''
302 <root a1="A1" a2="A2">
303 text
304 <nested>text1<deep>text2</deep></nested>
305 <nested2>text3<deep2>text4</deep2></nested2>
306 </root>
307 '''
308 test = xmlx.parser.from_string(xml)
309 assert test.textlist(deep=True) == ['text1', 'text2', 'text3', 'text4']
312def test_textdict_deep():
313 xml = '''
314 <root a1="A1" a2="A2">
315 text
316 <nested>text1<deep>text2</deep></nested>
317 <nested2>text3<deep2>text4</deep2></nested2>
318 </root>
319 '''
320 test = xmlx.parser.from_string(xml)
321 assert test.textdict(deep=True) == {'nested': 'text1', 'deep': 'text2', 'nested2': 'text3', 'deep2': 'text4'}
324def test_textdict():
325 xml = '''
326 <container>
327 <tag1> xxx </tag1>
328 <tag2> yyy </tag2>
329 <tag3> zzz </tag3>
330 </container>'''
331 test = xmlx.parser.from_string(xml)
332 assert test.textdict() == {'tag1': 'xxx', 'tag2': 'yyy', 'tag3': 'zzz'}
335def test_textdict_nested():
336 xml = '''
337 <root a1="A1" a2="A2">
338 text
339 <nested><deep>text3</deep></nested>
340 </root>
341 '''
342 test = xmlx.parser.from_string(xml)
343 assert test.textdict('nested/deep') == {'deep': 'text3'}
346def test_textdict_empty():
347 xml = '''
348 <root>
349 </root>
350 '''
351 test = xmlx.parser.from_string(xml)
352 assert test.textdict() == {}