2007-02-26 04:17:04 +01:00
|
|
|
from django.dispatch.dispatcher import *
|
|
|
|
from django.dispatch import dispatcher, robust
|
2007-02-26 04:44:36 +01:00
|
|
|
import unittest
|
|
|
|
import copy
|
2007-02-26 04:17:04 +01:00
|
|
|
|
|
|
|
def x(a):
|
|
|
|
return a
|
|
|
|
|
2007-02-26 04:44:36 +01:00
|
|
|
class Dummy(object):
|
2007-02-26 04:17:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
class Callable(object):
|
2007-02-26 04:44:36 +01:00
|
|
|
def __call__(self, a):
|
2007-02-26 04:17:04 +01:00
|
|
|
return a
|
2007-02-26 04:44:36 +01:00
|
|
|
|
|
|
|
def a(self, a):
|
2007-02-26 04:17:04 +01:00
|
|
|
return a
|
|
|
|
|
|
|
|
class DispatcherTests(unittest.TestCase):
|
|
|
|
"""Test suite for dispatcher (barely started)"""
|
2007-02-26 04:44:36 +01:00
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def setUp(self):
|
|
|
|
# track the initial state, since it's possible that others have bleed receivers in
|
|
|
|
self.sendersBack = copy.copy(dispatcher.sendersBack)
|
|
|
|
self.connections = copy.copy(dispatcher.connections)
|
|
|
|
self.senders = copy.copy(dispatcher.senders)
|
2007-02-26 04:44:36 +01:00
|
|
|
|
|
|
|
def _testIsClean(self):
|
2007-02-26 04:17:04 +01:00
|
|
|
"""Assert that everything has been cleaned up automatically"""
|
|
|
|
self.assertEqual(dispatcher.sendersBack, self.sendersBack)
|
|
|
|
self.assertEqual(dispatcher.connections, self.connections)
|
|
|
|
self.assertEqual(dispatcher.senders, self.senders)
|
|
|
|
|
2007-02-26 04:44:36 +01:00
|
|
|
def testExact(self):
|
2007-02-26 04:17:04 +01:00
|
|
|
a = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(x, signal, a)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = [(x,a)]
|
|
|
|
result = send('this',a, a=a)
|
|
|
|
self.assertEqual(result, expected)
|
2007-02-26 04:44:36 +01:00
|
|
|
disconnect(x, signal, a)
|
|
|
|
self.assertEqual(list(getAllReceivers(a,signal)), [])
|
|
|
|
self._testIsClean()
|
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testAnonymousSend(self):
|
|
|
|
a = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(x, signal)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = [(x,a)]
|
|
|
|
result = send(signal,None, a=a)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
disconnect(x, signal)
|
|
|
|
self.assertEqual(list(getAllReceivers(None,signal)), [])
|
|
|
|
self._testIsClean()
|
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testAnyRegistration(self):
|
|
|
|
a = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(x, signal, Any)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = [(x,a)]
|
|
|
|
result = send('this',object(), a=a)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
disconnect(x, signal, Any)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = []
|
|
|
|
result = send('this',object(), a=a)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertEqual(list(getAllReceivers(Any,signal)), [])
|
2007-02-26 04:17:04 +01:00
|
|
|
|
2007-02-26 04:44:36 +01:00
|
|
|
self._testIsClean()
|
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testAnyRegistration2(self):
|
|
|
|
a = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(x, Any, a)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = [(x,a)]
|
|
|
|
result = send('this',a, a=a)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
disconnect(x, Any, a)
|
|
|
|
self.assertEqual(list(getAllReceivers(a,Any)), [])
|
|
|
|
self._testIsClean()
|
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testGarbageCollected(self):
|
|
|
|
a = Callable()
|
|
|
|
b = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(a.a, signal, b)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = []
|
|
|
|
del a
|
|
|
|
result = send('this',b, a=b)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(result, expected)
|
|
|
|
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
|
|
|
self._testIsClean()
|
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testGarbageCollectedObj(self):
|
|
|
|
class x:
|
2007-02-26 04:44:36 +01:00
|
|
|
def __call__(self, a):
|
2007-02-26 04:17:04 +01:00
|
|
|
return a
|
|
|
|
a = Callable()
|
|
|
|
b = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(a, signal, b)
|
2007-02-26 04:17:04 +01:00
|
|
|
expected = []
|
|
|
|
del a
|
|
|
|
result = send('this',b, a=b)
|
|
|
|
self.assertEqual(result, expected)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
|
|
|
self._testIsClean()
|
2007-02-26 04:17:04 +01:00
|
|
|
|
2007-02-26 04:44:36 +01:00
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
def testMultipleRegistration(self):
|
|
|
|
a = Callable()
|
|
|
|
b = Dummy()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(a, signal, b)
|
|
|
|
connect(a, signal, b)
|
|
|
|
connect(a, signal, b)
|
|
|
|
connect(a, signal, b)
|
|
|
|
connect(a, signal, b)
|
|
|
|
connect(a, signal, b)
|
2007-02-26 04:17:04 +01:00
|
|
|
result = send('this',b, a=b)
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
self.assertEqual(len(list(getAllReceivers(b,signal))), 1)
|
2007-02-26 04:17:04 +01:00
|
|
|
del a
|
|
|
|
del b
|
|
|
|
del result
|
2007-02-26 04:44:36 +01:00
|
|
|
self._testIsClean()
|
|
|
|
|
|
|
|
def testRobust(self):
|
2007-02-26 04:17:04 +01:00
|
|
|
"""Test the sendRobust function"""
|
2007-02-26 04:44:36 +01:00
|
|
|
def fails():
|
|
|
|
raise ValueError('this')
|
2007-02-26 04:17:04 +01:00
|
|
|
a = object()
|
|
|
|
signal = 'this'
|
2007-02-26 04:44:36 +01:00
|
|
|
connect(fails, Any, a)
|
2007-02-26 04:17:04 +01:00
|
|
|
result = robust.sendRobust('this',a, a=a)
|
|
|
|
err = result[0][1]
|
2007-02-26 04:44:36 +01:00
|
|
|
self.assert_(isinstance(err, ValueError))
|
|
|
|
self.assertEqual(err.args, ('this',))
|
2007-02-26 04:17:04 +01:00
|
|
|
|
|
|
|
def getSuite():
|
|
|
|
return unittest.makeSuite(DispatcherTests,'test')
|
2007-02-26 04:44:36 +01:00
|
|
|
|
2007-02-26 04:17:04 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main ()
|