thirdparty/google_appengine/lib/django/tests/regressiontests/dispatch/tests/test_saferef.py
changeset 2866 a04b1e4126c4
parent 2864 2e0b0af889be
child 2868 9f7f269383f7
equal deleted inserted replaced
2864:2e0b0af889be 2866:a04b1e4126c4
     1 from django.dispatch.saferef import *
       
     2 
       
     3 import unittest
       
     4 
       
     5 class Test1(object):
       
     6     def x(self):
       
     7         pass
       
     8 
       
     9 def test2(obj):
       
    10     pass
       
    11 
       
    12 class Test2(object):
       
    13     def __call__(self, obj):
       
    14         pass
       
    15 
       
    16 class Tester(unittest.TestCase):
       
    17     def setUp(self):
       
    18         ts = []
       
    19         ss = []
       
    20         for x in xrange(5000):
       
    21             t = Test1()
       
    22             ts.append(t)
       
    23             s = safeRef(t.x, self._closure)
       
    24             ss.append(s)
       
    25         ts.append(test2)
       
    26         ss.append(safeRef(test2, self._closure))
       
    27         for x in xrange(30):
       
    28             t = Test2()
       
    29             ts.append(t)
       
    30             s = safeRef(t, self._closure)
       
    31             ss.append(s)
       
    32         self.ts = ts
       
    33         self.ss = ss
       
    34         self.closureCount = 0
       
    35     
       
    36     def tearDown(self):
       
    37         del self.ts
       
    38         del self.ss
       
    39     
       
    40     def testIn(self):
       
    41         """Test the "in" operator for safe references (cmp)"""
       
    42         for t in self.ts[:50]:
       
    43             self.assert_(safeRef(t.x) in self.ss)
       
    44     
       
    45     def testValid(self):
       
    46         """Test that the references are valid (return instance methods)"""
       
    47         for s in self.ss:
       
    48             self.assert_(s())
       
    49     
       
    50     def testShortCircuit (self):
       
    51         """Test that creation short-circuits to reuse existing references"""
       
    52         sd = {}
       
    53         for s in self.ss:
       
    54             sd[s] = 1
       
    55         for t in self.ts:
       
    56             if hasattr(t, 'x'):
       
    57                 self.assert_(sd.has_key(safeRef(t.x)))
       
    58             else:
       
    59                 self.assert_(sd.has_key(safeRef(t)))
       
    60     
       
    61     def testRepresentation (self):
       
    62         """Test that the reference object's representation works
       
    63         
       
    64         XXX Doesn't currently check the results, just that no error
       
    65             is raised
       
    66         """
       
    67         repr(self.ss[-1])
       
    68         
       
    69     def _closure(self, ref):
       
    70         """Dumb utility mechanism to increment deletion counter"""
       
    71         self.closureCount +=1
       
    72 
       
    73 def getSuite():
       
    74     return unittest.makeSuite(Tester,'test')
       
    75 
       
    76 if __name__ == "__main__":
       
    77     unittest.main()