~ubuntu-branches/ubuntu/quantal/python-django/quantal

« 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
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

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()