PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 34ms app.codeStats 0ms

/tests/regressiontests/dispatch/tests/test_saferef.py

https://code.google.com/p/mango-py/
Python | 79 lines | 72 code | 7 blank | 0 comment | 4 complexity | 938aa4314bfbdf83cd38a823cfcb4ecf MD5 | raw file
 1from django.dispatch.saferef import *
 2
 3from django.utils import unittest
 4
 5class Test1(object):
 6    def x(self):
 7        pass
 8
 9def test2(obj):
10    pass
11
12class Test2(object):
13    def __call__(self, obj):
14        pass
15
16class 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.assertTrue(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.assertTrue(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.assertTrue(sd.has_key(safeRef(t.x)))
58                self.assertTrue(safeRef(t.x) in sd)
59            else:
60                self.assertTrue(sd.has_key(safeRef(t)))
61                self.assertTrue(safeRef(t) in sd)
62    
63    def testRepresentation (self):
64        """Test that the reference object's representation works
65        
66        XXX Doesn't currently check the results, just that no error
67            is raised
68        """
69        repr(self.ss[-1])
70        
71    def _closure(self, ref):
72        """Dumb utility mechanism to increment deletion counter"""
73        self.closureCount +=1
74
75def getSuite():
76    return unittest.makeSuite(Tester,'test')
77
78if __name__ == "__main__":
79    unittest.main()