diff src/test/test_plugin_xep_0033.py @ 789:0cb423500fbb

test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
author souliane <souliane@mailoo.org>
date Tue, 07 Jan 2014 09:27:53 +0100
parents dd656d745d6a
children 2136be5a44a8
line wrap: on
line diff
--- a/src/test/test_plugin_xep_0033.py	Fri Jan 10 21:12:06 2014 +0100
+++ b/src/test/test_plugin_xep_0033.py	Tue Jan 07 09:27:53 2014 +0100
@@ -55,11 +55,12 @@
         self.plugin.messageReceivedTrigger(stanza, treatments, Const.TEST_PROFILE)
         data = {'extra': {}}
 
-        def assert_(data):
+        def cb(data):
             expected = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR)
-            assert(data['extra']['addresses'] == '%s:%s\n%s:%s\n%s:%s\n' % expected)
+            msg = 'Expected: %s\nGot:      %s' % (expected, data['extra']['addresses'])
+            self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
 
-        treatments.addCallback(assert_)
+        treatments.addCallback(cb)
         treatments.callback(data)
 
     def test_sendMessageTrigger(self):
@@ -86,16 +87,16 @@
         addresses_element = parseXml(addresses_extra.encode('utf-8'))
         expected.addChild(addresses_element)
 
-        def assert_(mess_data):
-            """The mess_data that we got here as been modified by self.plugin.sendMessageTrigger,
+        def assertAddresses(mess_data):
+            """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger,
             check that the addresses element has been added to the stanza."""
             self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
 
-        def fail_(failure, exception_class):
+        def sendMessageErrback(failure, exception_class):
             """If the failure does encapsulate the expected exception, it will be silently
             trapped, otherwise it will be re-raised and will make the test fail"""
             if exception_class == MessageSentAndStored:
-                assert_(failure.value.mess_data)
+                assertAddresses(failure.value.mess_data)
             failure.trap(exception_class)
 
         def checkSentAndStored():
@@ -116,28 +117,16 @@
                 else:
                     sent.append(to_jid)
                     stored.append(to_jid)
-            try:
-                assert(len(self.host.sent_messages) == len(sent))
-                assert(len(self.host.stored_messages) == len(stored))
-                assert(set(self.host.sent_messages) == set(sent))
-                assert(set(self.host.stored_messages) == set(stored))
-            except AssertionError as e:
-                print "----------------------------------------------------"
-                print "Comparing sent and stored messages failed!"
-                print self.host.sent_messages
-                print sent
-                print self.host.stored_messages
-                print stored
-                print "/!\ see the comments in XEP_0033.sendAndStoreMessage"
-                print "----------------------------------------------------"
-                raise e
+            msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
+            self.assertEqualUnsortedList(self.host.sent_messages, sent, msg)
+            self.assertEqualUnsortedList(self.host.stored_messages, stored, msg)
 
         # feature is not supported, abort the message
         self.host.memory.init()
         treatments = defer.Deferred()
         data = deepcopy(mess_data)
         self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
-        treatments.addCallbacks(assert_, lambda failure: fail_(failure, AbortSendMessage))
+        treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, AbortSendMessage))
         treatments.callback(data)
 
         # feature is supported
@@ -147,7 +136,7 @@
         treatments = defer.Deferred()
         data = deepcopy(mess_data)
         self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
-        treatments.addCallbacks(assert_, lambda failure: fail_(failure, MessageSentAndStored))
+        treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
         treatments.callback(data)
         checkSentAndStored()
 
@@ -159,6 +148,6 @@
         data = deepcopy(mess_data)
         data["to"] = Const.TEST_JID
         self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
-        treatments.addCallbacks(assert_, lambda failure: fail_(failure, MessageSentAndStored))
+        treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
         treatments.callback(mess_data)
         checkSentAndStored()