1 from sleekxmpp
.test
import *
2 from sleekxmpp
.xmlstream
.stanzabase
import ElementBase
5 class TestElementBase(SleekTest
):
8 """Test fixing namespaces in an XPath expression."""
11 ns
= "http://jabber.org/protocol/disco#items"
12 result
= e
._fix
_ns
("{%s}foo/bar/{abc}baz/{%s}more" % (ns
, ns
))
14 expected
= "/".join(["{%s}foo" % ns
,
18 self
.failUnless(expected
== result
,
19 "Incorrect namespace fixing result: %s" % str(result
))
22 def testExtendedName(self
):
23 """Test element names of the form tag1/tag2/tag3."""
25 class TestStanza(ElementBase
):
30 self
.check(stanza
, """
38 def testGetStanzaValues(self
):
39 """Test getStanzaValues using plugins and substanzas."""
41 class TestStanzaPlugin(ElementBase
):
44 interfaces
= set(('bar', 'baz'))
45 plugin_attrib
= "foo2"
47 class TestSubStanza(ElementBase
):
50 interfaces
= set(('bar', 'baz'))
52 class TestStanza(ElementBase
):
55 interfaces
= set(('bar', 'baz'))
56 subitem
= set((TestSubStanza
,))
58 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
62 stanza
['foo2']['baz'] = 'b'
63 substanza
= TestSubStanza()
64 substanza
['bar'] = 'c'
65 stanza
.append(substanza
)
67 values
= stanza
.getStanzaValues()
68 expected
= {'bar': 'a',
72 'substanzas': [{'__childtag__': '{foo}subfoo',
75 self
.failUnless(values
== expected
,
76 "Unexpected stanza values:\n%s\n%s" % (str(expected
), str(values
)))
79 def testSetStanzaValues(self
):
80 """Test using setStanzaValues with substanzas and plugins."""
82 class TestStanzaPlugin(ElementBase
):
85 interfaces
= set(('bar', 'baz'))
86 plugin_attrib
= "plugin_foo"
88 class TestStanzaPlugin2(ElementBase
):
91 interfaces
= set(('bar', 'baz'))
92 plugin_attrib
= "plugin_foo2"
94 class TestSubStanza(ElementBase
):
97 interfaces
= set(('bar', 'baz'))
99 class TestStanza(ElementBase
):
102 interfaces
= set(('bar', 'baz'))
103 subitem
= set((TestSubStanza
,))
105 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
106 register_stanza_plugin(TestStanza
, TestStanzaPlugin2
)
108 stanza
= TestStanza()
109 values
= {'bar': 'a',
111 'plugin_foo': {'bar': '',
113 'plugin_foo2': {'bar': 'd',
115 'substanzas': [{'__childtag__': '{foo}subfoo',
118 stanza
.setStanzaValues(values
)
120 self
.check(stanza
, """
121 <foo xmlns="foo" bar="a">
122 <pluginfoo baz="b" />
123 <pluginfoo2 bar="d" baz="e" />
128 def testGetItem(self
):
129 """Test accessing stanza interfaces."""
131 class TestStanza(ElementBase
):
134 interfaces
= set(('bar', 'baz', 'qux'))
135 sub_interfaces
= set(('baz',))
140 class TestStanzaPlugin(ElementBase
):
143 plugin_attrib
= "foobar"
144 interfaces
= set(('fizz',))
146 TestStanza
.subitem
= (TestStanza
,)
147 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
149 stanza
= TestStanza()
150 substanza
= TestStanza()
151 stanza
.append(substanza
)
152 stanza
.setStanzaValues({'bar': 'a',
155 'foobar': {'fizz': 'c'}})
157 # Test non-plugin interfaces
158 expected
= {'substanzas': [substanza
],
163 for interface
, value
in expected
.items():
164 result
= stanza
[interface
]
165 self
.failUnless(result
== value
,
166 "Incorrect stanza interface access result: %s" % result
)
168 # Test plugin interfaces
169 self
.failUnless(isinstance(stanza
['foobar'], TestStanzaPlugin
),
170 "Incorrect plugin object result.")
171 self
.failUnless(stanza
['foobar']['fizz'] == 'c',
172 "Incorrect plugin subvalue result.")
174 def testSetItem(self
):
175 """Test assigning to stanza interfaces."""
177 class TestStanza(ElementBase
):
180 interfaces
= set(('bar', 'baz', 'qux'))
181 sub_interfaces
= set(('baz',))
183 def setQux(self
, value
):
186 class TestStanzaPlugin(ElementBase
):
189 plugin_attrib
= "foobar"
190 interfaces
= set(('foobar',))
192 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
194 stanza
= TestStanza()
196 stanza
['bar'] = 'attribute!'
197 stanza
['baz'] = 'element!'
198 stanza
['qux'] = 'overridden'
199 stanza
['foobar'] = 'plugin'
201 self
.check(stanza
, """
202 <foo xmlns="foo" bar="attribute!">
204 <foobar foobar="plugin" />
208 def testDelItem(self
):
209 """Test deleting stanza interface values."""
211 class TestStanza(ElementBase
):
214 interfaces
= set(('bar', 'baz', 'qux'))
215 sub_interfaces
= set(('bar',))
220 class TestStanzaPlugin(ElementBase
):
223 plugin_attrib
= "foobar"
224 interfaces
= set(('foobar',))
226 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
228 stanza
= TestStanza()
232 stanza
['foobar']['foobar'] = 'd'
234 self
.check(stanza
, """
235 <foo xmlns="foo" baz="b" qux="c">
237 <foobar foobar="d" />
246 self
.check(stanza
, """
247 <foo xmlns="foo" qux="c" />
250 def testModifyingAttributes(self
):
251 """Test modifying top level attributes of a stanza's XML object."""
253 class TestStanza(ElementBase
):
256 interfaces
= set(('bar', 'baz'))
258 stanza
= TestStanza()
260 self
.check(stanza
, """
264 self
.failUnless(stanza
._get
_attr
('bar') == '',
265 "Incorrect value returned for an unset XML attribute.")
267 stanza
._set
_attr
('bar', 'a')
268 stanza
._set
_attr
('baz', 'b')
270 self
.check(stanza
, """
271 <foo xmlns="foo" bar="a" baz="b" />
274 self
.failUnless(stanza
._get
_attr
('bar') == 'a',
275 "Retrieved XML attribute value is incorrect.")
277 stanza
._set
_attr
('bar', None)
278 stanza
._del
_attr
('baz')
280 self
.check(stanza
, """
284 self
.failUnless(stanza
._get
_attr
('bar', 'c') == 'c',
285 "Incorrect default value returned for an unset XML attribute.")
287 def testGetSubText(self
):
288 """Test retrieving the contents of a sub element."""
290 class TestStanza(ElementBase
):
293 interfaces
= set(('bar',))
295 def setBar(self
, value
):
296 wrapper
= ET
.Element("{foo}wrapper")
297 bar
= ET
.Element("{foo}bar")
300 self
.xml
.append(wrapper
)
303 return self
._get
_sub
_text
("wrapper/bar", default
="not found")
305 stanza
= TestStanza()
306 self
.failUnless(stanza
['bar'] == 'not found',
307 "Default _get_sub_text value incorrect.")
309 stanza
['bar'] = 'found'
310 self
.check(stanza
, """
317 self
.failUnless(stanza
['bar'] == 'found',
318 "_get_sub_text value incorrect: %s." % stanza
['bar'])
320 def testSubElement(self
):
321 """Test setting the contents of a sub element."""
323 class TestStanza(ElementBase
):
326 interfaces
= set(('bar', 'baz'))
328 def setBaz(self
, value
):
329 self
._set
_sub
_text
("wrapper/baz", text
=value
)
332 return self
._get
_sub
_text
("wrapper/baz")
334 def setBar(self
, value
):
335 self
._set
_sub
_text
("wrapper/bar", text
=value
)
338 return self
._get
_sub
_text
("wrapper/bar")
340 stanza
= TestStanza()
343 self
.check(stanza
, """
351 stanza
._set
_sub
_text
('wrapper/bar', text
='', keep
=True)
352 self
.check(stanza
, """
359 """, use_values
=False)
362 stanza
._set
_sub
_text
('wrapper/bar', text
='')
363 self
.check(stanza
, """
371 def testDelSub(self
):
372 """Test removing sub elements."""
374 class TestStanza(ElementBase
):
377 interfaces
= set(('bar', 'baz'))
379 def setBar(self
, value
):
380 self
._set
_sub
_text
("path/to/only/bar", value
);
383 return self
._get
_sub
_text
("path/to/only/bar")
386 self
._del
_sub
("path/to/only/bar")
388 def setBaz(self
, value
):
389 self
._set
_sub
_text
("path/to/just/baz", value
);
392 return self
._get
_sub
_text
("path/to/just/baz")
395 self
._del
_sub
("path/to/just/baz")
397 stanza
= TestStanza()
401 self
.check(stanza
, """
419 self
.check(stanza
, """
428 """, use_values
=False)
433 stanza
._del
_sub
('path/to/only/bar', all
=True)
435 self
.check(stanza
, """
448 """Test matching a stanza against an XPath expression."""
450 class TestSubStanza(ElementBase
):
453 interfaces
= set(('attrib',))
455 class TestStanza(ElementBase
):
458 interfaces
= set(('bar','baz', 'qux'))
459 sub_interfaces
= set(('qux',))
460 subitem
= (TestSubStanza
,)
462 def setQux(self
, value
):
463 self
._set
_sub
_text
('qux', text
=value
)
466 return self
._get
_sub
_text
('qux')
468 class TestStanzaPlugin(ElementBase
):
470 namespace
= "http://test/slash/bar"
471 interfaces
= set(('attrib',))
473 register_stanza_plugin(TestStanza
, TestStanzaPlugin
)
475 stanza
= TestStanza()
476 self
.failUnless(stanza
.match("foo"),
477 "Stanza did not match its own tag name.")
479 self
.failUnless(stanza
.match("{foo}foo"),
480 "Stanza did not match its own namespaced name.")
483 self
.failUnless(stanza
.match("foo@bar=a"),
484 "Stanza did not match its own name with attribute value check.")
487 self
.failUnless(stanza
.match("foo@bar=a@baz=b"),
488 "Stanza did not match its own name with multiple attributes.")
491 self
.failUnless(stanza
.match("foo/qux"),
492 "Stanza did not match with subelements.")
495 self
.failUnless(stanza
.match("foo/qux") == False,
496 "Stanza matched missing subinterface element.")
498 self
.failUnless(stanza
.match("foo/bar") == False,
499 "Stanza matched nonexistent element.")
501 stanza
['plugin']['attrib'] = 'c'
502 self
.failUnless(stanza
.match("foo/plugin@attrib=c"),
503 "Stanza did not match with plugin and attribute.")
505 self
.failUnless(stanza
.match("foo/{http://test/slash/bar}plugin"),
506 "Stanza did not match with namespaced plugin.")
508 substanza
= TestSubStanza()
509 substanza
['attrib'] = 'd'
510 stanza
.append(substanza
)
511 self
.failUnless(stanza
.match("foo/sub@attrib=d"),
512 "Stanza did not match with substanzas and attribute.")
514 self
.failUnless(stanza
.match("foo/{baz}sub"),
515 "Stanza did not match with namespaced substanza.")
517 def testComparisons(self
):
518 """Test comparing ElementBase objects."""
520 class TestStanza(ElementBase
):
523 interfaces
= set(('bar', 'baz'))
525 stanza1
= TestStanza()
528 self
.failUnless(stanza1
,
529 "Stanza object does not evaluate to True")
531 stanza2
= TestStanza()
534 self
.failUnless(stanza1
!= stanza2
,
535 "Different stanza objects incorrectly compared equal.")
540 self
.failUnless(stanza1
== stanza2
,
541 "Equal stanzas incorrectly compared inequal.")
544 """Test extracting interface names from a stanza object."""
546 class TestStanza(ElementBase
):
549 interfaces
= set(('bar', 'baz'))
550 plugin_attrib
= 'qux'
552 register_stanza_plugin(TestStanza
, TestStanza
)
554 stanza
= TestStanza()
556 self
.failUnless(set(stanza
.keys()) == set(('bar', 'baz')),
557 "Returned set of interface keys does not match expected.")
561 self
.failUnless(set(stanza
.keys()) == set(('bar', 'baz', 'qux')),
562 "Incorrect set of interface and plugin keys.")
565 """Test accessing stanza interfaces using get()."""
567 class TestStanza(ElementBase
):
570 interfaces
= set(('bar', 'baz'))
572 stanza
= TestStanza()
575 self
.failUnless(stanza
.get('bar') == 'a',
576 "Incorrect value returned by stanza.get")
578 self
.failUnless(stanza
.get('baz', 'b') == 'b',
579 "Incorrect default value returned by stanza.get")
581 def testSubStanzas(self
):
582 """Test manipulating substanzas of a stanza object."""
584 class TestSubStanza(ElementBase
):
587 interfaces
= set(('qux',))
589 class TestStanza(ElementBase
):
592 interfaces
= set(('bar', 'baz'))
593 subitem
= (TestSubStanza
,)
595 stanza
= TestStanza()
596 substanza1
= TestSubStanza()
597 substanza2
= TestSubStanza()
598 substanza1
['qux'] = 'a'
599 substanza2
['qux'] = 'b'
601 # Test appending substanzas
602 self
.failUnless(len(stanza
) == 0,
603 "Incorrect empty stanza size.")
605 stanza
.append(substanza1
)
606 self
.check(stanza
, """
610 """, use_values
=False)
611 self
.failUnless(len(stanza
) == 1,
612 "Incorrect stanza size with 1 substanza.")
614 stanza
.append(substanza2
)
615 self
.check(stanza
, """
620 """, use_values
=False)
621 self
.failUnless(len(stanza
) == 2,
622 "Incorrect stanza size with 2 substanzas.")
624 # Test popping substanzas
626 self
.check(stanza
, """
630 """, use_values
=False)
632 # Test iterating over substanzas
633 stanza
.append(substanza1
)
635 for substanza
in stanza
:
636 results
.append(substanza
['qux'])
637 self
.failUnless(results
== ['b', 'a'],
638 "Iteration over substanzas failed: %s." % str(results
))
641 """Test copying stanza objects."""
643 class TestStanza(ElementBase
):
646 interfaces
= set(('bar', 'baz'))
648 stanza1
= TestStanza()
651 stanza2
= stanza1
.__copy__()
653 self
.failUnless(stanza1
== stanza2
,
654 "Copied stanzas are not equal to each other.")
657 self
.failUnless(stanza1
!= stanza2
,
658 "Divergent stanza copies incorrectly compared equal.")
660 suite
= unittest
.TestLoader().loadTestsFromTestCase(TestElementBase
)