Coverage for gws-app/gws/lib/xmlx/_test/tag_test.py: 100%

57 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +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 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}bar'], 

44 ) 

45 

46 xml = el.to_string() 

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

48 

49 xml = el.to_string(with_namespace_declarations=True) 

50 assert xml == u.fxml(''' 

51 <root  

52 xmlns:wfs="http://www.opengis.net/wfs/2.0"  

53 xmlns:wms="http://www.opengis.net/wms/1.3.0" 

54 > 

55 <wms:foo/> 

56 <wfs:bar/> 

57 </root> 

58 ''') 

59 

60 

61def test_with_default_namespace(): 

62 el = xmlx.tag( 

63 'root', 

64 {'xmlns': 'wms'}, 

65 ['wms:foo'], 

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

67 ) 

68 

69 xml = el.to_string(with_namespace_declarations=True) 

70 assert xml == u.fxml(''' 

71 <root  

72 xmlns="http://www.opengis.net/wms/1.3.0"  

73 xmlns:wfs="http://www.opengis.net/wfs/2.0" 

74 > 

75 <foo/> 

76 <wfs:bar/> 

77 </root> 

78 ''') 

79 

80 

81def test_with_space(): 

82 el = xmlx.tag('1 / 2 / 3') 

83 assert el.to_string() == u.fxml(''' 

84 <1> 

85 <2> 

86 <3/> 

87 </2> 

88 </1> 

89 ''') 

90 

91 

92def test_text_str(): 

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

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

95 

96 

97def test_text_int(): 

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

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

100 

101 

102def test_append_tuple2(): 

103 el = xmlx.tag('root/nested', ('foo', 2)) 

104 assert el.to_string() == u.fxml(''' 

105 <root> 

106 <nested> 

107 <foo> 

108 2 

109 </foo> 

110 </nested> 

111 </root> 

112 ''') 

113 

114 

115def test_append_tuple(): 

116 el = xmlx.tag('root/nested', ('foo', 'bar')) 

117 assert el.to_string() == u.fxml(''' 

118 <root> 

119 <nested> 

120 <foo> 

121 bar 

122 </foo> 

123 </nested> 

124 </root> 

125 ''') 

126 

127 

128def test_child(): 

129 child = xmlx.tag('child') 

130 el = xmlx.tag('root', child) 

131 assert el.to_string() == u.fxml(''' 

132 <root> 

133 <child/> 

134 </root> 

135 ''') 

136 

137 

138def test_dict_attr(): 

139 attr = {'foo': 1, 'bar': 2} 

140 el = xmlx.tag('root', attr) 

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

142 

143 

144def test_list(): 

145 list = ['foo', 'bar', 'foo2', 'bar2'] 

146 el = xmlx.tag('root', list) 

147 assert el.to_string() == u.fxml(''' 

148 <root> 

149 <foo> 

150 barfoo2bar2 

151 </foo> 

152 </root> 

153 ''') 

154 

155 

156def test_keywords(): 

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

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

159 

160 

161def test_tag(): 

162 el = xmlx.tag('geometry/gml:Point', 

163 {'gml:id': 'xy'}, 

164 ['gml:coordinates', '12.345,56.789'], 

165 srsName=3857) 

166 assert el.to_string() == u.fxml(''' 

167 <geometry> 

168 <gml:Point gml:id="xy" srsName="3857"> 

169 <gml:coordinates>12.345,56.789</gml:coordinates> 

170 </gml:Point> 

171 </geometry> 

172 ''') 

173 

174 

175def test_tag_uri(): 

176 el = xmlx.tag('{http://www.opengis.net/cat/csw}foo/nested', 

177 {'gml:id': 'xy'}, 

178 ['gml:coordinates', '12.345,56.789'], 

179 srsName=3857) 

180 assert el.to_string() == u.fxml(''' 

181 <csw:foo> 

182 <nested gml:id="xy" srsName="3857"> 

183 <gml:coordinates>12.345,56.789</gml:coordinates> 

184 </nested> 

185 </csw:foo> 

186 ''')