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,7 +1,7 @@
|
|||
from django.dispatch.dispatcher import *
|
||||
from django.dispatch import dispatcher, robust
|
||||
|
||||
import unittest, pprint, copy
|
||||
import unittest
|
||||
import copy
|
||||
|
||||
def x(a):
|
||||
return a
|
||||
|
@ -9,16 +9,13 @@ def x(a):
|
|||
class Dummy(object):
|
||||
pass
|
||||
|
||||
|
||||
class Callable(object):
|
||||
|
||||
def __call__(self, a):
|
||||
return a
|
||||
|
||||
def a(self, a):
|
||||
return a
|
||||
|
||||
|
||||
class DispatcherTests(unittest.TestCase):
|
||||
"""Test suite for dispatcher (barely started)"""
|
||||
|
||||
|
@ -28,7 +25,7 @@ class DispatcherTests(unittest.TestCase):
|
|||
self.connections = copy.copy(dispatcher.connections)
|
||||
self.senders = copy.copy(dispatcher.senders)
|
||||
|
||||
def _isclean( self ):
|
||||
def _testIsClean(self):
|
||||
"""Assert that everything has been cleaned up automatically"""
|
||||
self.assertEqual(dispatcher.sendersBack, self.sendersBack)
|
||||
self.assertEqual(dispatcher.connections, self.connections)
|
||||
|
@ -42,8 +39,8 @@ class DispatcherTests(unittest.TestCase):
|
|||
result = send('this',a, a=a)
|
||||
self.assertEqual(result, expected)
|
||||
disconnect(x, signal, a)
|
||||
self.assertEqual(len(list(getAllReceivers(a,signal))), 0)
|
||||
self._isclean()
|
||||
self.assertEqual(list(getAllReceivers(a,signal)), [])
|
||||
self._testIsClean()
|
||||
|
||||
def testAnonymousSend(self):
|
||||
a = Dummy()
|
||||
|
@ -51,10 +48,10 @@ class DispatcherTests(unittest.TestCase):
|
|||
connect(x, signal)
|
||||
expected = [(x,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)
|
||||
assert len(list(getAllReceivers(None,signal))) == 0
|
||||
self._isclean()
|
||||
self.assertEqual(list(getAllReceivers(None,signal)), [])
|
||||
self._testIsClean()
|
||||
|
||||
def testAnyRegistration(self):
|
||||
a = Dummy()
|
||||
|
@ -62,14 +59,14 @@ class DispatcherTests(unittest.TestCase):
|
|||
connect(x, signal, Any)
|
||||
expected = [(x,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)
|
||||
expected = []
|
||||
result = send('this',object(), a=a)
|
||||
assert result == expected,"""Send didn't return expected result:\n\texpected:%s\n\tgot:%s"""% (expected, result)
|
||||
assert len(list(getAllReceivers(Any,signal))) == 0
|
||||
self.assertEqual(result, expected)
|
||||
self.assertEqual(list(getAllReceivers(Any,signal)), [])
|
||||
|
||||
self._isclean()
|
||||
self._testIsClean()
|
||||
|
||||
def testAnyRegistration2(self):
|
||||
a = Dummy()
|
||||
|
@ -77,10 +74,10 @@ class DispatcherTests(unittest.TestCase):
|
|||
connect(x, Any, a)
|
||||
expected = [(x,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)
|
||||
assert len(list(getAllReceivers(a,Any))) == 0
|
||||
self._isclean()
|
||||
self.assertEqual(list(getAllReceivers(a,Any)), [])
|
||||
self._testIsClean()
|
||||
|
||||
def testGarbageCollected(self):
|
||||
a = Callable()
|
||||
|
@ -90,9 +87,9 @@ class DispatcherTests(unittest.TestCase):
|
|||
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()
|
||||
self.assertEqual(result, expected)
|
||||
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
||||
self._testIsClean()
|
||||
|
||||
def testGarbageCollectedObj(self):
|
||||
class x:
|
||||
|
@ -106,8 +103,8 @@ class DispatcherTests(unittest.TestCase):
|
|||
del a
|
||||
result = send('this',b, a=b)
|
||||
self.assertEqual(result, expected)
|
||||
self.assertEqual(len(list(getAllReceivers(b,signal))), 0)
|
||||
self._isclean()
|
||||
self.assertEqual(list(getAllReceivers(b,signal)), [])
|
||||
self._testIsClean()
|
||||
|
||||
|
||||
def testMultipleRegistration(self):
|
||||
|
@ -121,12 +118,12 @@ class DispatcherTests(unittest.TestCase):
|
|||
connect(a, signal, b)
|
||||
connect(a, signal, b)
|
||||
result = send('this',b, a=b)
|
||||
assert len( result ) == 1, result
|
||||
assert len(list(getAllReceivers(b,signal))) == 1, """Remaining handlers: %s"""%(getAllReceivers(b,signal),)
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertEqual(len(list(getAllReceivers(b,signal))), 1)
|
||||
del a
|
||||
del b
|
||||
del result
|
||||
self._isclean()
|
||||
self._testIsClean()
|
||||
|
||||
def testRobust(self):
|
||||
"""Test the sendRobust function"""
|
||||
|
@ -137,8 +134,8 @@ class DispatcherTests(unittest.TestCase):
|
|||
connect(fails, Any, a)
|
||||
result = robust.sendRobust('this',a, a=a)
|
||||
err = result[0][1]
|
||||
assert isinstance( err, ValueError )
|
||||
assert err.args == ('this',)
|
||||
self.assert_(isinstance(err, ValueError))
|
||||
self.assertEqual(err.args, ('this',))
|
||||
|
||||
def getSuite():
|
||||
return unittest.makeSuite(DispatcherTests,'test')
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
from django.dispatch.robustapply import *
|
||||
|
||||
import unittest
|
||||
|
||||
def noArgument():
|
||||
pass
|
||||
|
||||
def oneArgument(blah):
|
||||
pass
|
||||
|
||||
def twoArgument(blah, other):
|
||||
pass
|
||||
|
||||
class TestCases(unittest.TestCase):
|
||||
def test01(self):
|
||||
robustApply(noArgument)
|
||||
|
||||
def test02(self):
|
||||
self.assertRaises(TypeError, robustApply, noArgument, "this")
|
||||
|
||||
def test03(self):
|
||||
self.assertRaises(TypeError, robustApply, oneArgument)
|
||||
|
||||
def test04(self):
|
||||
"""Raise error on duplication of a particular argument"""
|
||||
self.assertRaises(TypeError, robustApply, oneArgument, "this", blah = "that")
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
from django.dispatch.saferef import *
|
||||
|
||||
import unittest
|
||||
|
||||
class Test1(object):
|
||||
def x(self):
|
||||
pass
|
||||
|
||||
def test2(obj):
|
||||
pass
|
||||
|
||||
class Test2(object):
|
||||
def __call__(self, obj):
|
||||
pass
|
||||
|
||||
class Tester(unittest.TestCase):
|
||||
def setUp(self):
|
||||
ts = []
|
||||
|
@ -28,17 +32,21 @@ class Tester (unittest.TestCase):
|
|||
self.ts = ts
|
||||
self.ss = ss
|
||||
self.closureCount = 0
|
||||
|
||||
def tearDown(self):
|
||||
del self.ts
|
||||
del self.ss
|
||||
|
||||
def testIn(self):
|
||||
"""Test the "in" operator for safe references (cmp)"""
|
||||
for t in self.ts[:50]:
|
||||
assert safeRef(t.x) in self.ss
|
||||
self.assert_(safeRef(t.x) in self.ss)
|
||||
|
||||
def testValid(self):
|
||||
"""Test that the references are valid (return instance methods)"""
|
||||
for s in self.ss:
|
||||
assert s()
|
||||
self.assert_(s())
|
||||
|
||||
def testShortCircuit (self):
|
||||
"""Test that creation short-circuits to reuse existing references"""
|
||||
sd = {}
|
||||
|
@ -46,9 +54,10 @@ class Tester (unittest.TestCase):
|
|||
sd[s] = 1
|
||||
for t in self.ts:
|
||||
if hasattr(t, 'x'):
|
||||
assert sd.has_key( safeRef(t.x))
|
||||
self.assert_(sd.has_key(safeRef(t.x)))
|
||||
else:
|
||||
assert sd.has_key( safeRef(t))
|
||||
self.assert_(sd.has_key(safeRef(t)))
|
||||
|
||||
def testRepresentation (self):
|
||||
"""Test that the reference object's representation works
|
||||
|
||||
|
@ -57,14 +66,6 @@ class Tester (unittest.TestCase):
|
|||
"""
|
||||
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):
|
||||
"""Dumb utility mechanism to increment deletion counter"""
|
||||
self.closureCount +=1
|
||||
|
|
Loading…
Reference in New Issue