Coverage for gws-app/gws/lib/xmlx/_test/tag_test.py: 100%
57 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
1import gws
2import gws.test.util as u
4import gws.lib.xmlx as xmlx
7def test_simple():
8 tag = xmlx.tag('name', 'text', {'a1': 'A1', 'a2': 'A2'})
9 xml = tag.to_string()
10 assert xml == '<name a1="A1" a2="A2">text</name>'
13def test_nested():
14 el = xmlx.tag(
15 'root',
16 'text',
17 {'a1': 'A1', 'a2': 'A2'},
18 [
19 'nested',
20 ['deep', 'text2'],
21 'text3',
22 ['single', {'b': 'B1'}],
23 ],
24 )
26 xml = el.to_string()
27 assert u.fxml(xml) == u.fxml("""
28 <root a1="A1" a2="A2">
29 text
30 <nested>
31 <deep>text2</deep>
32 text3
33 <single b="B1"/>
34 </nested>
35 </root>
36 """)
39def test_with_namespaces():
40 el = xmlx.tag(
41 'root',
42 ['wms:foo'],
43 ['{http://www.opengis.net/wfs/2.0}bar'],
44 )
46 xml = el.to_string()
47 assert u.fxml(xml) == u.fxml('<root><wms:foo/><wfs:bar/></root>')
49 xml = el.to_string(gws.XmlOptions(withNamespaceDeclarations=True))
51 assert u.fxml(xml) == u.fxml("""
52 <root
53 xmlns:wfs="http://www.opengis.net/wfs/2.0"
54 xmlns:wms="http://www.opengis.net/wms">
55 <wms:foo/>
56 <wfs:bar/>
57 </root>
58 """)
61def test_with_default_namespace():
62 el = xmlx.tag(
63 'root',
64 ['wms:foo'],
65 ['{http://www.opengis.net/wfs/2.0}bar'],
66 )
68 xml = el.to_string(
69 gws.XmlOptions(
70 withNamespaceDeclarations=True,
71 defaultNamespace=xmlx.namespace.require('wms'),
72 )
73 )
75 assert u.fxml(xml) == u.fxml("""
76 <root
77 xmlns="http://www.opengis.net/wms"
78 xmlns:wfs="http://www.opengis.net/wfs/2.0">
79 <foo/>
80 <wfs:bar/>
81 </root>
82 """)
85def test_with_space():
86 el = xmlx.tag('1 / 2 / 3')
87 assert u.fxml(el.to_string()) == u.fxml("""
88 <1>
89 <2>
90 <3/>
91 </2>
92 </1>
93 """)
96def test_text_str():
97 el = xmlx.tag('root', 'text')
98 assert u.fxml(el.to_string()) == u.fxml('<root>text</root>')
101def test_text_int():
102 el = xmlx.tag('root', 2)
103 assert u.fxml(el.to_string()) == u.fxml('<root>2</root>')
106def test_append_tuple2():
107 el = xmlx.tag('root/nested', ('foo', 2))
108 assert u.fxml(el.to_string()) == u.fxml("""
109 <root>
110 <nested>
111 <foo>
112 2
113 </foo>
114 </nested>
115 </root>
116 """)
119def test_append_tuple():
120 el = xmlx.tag('root/nested', ('foo', 'bar'))
121 assert u.fxml(el.to_string()) == u.fxml("""
122 <root>
123 <nested>
124 <foo>
125 bar
126 </foo>
127 </nested>
128 </root>
129 """)
132def test_child():
133 child = xmlx.tag('child')
134 el = xmlx.tag('root', child)
135 assert u.fxml(el.to_string()) == u.fxml("""
136 <root>
137 <child/>
138 </root>
139 """)
142def test_dict_attr():
143 attr = {'foo': 1, 'bar': 2}
144 el = xmlx.tag('root', attr)
145 assert u.fxml(el.to_string()) == u.fxml('<root foo="1" bar="2"/>')
148def test_list():
149 list = ['foo', 'bar', 'foo2', 'bar2']
150 el = xmlx.tag('root', list)
151 assert u.fxml(el.to_string()) == u.fxml("""
152 <root>
153 <foo>
154 barfoo2bar2
155 </foo>
156 </root>
157 """)
160def test_keywords():
161 el = xmlx.tag('root', foo='bar')
162 assert u.fxml(el.to_string()) == u.fxml('<root foo="bar"/>')
165def test_tag():
166 el = xmlx.tag(
167 'geometry/gml:Point',
168 {'gml:id': 'xy'},
169 ['gml:coordinates', '12.345,56.789'],
170 srsName=3857,
171 )
172 assert u.fxml(el.to_string()) == u.fxml("""
173 <geometry>
174 <gml:Point gml:id="xy" srsName="3857">
175 <gml:coordinates>12.345,56.789</gml:coordinates>
176 </gml:Point>
177 </geometry>
178 """)
181def test_tag_uri():
182 el = xmlx.tag(
183 '{http://www.opengis.net/cat/csw/2.0.2}foo/nested',
184 {'gml:id': 'xy'},
185 ['gml:coordinates', '12.345,56.789'],
186 srsName=3857,
187 )
188 assert u.fxml(el.to_string()) == u.fxml("""
189 <csw:foo>
190 <nested gml:id="xy" srsName="3857">
191 <gml:coordinates>12.345,56.789</gml:coordinates>
192 </nested>
193 </csw:foo>
194 """)