test_monkey.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.monkey}.
  5. """
  6. from __future__ import division, absolute_import
  7. from twisted.trial import unittest
  8. from twisted.python.monkey import MonkeyPatcher
  9. class TestObj:
  10. def __init__(self):
  11. self.foo = 'foo value'
  12. self.bar = 'bar value'
  13. self.baz = 'baz value'
  14. class MonkeyPatcherTests(unittest.SynchronousTestCase):
  15. """
  16. Tests for L{MonkeyPatcher} monkey-patching class.
  17. """
  18. def setUp(self):
  19. self.testObject = TestObj()
  20. self.originalObject = TestObj()
  21. self.monkeyPatcher = MonkeyPatcher()
  22. def test_empty(self):
  23. """
  24. A monkey patcher without patches shouldn't change a thing.
  25. """
  26. self.monkeyPatcher.patch()
  27. # We can't assert that all state is unchanged, but at least we can
  28. # check our test object.
  29. self.assertEqual(self.originalObject.foo, self.testObject.foo)
  30. self.assertEqual(self.originalObject.bar, self.testObject.bar)
  31. self.assertEqual(self.originalObject.baz, self.testObject.baz)
  32. def test_constructWithPatches(self):
  33. """
  34. Constructing a L{MonkeyPatcher} with patches should add all of the
  35. given patches to the patch list.
  36. """
  37. patcher = MonkeyPatcher((self.testObject, 'foo', 'haha'),
  38. (self.testObject, 'bar', 'hehe'))
  39. patcher.patch()
  40. self.assertEqual('haha', self.testObject.foo)
  41. self.assertEqual('hehe', self.testObject.bar)
  42. self.assertEqual(self.originalObject.baz, self.testObject.baz)
  43. def test_patchExisting(self):
  44. """
  45. Patching an attribute that exists sets it to the value defined in the
  46. patch.
  47. """
  48. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'haha')
  49. self.monkeyPatcher.patch()
  50. self.assertEqual(self.testObject.foo, 'haha')
  51. def test_patchNonExisting(self):
  52. """
  53. Patching a non-existing attribute fails with an C{AttributeError}.
  54. """
  55. self.monkeyPatcher.addPatch(self.testObject, 'nowhere',
  56. 'blow up please')
  57. self.assertRaises(AttributeError, self.monkeyPatcher.patch)
  58. def test_patchAlreadyPatched(self):
  59. """
  60. Adding a patch for an object and attribute that already have a patch
  61. overrides the existing patch.
  62. """
  63. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'blah')
  64. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'BLAH')
  65. self.monkeyPatcher.patch()
  66. self.assertEqual(self.testObject.foo, 'BLAH')
  67. self.monkeyPatcher.restore()
  68. self.assertEqual(self.testObject.foo, self.originalObject.foo)
  69. def test_restoreTwiceIsANoOp(self):
  70. """
  71. Restoring an already-restored monkey patch is a no-op.
  72. """
  73. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'blah')
  74. self.monkeyPatcher.patch()
  75. self.monkeyPatcher.restore()
  76. self.assertEqual(self.testObject.foo, self.originalObject.foo)
  77. self.monkeyPatcher.restore()
  78. self.assertEqual(self.testObject.foo, self.originalObject.foo)
  79. def test_runWithPatchesDecoration(self):
  80. """
  81. runWithPatches should run the given callable, passing in all arguments
  82. and keyword arguments, and return the return value of the callable.
  83. """
  84. log = []
  85. def f(a, b, c=None):
  86. log.append((a, b, c))
  87. return 'foo'
  88. result = self.monkeyPatcher.runWithPatches(f, 1, 2, c=10)
  89. self.assertEqual('foo', result)
  90. self.assertEqual([(1, 2, 10)], log)
  91. def test_repeatedRunWithPatches(self):
  92. """
  93. We should be able to call the same function with runWithPatches more
  94. than once. All patches should apply for each call.
  95. """
  96. def f():
  97. return (self.testObject.foo, self.testObject.bar,
  98. self.testObject.baz)
  99. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'haha')
  100. result = self.monkeyPatcher.runWithPatches(f)
  101. self.assertEqual(
  102. ('haha', self.originalObject.bar, self.originalObject.baz), result)
  103. result = self.monkeyPatcher.runWithPatches(f)
  104. self.assertEqual(
  105. ('haha', self.originalObject.bar, self.originalObject.baz),
  106. result)
  107. def test_runWithPatchesRestores(self):
  108. """
  109. C{runWithPatches} should restore the original values after the function
  110. has executed.
  111. """
  112. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'haha')
  113. self.assertEqual(self.originalObject.foo, self.testObject.foo)
  114. self.monkeyPatcher.runWithPatches(lambda: None)
  115. self.assertEqual(self.originalObject.foo, self.testObject.foo)
  116. def test_runWithPatchesRestoresOnException(self):
  117. """
  118. Test runWithPatches restores the original values even when the function
  119. raises an exception.
  120. """
  121. def _():
  122. self.assertEqual(self.testObject.foo, 'haha')
  123. self.assertEqual(self.testObject.bar, 'blahblah')
  124. raise RuntimeError("Something went wrong!")
  125. self.monkeyPatcher.addPatch(self.testObject, 'foo', 'haha')
  126. self.monkeyPatcher.addPatch(self.testObject, 'bar', 'blahblah')
  127. self.assertRaises(RuntimeError, self.monkeyPatcher.runWithPatches, _)
  128. self.assertEqual(self.testObject.foo, self.originalObject.foo)
  129. self.assertEqual(self.testObject.bar, self.originalObject.bar)