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 22:59 +0200

1import gws 

2import gws.test.util as u 

3 

4import gws.lib.xmlx as xmlx 

5 

6 

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>' 

11 

12 

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 ) 

25 

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 """) 

37 

38 

39def test_with_namespaces(): 

40 el = xmlx.tag( 

41 'root', 

42 ['wms:foo'], 

43 ['{http://www.opengis.net/wfs/2.0}bar'], 

44 ) 

45 

46 xml = el.to_string() 

47 assert u.fxml(xml) == u.fxml('<root><wms:foo/><wfs:bar/></root>') 

48 

49 xml = el.to_string(gws.XmlOptions(withNamespaceDeclarations=True)) 

50 

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 """) 

59 

60 

61def test_with_default_namespace(): 

62 el = xmlx.tag( 

63 'root', 

64 ['wms:foo'], 

65 ['{http://www.opengis.net/wfs/2.0}bar'], 

66 ) 

67 

68 xml = el.to_string( 

69 gws.XmlOptions( 

70 withNamespaceDeclarations=True, 

71 defaultNamespace=xmlx.namespace.require('wms'), 

72 ) 

73 ) 

74 

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 """) 

83 

84 

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 """) 

94 

95 

96def test_text_str(): 

97 el = xmlx.tag('root', 'text') 

98 assert u.fxml(el.to_string()) == u.fxml('<root>text</root>') 

99 

100 

101def test_text_int(): 

102 el = xmlx.tag('root', 2) 

103 assert u.fxml(el.to_string()) == u.fxml('<root>2</root>') 

104 

105 

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 """) 

117 

118 

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 """) 

130 

131 

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 """) 

140 

141 

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"/>') 

146 

147 

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 """) 

158 

159 

160def test_keywords(): 

161 el = xmlx.tag('root', foo='bar') 

162 assert u.fxml(el.to_string()) == u.fxml('<root foo="bar"/>') 

163 

164 

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 """) 

179 

180 

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 """)