PageRenderTime 212ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/utils/simplelazyobject.py

https://code.google.com/p/mango-py/
Python | 77 lines | 42 code | 18 blank | 17 comment | 0 complexity | 8d4a708f23fe662af3b2346c41ad6252 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import unittest
  2. import django.utils.copycompat as copy
  3. from django.utils.functional import SimpleLazyObject
  4. class _ComplexObject(object):
  5. def __init__(self, name):
  6. self.name = name
  7. def __eq__(self, other):
  8. return self.name == other.name
  9. def __hash__(self):
  10. return hash(self.name)
  11. def __str__(self):
  12. return "I am _ComplexObject(%r)" % self.name
  13. def __unicode__(self):
  14. return unicode(self.name)
  15. def __repr__(self):
  16. return "_ComplexObject(%r)" % self.name
  17. complex_object = lambda: _ComplexObject("joe")
  18. class TestUtilsSimpleLazyObject(unittest.TestCase):
  19. """
  20. Tests for SimpleLazyObject
  21. """
  22. # Note that concrete use cases for SimpleLazyObject are also found in the
  23. # auth context processor tests (unless the implementation of that function
  24. # is changed).
  25. def test_equality(self):
  26. self.assertEqual(complex_object(), SimpleLazyObject(complex_object))
  27. self.assertEqual(SimpleLazyObject(complex_object), complex_object())
  28. def test_hash(self):
  29. # hash() equality would not be true for many objects, but it should be
  30. # for _ComplexObject
  31. self.assertEqual(hash(complex_object()),
  32. hash(SimpleLazyObject(complex_object)))
  33. def test_repr(self):
  34. # For debugging, it will really confuse things if there is no clue that
  35. # SimpleLazyObject is actually a proxy object. So we don't
  36. # proxy __repr__
  37. self.assertTrue("SimpleLazyObject" in repr(SimpleLazyObject(complex_object)))
  38. def test_str(self):
  39. self.assertEqual("I am _ComplexObject('joe')", str(SimpleLazyObject(complex_object)))
  40. def test_unicode(self):
  41. self.assertEqual(u"joe", unicode(SimpleLazyObject(complex_object)))
  42. def test_class(self):
  43. # This is important for classes that use __class__ in things like
  44. # equality tests.
  45. self.assertEqual(_ComplexObject, SimpleLazyObject(complex_object).__class__)
  46. def test_deepcopy(self):
  47. # Check that we *can* do deep copy, and that it returns the right
  48. # objects.
  49. # First, for an unevaluated SimpleLazyObject
  50. s = SimpleLazyObject(complex_object)
  51. assert s._wrapped is None
  52. s2 = copy.deepcopy(s)
  53. assert s._wrapped is None # something has gone wrong is s is evaluated
  54. self.assertEqual(s2, complex_object())
  55. # Second, for an evaluated SimpleLazyObject
  56. name = s.name # evaluate
  57. assert s._wrapped is not None
  58. s3 = copy.deepcopy(s)
  59. self.assertEqual(s3, complex_object())