Updated the dispatch unit tests to be consistant with our coding style.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4589 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
357e26baf6
commit
907e4f840e
|
@ -1,24 +1,21 @@
|
||||||
from django.dispatch.dispatcher import *
|
from django.dispatch.dispatcher import *
|
||||||
from django.dispatch import dispatcher, robust
|
from django.dispatch import dispatcher, robust
|
||||||
|
import unittest
|
||||||
import unittest, pprint, copy
|
import copy
|
||||||
|
|
||||||
def x(a):
|
def x(a):
|
||||||
return a
|
return a
|
||||||
|
|
||||||
class Dummy( object ):
|
class Dummy(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Callable(object):
|
class Callable(object):
|
||||||
|
def __call__(self, a):
|
||||||
def __call__( self, a ):
|
|
||||||
return a
|
return a
|
||||||
|
|
||||||
def a( self, a ):
|
def a(self, a):
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
class DispatcherTests(unittest.TestCase):
|
class DispatcherTests(unittest.TestCase):
|
||||||
"""Test suite for dispatcher (barely started)"""
|
"""Test suite for dispatcher (barely started)"""
|
||||||
|
|
||||||
|
@ -28,117 +25,117 @@ class DispatcherTests(unittest.TestCase):
|
||||||
self.connections = copy.copy(dispatcher.connections)
|
self.connections = copy.copy(dispatcher.connections)
|
||||||
self.senders = copy.copy(dispatcher.senders)
|
self.senders = copy.copy(dispatcher.senders)
|
||||||
|
|
||||||
def _isclean( self ):
|
def _testIsClean(self):
|
||||||
"""Assert that everything has been cleaned up automatically"""
|
"""Assert that everything has been cleaned up automatically"""
|
||||||
self.assertEqual(dispatcher.sendersBack, self.sendersBack)
|
self.assertEqual(dispatcher.sendersBack, self.sendersBack)
|
||||||
self.assertEqual(dispatcher.connections, self.connections)
|
self.assertEqual(dispatcher.connections, self.connections)
|
||||||
self.assertEqual(dispatcher.senders, self.senders)
|
self.assertEqual(dispatcher.senders, self.senders)
|
||||||
|
|
||||||
def testExact (self):
|
def testExact(self):
|
||||||
a = Dummy()
|
a = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( x, signal, a )
|
connect(x, signal, a)
|
||||||
expected = [(x,a)]
|
expected = [(x,a)]
|
||||||
result = send('this',a, a=a)
|
result = send('this',a, a=a)
|
||||||
self.assertEqual(result, expected)
|
self.assertEqual(result, expected)
|
||||||
disconnect( x, signal, a )
|
disconnect(x, signal, a)
|
||||||
self.assertEqual(len(list(getAllReceivers(a,signal))), 0)
|
self.assertEqual(list(getAllReceivers(a,signal)), [])
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
def testAnonymousSend(self):
|
def testAnonymousSend(self):
|
||||||
a = Dummy()
|
a = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( x, signal )
|
connect(x, signal)
|
||||||
expected = [(x,a)]
|
expected = [(x,a)]
|
||||||
result = send(signal,None, a=a)
|
result = send(signal,None, a=a)
|
||||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
self.assertEqual(result, expected)
|
||||||
disconnect( x, signal )
|
disconnect(x, signal)
|
||||||
assert len(list(getAllReceivers(None,signal))) == 0
|
self.assertEqual(list(getAllReceivers(None,signal)), [])
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
def testAnyRegistration(self):
|
def testAnyRegistration(self):
|
||||||
a = Dummy()
|
a = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( x, signal, Any )
|
connect(x, signal, Any)
|
||||||
expected = [(x,a)]
|
expected = [(x,a)]
|
||||||
result = send('this',object(), a=a)
|
result = send('this',object(), a=a)
|
||||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
self.assertEqual(result, expected)
|
||||||
disconnect( x, signal, Any )
|
disconnect(x, signal, Any)
|
||||||
expected = []
|
expected = []
|
||||||
result = send('this',object(), a=a)
|
result = send('this',object(), a=a)
|
||||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
self.assertEqual(result, expected)
|
||||||
assert len(list(getAllReceivers(Any,signal))) == 0
|
self.assertEqual(list(getAllReceivers(Any,signal)), [])
|
||||||
|
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
def testAnyRegistration2(self):
|
def testAnyRegistration2(self):
|
||||||
a = Dummy()
|
a = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( x, Any, a )
|
connect(x, Any, a)
|
||||||
expected = [(x,a)]
|
expected = [(x,a)]
|
||||||
result = send('this',a, a=a)
|
result = send('this',a, a=a)
|
||||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
self.assertEqual(result, expected)
|
||||||
disconnect( x, Any, a )
|
disconnect(x, Any, a)
|
||||||
assert len(list(getAllReceivers(a,Any))) == 0
|
self.assertEqual(list(getAllReceivers(a,Any)), [])
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
def testGarbageCollected(self):
|
def testGarbageCollected(self):
|
||||||
a = Callable()
|
a = Callable()
|
||||||
b = Dummy()
|
b = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( a.a, signal, b )
|
connect(a.a, signal, b)
|
||||||
expected = []
|
|
||||||
del a
|
|
||||||
result = send('this',b, a=b)
|
|
||||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
|
||||||
assert len(list(getAllReceivers(b,signal))) == 0, """Remaining handlers: %s"""%(getAllReceivers(b,signal),)
|
|
||||||
self._isclean()
|
|
||||||
|
|
||||||
def testGarbageCollectedObj(self):
|
|
||||||
class x:
|
|
||||||
def __call__( self, a ):
|
|
||||||
return a
|
|
||||||
a = Callable()
|
|
||||||
b = Dummy()
|
|
||||||
signal = 'this'
|
|
||||||
connect( a, signal, b )
|
|
||||||
expected = []
|
expected = []
|
||||||
del a
|
del a
|
||||||
result = send('this',b, a=b)
|
result = send('this',b, a=b)
|
||||||
self.assertEqual(result, expected)
|
self.assertEqual(result, expected)
|
||||||
self.assertEqual(len(list(getAllReceivers(b,signal))), 0)
|
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
|
def testGarbageCollectedObj(self):
|
||||||
|
class x:
|
||||||
|
def __call__(self, a):
|
||||||
|
return a
|
||||||
|
a = Callable()
|
||||||
|
b = Dummy()
|
||||||
|
signal = 'this'
|
||||||
|
connect(a, signal, b)
|
||||||
|
expected = []
|
||||||
|
del a
|
||||||
|
result = send('this',b, a=b)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
||||||
|
self._testIsClean()
|
||||||
|
|
||||||
|
|
||||||
def testMultipleRegistration(self):
|
def testMultipleRegistration(self):
|
||||||
a = Callable()
|
a = Callable()
|
||||||
b = Dummy()
|
b = Dummy()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
connect( a, signal, b )
|
connect(a, signal, b)
|
||||||
result = send('this',b, a=b)
|
result = send('this',b, a=b)
|
||||||
assert len( result ) == 1, result
|
self.assertEqual(len(result), 1)
|
||||||
assert len(list(getAllReceivers(b,signal))) == 1, """Remaining handlers: %s"""%(getAllReceivers(b,signal),)
|
self.assertEqual(len(list(getAllReceivers(b,signal))), 1)
|
||||||
del a
|
del a
|
||||||
del b
|
del b
|
||||||
del result
|
del result
|
||||||
self._isclean()
|
self._testIsClean()
|
||||||
|
|
||||||
def testRobust( self ):
|
def testRobust(self):
|
||||||
"""Test the sendRobust function"""
|
"""Test the sendRobust function"""
|
||||||
def fails( ):
|
def fails():
|
||||||
raise ValueError( 'this' )
|
raise ValueError('this')
|
||||||
a = object()
|
a = object()
|
||||||
signal = 'this'
|
signal = 'this'
|
||||||
connect( fails, Any, a )
|
connect(fails, Any, a)
|
||||||
result = robust.sendRobust('this',a, a=a)
|
result = robust.sendRobust('this',a, a=a)
|
||||||
err = result[0][1]
|
err = result[0][1]
|
||||||
assert isinstance( err, ValueError )
|
self.assert_(isinstance(err, ValueError))
|
||||||
assert err.args == ('this',)
|
self.assertEqual(err.args, ('this',))
|
||||||
|
|
||||||
def getSuite():
|
def getSuite():
|
||||||
return unittest.makeSuite(DispatcherTests,'test')
|
return unittest.makeSuite(DispatcherTests,'test')
|
||||||
|
|
|
@ -1,22 +1,29 @@
|
||||||
from django.dispatch.robustapply import *
|
from django.dispatch.robustapply import *
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
def noArgument():
|
def noArgument():
|
||||||
pass
|
pass
|
||||||
def oneArgument (blah):
|
|
||||||
|
def oneArgument(blah):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def twoArgument(blah, other):
|
def twoArgument(blah, other):
|
||||||
pass
|
pass
|
||||||
class TestCases( unittest.TestCase ):
|
|
||||||
def test01( self ):
|
class TestCases(unittest.TestCase):
|
||||||
robustApply(noArgument )
|
def test01(self):
|
||||||
def test02( self ):
|
robustApply(noArgument)
|
||||||
self.assertRaises( TypeError, robustApply, noArgument, "this" )
|
|
||||||
def test03( self ):
|
def test02(self):
|
||||||
self.assertRaises( TypeError, robustApply, oneArgument )
|
self.assertRaises(TypeError, robustApply, noArgument, "this")
|
||||||
def test04( self ):
|
|
||||||
|
def test03(self):
|
||||||
|
self.assertRaises(TypeError, robustApply, oneArgument)
|
||||||
|
|
||||||
|
def test04(self):
|
||||||
"""Raise error on duplication of a particular argument"""
|
"""Raise error on duplication of a particular argument"""
|
||||||
self.assertRaises( TypeError, robustApply, oneArgument, "this", blah = "that" )
|
self.assertRaises(TypeError, robustApply, oneArgument, "this", blah = "that")
|
||||||
|
|
||||||
def getSuite():
|
def getSuite():
|
||||||
return unittest.makeSuite(TestCases,'test')
|
return unittest.makeSuite(TestCases,'test')
|
||||||
|
|
|
@ -1,69 +1,70 @@
|
||||||
from django.dispatch.saferef import *
|
from django.dispatch.saferef import *
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
class Test1( object):
|
|
||||||
def x( self ):
|
class Test1(object):
|
||||||
|
def x(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test2(obj):
|
def test2(obj):
|
||||||
pass
|
pass
|
||||||
class Test2( object ):
|
|
||||||
def __call__( self, obj ):
|
class Test2(object):
|
||||||
|
def __call__(self, obj):
|
||||||
pass
|
pass
|
||||||
class Tester (unittest.TestCase):
|
|
||||||
def setUp (self):
|
class Tester(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
ts = []
|
ts = []
|
||||||
ss = []
|
ss = []
|
||||||
for x in xrange( 5000 ):
|
for x in xrange(5000):
|
||||||
t = Test1()
|
t = Test1()
|
||||||
ts.append( t)
|
ts.append(t)
|
||||||
s = safeRef(t.x, self._closure )
|
s = safeRef(t.x, self._closure)
|
||||||
ss.append( s)
|
ss.append(s)
|
||||||
ts.append( test2 )
|
ts.append(test2)
|
||||||
ss.append( safeRef(test2, self._closure) )
|
ss.append(safeRef(test2, self._closure))
|
||||||
for x in xrange( 30 ):
|
for x in xrange(30):
|
||||||
t = Test2()
|
t = Test2()
|
||||||
ts.append( t)
|
ts.append(t)
|
||||||
s = safeRef(t, self._closure )
|
s = safeRef(t, self._closure)
|
||||||
ss.append( s)
|
ss.append(s)
|
||||||
self.ts = ts
|
self.ts = ts
|
||||||
self.ss = ss
|
self.ss = ss
|
||||||
self.closureCount = 0
|
self.closureCount = 0
|
||||||
def tearDown( self ):
|
|
||||||
|
def tearDown(self):
|
||||||
del self.ts
|
del self.ts
|
||||||
del self.ss
|
del self.ss
|
||||||
|
|
||||||
def testIn(self):
|
def testIn(self):
|
||||||
"""Test the "in" operator for safe references (cmp)"""
|
"""Test the "in" operator for safe references (cmp)"""
|
||||||
for t in self.ts[:50]:
|
for t in self.ts[:50]:
|
||||||
assert safeRef(t.x) in self.ss
|
self.assert_(safeRef(t.x) in self.ss)
|
||||||
|
|
||||||
def testValid(self):
|
def testValid(self):
|
||||||
"""Test that the references are valid (return instance methods)"""
|
"""Test that the references are valid (return instance methods)"""
|
||||||
for s in self.ss:
|
for s in self.ss:
|
||||||
assert s()
|
self.assert_(s())
|
||||||
|
|
||||||
def testShortCircuit (self):
|
def testShortCircuit (self):
|
||||||
"""Test that creation short-circuits to reuse existing references"""
|
"""Test that creation short-circuits to reuse existing references"""
|
||||||
sd = {}
|
sd = {}
|
||||||
for s in self.ss:
|
for s in self.ss:
|
||||||
sd[s] = 1
|
sd[s] = 1
|
||||||
for t in self.ts:
|
for t in self.ts:
|
||||||
if hasattr( t, 'x'):
|
if hasattr(t, 'x'):
|
||||||
assert sd.has_key( safeRef(t.x))
|
self.assert_(sd.has_key(safeRef(t.x)))
|
||||||
else:
|
else:
|
||||||
assert sd.has_key( safeRef(t))
|
self.assert_(sd.has_key(safeRef(t)))
|
||||||
|
|
||||||
def testRepresentation (self):
|
def testRepresentation (self):
|
||||||
"""Test that the reference object's representation works
|
"""Test that the reference object's representation works
|
||||||
|
|
||||||
XXX Doesn't currently check the results, just that no error
|
XXX Doesn't currently check the results, just that no error
|
||||||
is raised
|
is raised
|
||||||
"""
|
"""
|
||||||
repr( self.ss[-1] )
|
repr(self.ss[-1])
|
||||||
|
|
||||||
def test(self):
|
|
||||||
self.closureCount = 0
|
|
||||||
wholeI = len(self.ts)
|
|
||||||
for i in xrange( len(self.ts)-1, -1, -1):
|
|
||||||
del self.ts[i]
|
|
||||||
if wholeI-i != self.closureCount:
|
|
||||||
"""Unexpected number of items closed, expected %s, got %s closed"""%( wholeI-i,self.closureCount)
|
|
||||||
|
|
||||||
def _closure(self, ref):
|
def _closure(self, ref):
|
||||||
"""Dumb utility mechanism to increment deletion counter"""
|
"""Dumb utility mechanism to increment deletion counter"""
|
||||||
|
@ -73,4 +74,4 @@ def getSuite():
|
||||||
return unittest.makeSuite(Tester,'test')
|
return unittest.makeSuite(Tester,'test')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main ()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue