From 9e86a7b357afffdf5cb99cf4de76472f29f740e4 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 5 May 2012 14:01:13 -0700 Subject: Tidy up and add tests for multi_attrib plugins. --- tests/test_stanza_element.py | 160 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) (limited to 'tests') diff --git a/tests/test_stanza_element.py b/tests/test_stanza_element.py index 60ccb7c9..09093003 100644 --- a/tests/test_stanza_element.py +++ b/tests/test_stanza_element.py @@ -774,5 +774,165 @@ class TestElementBase(SleekTest): """) + def testGetMultiAttrib(self): + """Test retrieving multi_attrib substanzas.""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'foo' + interfaces = set() + + class TestMultiStanza1(ElementBase): + name = 'bar' + namespace = 'bar' + plugin_attrib = name + plugin_multi_attrib = 'bars' + + class TestMultiStanza2(ElementBase): + name = 'baz' + namespace = 'baz' + plugin_attrib = name + plugin_multi_attrib = 'bazs' + + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) + register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) + + stanza = TestStanza() + stanza.append(TestMultiStanza1()) + stanza.append(TestMultiStanza2()) + stanza.append(TestMultiStanza1()) + stanza.append(TestMultiStanza2()) + + self.check(stanza, """ + + + + + + + """, use_values=False) + + bars = stanza['bars'] + bazs = stanza['bazs'] + + for bar in bars: + self.check(bar, """ + + """) + + for baz in bazs: + self.check(baz, """ + + """) + + self.assertEqual(len(bars), 2, + "Wrong number of stanzas: %s" % len(bars)) + self.assertEqual(len(bazs), 2, + "Wrong number of stanzas: %s" % len(bazs)) + + def testSetMultiAttrib(self): + """Test setting multi_attrib substanzas.""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'foo' + interfaces = set() + + class TestMultiStanza1(ElementBase): + name = 'bar' + namespace = 'bar' + plugin_attrib = name + plugin_multi_attrib = 'bars' + + class TestMultiStanza2(ElementBase): + name = 'baz' + namespace = 'baz' + plugin_attrib = name + plugin_multi_attrib = 'bazs' + + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) + register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) + + stanza = TestStanza() + stanza['bars'] = [TestMultiStanza1(), TestMultiStanza1()] + stanza['bazs'] = [TestMultiStanza2(), TestMultiStanza2()] + + self.check(stanza, """ + + + + + + + """, use_values=False) + + self.assertEqual(len(stanza['substanzas']), 4, + "Wrong number of substanzas: %s" % len(stanza['substanzas'])) + + stanza['bars'] = [TestMultiStanza1()] + + self.check(stanza, """ + + + + + + """, use_values=False) + + self.assertEqual(len(stanza['substanzas']), 3, + "Wrong number of substanzas: %s" % len(stanza['substanzas'])) + + + def testDelMultiAttrib(self): + """Test deleting multi_attrib substanzas.""" + + class TestStanza(ElementBase): + name = 'foo' + namespace = 'foo' + interfaces = set() + + class TestMultiStanza1(ElementBase): + name = 'bar' + namespace = 'bar' + plugin_attrib = name + plugin_multi_attrib = 'bars' + + class TestMultiStanza2(ElementBase): + name = 'baz' + namespace = 'baz' + plugin_attrib = name + plugin_multi_attrib = 'bazs' + + register_stanza_plugin(TestStanza, TestMultiStanza1, iterable=True) + register_stanza_plugin(TestStanza, TestMultiStanza2, iterable=True) + + stanza = TestStanza() + bars = [TestMultiStanza1(), TestMultiStanza1()] + bazs = [TestMultiStanza2(), TestMultiStanza2()] + + stanza['bars'] = bars + stanza['bazs'] = bazs + + self.check(stanza, """ + + + + + + + """, use_values=False) + + del stanza['bars'] + + self.check(stanza, """ + + + + + """, use_values=False) + + self.assertEqual(len(stanza['substanzas']), 2, + "Wrong number of substanzas: %s" % len(stanza['substanzas'])) + suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase) -- cgit v1.2.3