~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to tests/regressiontests/dispatch/tests/test_saferef.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
                self.assert_(safeRef(t.x) in sd)
59
 
            else:
60
 
                self.assert_(sd.has_key(safeRef(t)))
61
 
                self.assert_(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
 
 
75
 
def getSuite():
76
 
    return unittest.makeSuite(Tester,'test')
77
 
 
78
 
if __name__ == "__main__":
79
 
    unittest.main()