OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2005 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 from twisted.trial import unittest | |
6 import sys, os | |
7 | |
8 from twisted.words.xish.domish import Element | |
9 from twisted.words.xish.xpath import XPathQuery | |
10 from twisted.words.xish import xpath | |
11 | |
12 class XPathTest(unittest.TestCase): | |
13 def setUp(self): | |
14 # Build element: | |
15 # <foo xmlns='testns' attrib1='value1' attrib3="user@host/resource"> | |
16 # somecontent | |
17 # <bar> | |
18 # <foo> | |
19 # <gar>DEF</gar> | |
20 # </foo> | |
21 # </bar> | |
22 # somemorecontent | |
23 # <bar attrib2="value2"> | |
24 # <bar> | |
25 # <foo/> | |
26 # <gar>ABC</gar> | |
27 # </bar> | |
28 # <bar/> | |
29 # <bar attrib4='value4' attrib5='value5'> | |
30 # <foo/> | |
31 # <gar>JKL</gar> | |
32 # </bar> | |
33 # <bar attrib4='value4' attrib5='value4'> | |
34 # <foo/> | |
35 # <gar>MNO</gar> | |
36 # </bar> | |
37 # <bar attrib4='value4' attrib5='value6'/> | |
38 # </foo> | |
39 self.e = Element(("testns", "foo")) | |
40 self.e["attrib1"] = "value1" | |
41 self.e["attrib3"] = "user@host/resource" | |
42 self.e.addContent("somecontent") | |
43 self.bar1 = self.e.addElement("bar") | |
44 self.subfoo = self.bar1.addElement("foo") | |
45 self.gar1 = self.subfoo.addElement("gar") | |
46 self.gar1.addContent("DEF") | |
47 self.e.addContent("somemorecontent") | |
48 self.bar2 = self.e.addElement("bar") | |
49 self.bar2["attrib2"] = "value2" | |
50 self.bar3 = self.bar2.addElement("bar") | |
51 self.subfoo2 = self.bar3.addElement("foo") | |
52 self.gar2 = self.bar3.addElement("gar") | |
53 self.gar2.addContent("ABC") | |
54 self.bar4 = self.e.addElement("bar") | |
55 self.bar5 = self.e.addElement("bar") | |
56 self.bar5["attrib4"] = "value4" | |
57 self.bar5["attrib5"] = "value5" | |
58 self.subfoo3 = self.bar5.addElement("foo") | |
59 self.gar3 = self.bar5.addElement("gar") | |
60 self.gar3.addContent("JKL") | |
61 self.bar6 = self.e.addElement("bar") | |
62 self.bar6["attrib4"] = "value4" | |
63 self.bar6["attrib5"] = "value4" | |
64 self.subfoo4 = self.bar6.addElement("foo") | |
65 self.gar4 = self.bar6.addElement("gar") | |
66 self.gar4.addContent("MNO") | |
67 self.bar7 = self.e.addElement("bar") | |
68 self.bar7["attrib4"] = "value4" | |
69 self.bar7["attrib5"] = "value6" | |
70 | |
71 def test_staticMethods(self): | |
72 """ | |
73 Test basic operation of the static methods. | |
74 """ | |
75 self.assertEquals(xpath.matches("/foo/bar", self.e), | |
76 True) | |
77 self.assertEquals(xpath.queryForNodes("/foo/bar", self.e), | |
78 [self.bar1, self.bar2, self.bar4, | |
79 self.bar5, self.bar6, self.bar7]) | |
80 self.assertEquals(xpath.queryForString("/foo", self.e), | |
81 "somecontent") | |
82 self.assertEquals(xpath.queryForStringList("/foo", self.e), | |
83 ["somecontent", "somemorecontent"]) | |
84 | |
85 def test_locationFooBar(self): | |
86 """ | |
87 Test matching foo with child bar. | |
88 """ | |
89 xp = XPathQuery("/foo/bar") | |
90 self.assertEquals(xp.matches(self.e), 1) | |
91 | |
92 def test_locationFooBarFoo(self): | |
93 """ | |
94 Test finding foos at the second level. | |
95 """ | |
96 xp = XPathQuery("/foo/bar/foo") | |
97 self.assertEquals(xp.matches(self.e), 1) | |
98 self.assertEquals(xp.queryForNodes(self.e), [self.subfoo, | |
99 self.subfoo3, | |
100 self.subfoo4]) | |
101 | |
102 def test_locationNoBar3(self): | |
103 """ | |
104 Test not finding bar3. | |
105 """ | |
106 xp = XPathQuery("/foo/bar3") | |
107 self.assertEquals(xp.matches(self.e), 0) | |
108 | |
109 def test_locationAllChilds(self): | |
110 """ | |
111 Test finding childs of foo. | |
112 """ | |
113 xp = XPathQuery("/foo/*") | |
114 self.assertEquals(xp.matches(self.e), True) | |
115 self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, | |
116 self.bar4, self.bar5, | |
117 self.bar6, self.bar7]) | |
118 | |
119 def test_attribute(self): | |
120 """ | |
121 Test matching foo with attribute. | |
122 """ | |
123 xp = XPathQuery("/foo[@attrib1]") | |
124 self.assertEquals(xp.matches(self.e), True) | |
125 | |
126 def test_attributeWithValueAny(self): | |
127 """ | |
128 Test find nodes with attribute having value. | |
129 """ | |
130 xp = XPathQuery("/foo/*[@attrib2='value2']") | |
131 self.assertEquals(xp.matches(self.e), True) | |
132 self.assertEquals(xp.queryForNodes(self.e), [self.bar2]) | |
133 | |
134 def test_position(self): | |
135 """ | |
136 Test finding element at position. | |
137 """ | |
138 xp = XPathQuery("/foo/bar[2]") | |
139 self.assertEquals(xp.matches(self.e), 1) | |
140 self.assertEquals(xp.queryForNodes(self.e), [self.bar1]) | |
141 | |
142 test_position.todo = "XPath queries with position are not working." | |
143 | |
144 def test_namespaceFound(self): | |
145 """ | |
146 Test matching node with namespace. | |
147 """ | |
148 xp = XPathQuery("/foo[@xmlns='testns']/bar") | |
149 self.assertEquals(xp.matches(self.e), 1) | |
150 | |
151 def test_namespaceNotFound(self): | |
152 """ | |
153 Test not matching node with wrong namespace. | |
154 """ | |
155 xp = XPathQuery("/foo[@xmlns='badns']/bar2") | |
156 self.assertEquals(xp.matches(self.e), 0) | |
157 | |
158 def test_attributeWithValue(self): | |
159 """ | |
160 Test matching node with attribute having value. | |
161 """ | |
162 xp = XPathQuery("/foo[@attrib1='value1']") | |
163 self.assertEquals(xp.matches(self.e), 1) | |
164 | |
165 def test_queryForString(self): | |
166 """ | |
167 Test for queryForString and queryForStringList. | |
168 """ | |
169 xp = XPathQuery("/foo") | |
170 self.assertEquals(xp.queryForString(self.e), "somecontent") | |
171 self.assertEquals(xp.queryForStringList(self.e), | |
172 ["somecontent", "somemorecontent"]) | |
173 | |
174 def test_queryForNodes(self): | |
175 """ | |
176 Test finding nodes. | |
177 """ | |
178 xp = XPathQuery("/foo/bar") | |
179 self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, | |
180 self.bar4, self.bar5, | |
181 self.bar6, self.bar7]) | |
182 | |
183 def test_textCondition(self): | |
184 """ | |
185 Test matching a node with given text. | |
186 """ | |
187 xp = XPathQuery("/foo[text() = 'somecontent']") | |
188 self.assertEquals(xp.matches(self.e), True) | |
189 | |
190 def test_textNotOperator(self): | |
191 """ | |
192 Test for not operator. | |
193 """ | |
194 xp = XPathQuery("/foo[not(@nosuchattrib)]") | |
195 self.assertEquals(xp.matches(self.e), True) | |
196 | |
197 def test_anyLocationAndText(self): | |
198 """ | |
199 Test finding any nodes named gar and getting their text contents. | |
200 """ | |
201 xp = XPathQuery("//gar") | |
202 self.assertEquals(xp.matches(self.e), True) | |
203 self.assertEquals(xp.queryForNodes(self.e), [self.gar1, self.gar2, | |
204 self.gar3, self.gar4]) | |
205 self.assertEquals(xp.queryForStringList(self.e), ["DEF", "ABC", | |
206 "JKL", "MNO"]) | |
207 | |
208 def test_anyLocation(self): | |
209 """ | |
210 Test finding any nodes named bar. | |
211 """ | |
212 xp = XPathQuery("//bar") | |
213 self.assertEquals(xp.matches(self.e), True) | |
214 self.assertEquals(xp.queryForNodes(self.e), [self.bar1, self.bar2, | |
215 self.bar3, self.bar4, | |
216 self.bar5, self.bar6, | |
217 self.bar7]) | |
218 | |
219 def test_anyLocationQueryForString(self): | |
220 """ | |
221 L{XPathQuery.queryForString} should raise a L{NotImplementedError} | |
222 for any location. | |
223 """ | |
224 xp = XPathQuery("//bar") | |
225 self.assertRaises(NotImplementedError, xp.queryForString, None) | |
226 | |
227 def test_andOperator(self): | |
228 """ | |
229 Test boolean and operator in condition. | |
230 """ | |
231 xp = XPathQuery("//bar[@attrib4='value4' and @attrib5='value5']") | |
232 self.assertEquals(xp.matches(self.e), True) | |
233 self.assertEquals(xp.queryForNodes(self.e), [self.bar5]) | |
234 | |
235 def test_orOperator(self): | |
236 """ | |
237 Test boolean or operator in condition. | |
238 """ | |
239 xp = XPathQuery("//bar[@attrib5='value4' or @attrib5='value5']") | |
240 self.assertEquals(xp.matches(self.e), True) | |
241 self.assertEquals(xp.queryForNodes(self.e), [self.bar5, self.bar6]) | |
242 | |
243 def test_booleanOperatorsParens(self): | |
244 """ | |
245 Test multiple boolean operators in condition with parens. | |
246 """ | |
247 xp = XPathQuery("""//bar[@attrib4='value4' and | |
248 (@attrib5='value4' or @attrib5='value6')]""") | |
249 self.assertEquals(xp.matches(self.e), True) | |
250 self.assertEquals(xp.queryForNodes(self.e), [self.bar6, self.bar7]) | |
251 | |
252 def test_booleanOperatorsNoParens(self): | |
253 """ | |
254 Test multiple boolean operators in condition without parens. | |
255 """ | |
256 xp = XPathQuery("""//bar[@attrib5='value4' or | |
257 @attrib5='value5' or | |
258 @attrib5='value6']""") | |
259 self.assertEquals(xp.matches(self.e), True) | |
260 self.assertEquals(xp.queryForNodes(self.e), [self.bar5, self.bar6, self.
bar7]) | |
OLD | NEW |