PageRenderTime 37ms CodeModel.GetById 20ms RepoModel.GetById 1ms 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 | 2 complexity | 938aa4314bfbdf83cd38a823cfcb4ecf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.dispatch.saferef import *
  2. from django.utils import unittest
  3. class Test1(object):
  4. def x(self):
  5. pass
  6. def test2(obj):
  7. pass
  8. class Test2(object):
  9. def __call__(self, obj):
  10. pass
  11. class Tester(unittest.TestCase):
  12. def setUp(self):
  13. ts = []
  14. ss = []
  15. for x in xrange(5000):
  16. t = Test1()
  17. ts.append(t)
  18. s = safeRef(t.x, self._closure)
  19. ss.append(s)
  20. ts.append(test2)
  21. ss.append(safeRef(test2, self._closure))
  22. for x in xrange(30):
  23. t = Test2()
  24. ts.append(t)
  25. s = safeRef(t, self._closure)
  26. ss.append(s)
  27. self.ts = ts
  28. self.ss = ss
  29. self.closureCount = 0
  30. def tearDown(self):
  31. del self.ts
  32. del self.ss
  33. def testIn(self):
  34. """Test the "in" operator for safe references (cmp)"""
  35. for t in self.ts[:50]:
  36. self.assertTrue(safeRef(t.x) in self.ss)
  37. def testValid(self):
  38. """Test that the references are valid (return instance methods)"""
  39. for s in self.ss:
  40. self.assertTrue(s())
  41. def testShortCircuit (self):
  42. """Test that creation short-circuits to reuse existing references"""
  43. sd = {}
  44. for s in self.ss:
  45. sd[s] = 1
  46. for t in self.ts:
  47. if hasattr(t, 'x'):
  48. self.assertTrue(sd.has_key(safeRef(t.x)))
  49. self.assertTrue(safeRef(t.x) in sd)
  50. else:
  51. self.assertTrue(sd.has_key(safeRef(t)))
  52. self.assertTrue(safeRef(t) in sd)
  53. def testRepresentation (self):
  54. """Test that the reference object's representation works
  55. XXX Doesn't currently check the results, just that no error
  56. is raised
  57. """
  58. repr(self.ss[-1])
  59. def _closure(self, ref):
  60. """Dumb utility mechanism to increment deletion counter"""
  61. self.closureCount +=1
  62. def getSuite():
  63. return unittest.makeSuite(Tester,'test')
  64. if __name__ == "__main__":
  65. unittest.main()