comparison 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
comparison
equal deleted inserted replaced
788:d6652683c572 789:0cb423500fbb
53 stanza = parseXml(xml.encode("utf-8")) 53 stanza = parseXml(xml.encode("utf-8"))
54 treatments = defer.Deferred() 54 treatments = defer.Deferred()
55 self.plugin.messageReceivedTrigger(stanza, treatments, Const.TEST_PROFILE) 55 self.plugin.messageReceivedTrigger(stanza, treatments, Const.TEST_PROFILE)
56 data = {'extra': {}} 56 data = {'extra': {}}
57 57
58 def assert_(data): 58 def cb(data):
59 expected = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR) 59 expected = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR)
60 assert(data['extra']['addresses'] == '%s:%s\n%s:%s\n%s:%s\n' % expected) 60 msg = 'Expected: %s\nGot: %s' % (expected, data['extra']['addresses'])
61 self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
61 62
62 treatments.addCallback(assert_) 63 treatments.addCallback(cb)
63 treatments.callback(data) 64 treatments.callback(data)
64 65
65 def test_sendMessageTrigger(self): 66 def test_sendMessageTrigger(self):
66 mess_data = {"to": self.host.getClientHostJid(Const.TEST_PROFILE), 67 mess_data = {"to": self.host.getClientHostJid(Const.TEST_PROFILE),
67 "type": "chat", 68 "type": "chat",
84 <address type='%s' jid='%s'/> 85 <address type='%s' jid='%s'/>
85 </addresses>""" % addresses 86 </addresses>""" % addresses
86 addresses_element = parseXml(addresses_extra.encode('utf-8')) 87 addresses_element = parseXml(addresses_extra.encode('utf-8'))
87 expected.addChild(addresses_element) 88 expected.addChild(addresses_element)
88 89
89 def assert_(mess_data): 90 def assertAddresses(mess_data):
90 """The mess_data that we got here as been modified by self.plugin.sendMessageTrigger, 91 """The mess_data that we got here has been modified by self.plugin.sendMessageTrigger,
91 check that the addresses element has been added to the stanza.""" 92 check that the addresses element has been added to the stanza."""
92 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) 93 self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
93 94
94 def fail_(failure, exception_class): 95 def sendMessageErrback(failure, exception_class):
95 """If the failure does encapsulate the expected exception, it will be silently 96 """If the failure does encapsulate the expected exception, it will be silently
96 trapped, otherwise it will be re-raised and will make the test fail""" 97 trapped, otherwise it will be re-raised and will make the test fail"""
97 if exception_class == MessageSentAndStored: 98 if exception_class == MessageSentAndStored:
98 assert_(failure.value.mess_data) 99 assertAddresses(failure.value.mess_data)
99 failure.trap(exception_class) 100 failure.trap(exception_class)
100 101
101 def checkSentAndStored(): 102 def checkSentAndStored():
102 """Check that all the recipients got their messages and that the history has been filled. 103 """Check that all the recipients got their messages and that the history has been filled.
103 /!\ see the comments in XEP_0033.sendAndStoreMessage""" 104 /!\ see the comments in XEP_0033.sendAndStoreMessage"""
114 cache.add(host) 115 cache.add(host)
115 stored.append(to_jid) 116 stored.append(to_jid)
116 else: 117 else:
117 sent.append(to_jid) 118 sent.append(to_jid)
118 stored.append(to_jid) 119 stored.append(to_jid)
119 try: 120 msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
120 assert(len(self.host.sent_messages) == len(sent)) 121 self.assertEqualUnsortedList(self.host.sent_messages, sent, msg)
121 assert(len(self.host.stored_messages) == len(stored)) 122 self.assertEqualUnsortedList(self.host.stored_messages, stored, msg)
122 assert(set(self.host.sent_messages) == set(sent))
123 assert(set(self.host.stored_messages) == set(stored))
124 except AssertionError as e:
125 print "----------------------------------------------------"
126 print "Comparing sent and stored messages failed!"
127 print self.host.sent_messages
128 print sent
129 print self.host.stored_messages
130 print stored
131 print "/!\ see the comments in XEP_0033.sendAndStoreMessage"
132 print "----------------------------------------------------"
133 raise e
134 123
135 # feature is not supported, abort the message 124 # feature is not supported, abort the message
136 self.host.memory.init() 125 self.host.memory.init()
137 treatments = defer.Deferred() 126 treatments = defer.Deferred()
138 data = deepcopy(mess_data) 127 data = deepcopy(mess_data)
139 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) 128 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
140 treatments.addCallbacks(assert_, lambda failure: fail_(failure, AbortSendMessage)) 129 treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, AbortSendMessage))
141 treatments.callback(data) 130 treatments.callback(data)
142 131
143 # feature is supported 132 # feature is supported
144 self.host.init() 133 self.host.init()
145 self.host.memory.init() 134 self.host.memory.init()
146 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE) 135 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE)
147 treatments = defer.Deferred() 136 treatments = defer.Deferred()
148 data = deepcopy(mess_data) 137 data = deepcopy(mess_data)
149 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) 138 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
150 treatments.addCallbacks(assert_, lambda failure: fail_(failure, MessageSentAndStored)) 139 treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
151 treatments.callback(data) 140 treatments.callback(data)
152 checkSentAndStored() 141 checkSentAndStored()
153 142
154 # check that a wrong recipient entity is fixed by the backend 143 # check that a wrong recipient entity is fixed by the backend
155 self.host.init() 144 self.host.init()
157 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE) 146 self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE)
158 treatments = defer.Deferred() 147 treatments = defer.Deferred()
159 data = deepcopy(mess_data) 148 data = deepcopy(mess_data)
160 data["to"] = Const.TEST_JID 149 data["to"] = Const.TEST_JID
161 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE) 150 self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
162 treatments.addCallbacks(assert_, lambda failure: fail_(failure, MessageSentAndStored)) 151 treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
163 treatments.callback(mess_data) 152 treatments.callback(mess_data)
164 checkSentAndStored() 153 checkSentAndStored()