testsealable.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import unittest
  2. from unittest import mock
  3. class SampleObject:
  4. def method_sample1(self): pass
  5. def method_sample2(self): pass
  6. class TestSealable(unittest.TestCase):
  7. def test_attributes_return_more_mocks_by_default(self):
  8. m = mock.Mock()
  9. self.assertIsInstance(m.test, mock.Mock)
  10. self.assertIsInstance(m.test(), mock.Mock)
  11. self.assertIsInstance(m.test().test2(), mock.Mock)
  12. def test_new_attributes_cannot_be_accessed_on_seal(self):
  13. m = mock.Mock()
  14. mock.seal(m)
  15. with self.assertRaises(AttributeError):
  16. m.test
  17. with self.assertRaises(AttributeError):
  18. m()
  19. def test_new_attributes_cannot_be_set_on_seal(self):
  20. m = mock.Mock()
  21. mock.seal(m)
  22. with self.assertRaises(AttributeError):
  23. m.test = 1
  24. def test_existing_attributes_can_be_set_on_seal(self):
  25. m = mock.Mock()
  26. m.test.test2 = 1
  27. mock.seal(m)
  28. m.test.test2 = 2
  29. self.assertEqual(m.test.test2, 2)
  30. def test_new_attributes_cannot_be_set_on_child_of_seal(self):
  31. m = mock.Mock()
  32. m.test.test2 = 1
  33. mock.seal(m)
  34. with self.assertRaises(AttributeError):
  35. m.test.test3 = 1
  36. def test_existing_attributes_allowed_after_seal(self):
  37. m = mock.Mock()
  38. m.test.return_value = 3
  39. mock.seal(m)
  40. self.assertEqual(m.test(), 3)
  41. def test_initialized_attributes_allowed_after_seal(self):
  42. m = mock.Mock(test_value=1)
  43. mock.seal(m)
  44. self.assertEqual(m.test_value, 1)
  45. def test_call_on_sealed_mock_fails(self):
  46. m = mock.Mock()
  47. mock.seal(m)
  48. with self.assertRaises(AttributeError):
  49. m()
  50. def test_call_on_defined_sealed_mock_succeeds(self):
  51. m = mock.Mock(return_value=5)
  52. mock.seal(m)
  53. self.assertEqual(m(), 5)
  54. def test_seals_recurse_on_added_attributes(self):
  55. m = mock.Mock()
  56. m.test1.test2().test3 = 4
  57. mock.seal(m)
  58. self.assertEqual(m.test1.test2().test3, 4)
  59. with self.assertRaises(AttributeError):
  60. m.test1.test2().test4
  61. with self.assertRaises(AttributeError):
  62. m.test1.test3
  63. def test_seals_recurse_on_magic_methods(self):
  64. m = mock.MagicMock()
  65. m.test1.test2["a"].test3 = 4
  66. m.test1.test3[2:5].test3 = 4
  67. mock.seal(m)
  68. self.assertEqual(m.test1.test2["a"].test3, 4)
  69. self.assertEqual(m.test1.test2[2:5].test3, 4)
  70. with self.assertRaises(AttributeError):
  71. m.test1.test2["a"].test4
  72. with self.assertRaises(AttributeError):
  73. m.test1.test3[2:5].test4
  74. def test_seals_dont_recurse_on_manual_attributes(self):
  75. m = mock.Mock(name="root_mock")
  76. m.test1.test2 = mock.Mock(name="not_sealed")
  77. m.test1.test2.test3 = 4
  78. mock.seal(m)
  79. self.assertEqual(m.test1.test2.test3, 4)
  80. m.test1.test2.test4 # Does not raise
  81. m.test1.test2.test4 = 1 # Does not raise
  82. def test_integration_with_spec_att_definition(self):
  83. """You are not restricted when using mock with spec"""
  84. m = mock.Mock(SampleObject)
  85. m.attr_sample1 = 1
  86. m.attr_sample3 = 3
  87. mock.seal(m)
  88. self.assertEqual(m.attr_sample1, 1)
  89. self.assertEqual(m.attr_sample3, 3)
  90. with self.assertRaises(AttributeError):
  91. m.attr_sample2
  92. def test_integration_with_spec_method_definition(self):
  93. """You need to define the methods, even if they are in the spec"""
  94. m = mock.Mock(SampleObject)
  95. m.method_sample1.return_value = 1
  96. mock.seal(m)
  97. self.assertEqual(m.method_sample1(), 1)
  98. with self.assertRaises(AttributeError):
  99. m.method_sample2()
  100. def test_integration_with_spec_method_definition_respects_spec(self):
  101. """You cannot define methods out of the spec"""
  102. m = mock.Mock(SampleObject)
  103. with self.assertRaises(AttributeError):
  104. m.method_sample3.return_value = 3
  105. def test_sealed_exception_has_attribute_name(self):
  106. m = mock.Mock()
  107. mock.seal(m)
  108. with self.assertRaises(AttributeError) as cm:
  109. m.SECRETE_name
  110. self.assertIn("SECRETE_name", str(cm.exception))
  111. def test_attribute_chain_is_maintained(self):
  112. m = mock.Mock(name="mock_name")
  113. m.test1.test2.test3.test4
  114. mock.seal(m)
  115. with self.assertRaises(AttributeError) as cm:
  116. m.test1.test2.test3.test4.boom
  117. self.assertIn("mock_name.test1.test2.test3.test4.boom", str(cm.exception))
  118. def test_call_chain_is_maintained(self):
  119. m = mock.Mock()
  120. m.test1().test2.test3().test4
  121. mock.seal(m)
  122. with self.assertRaises(AttributeError) as cm:
  123. m.test1().test2.test3().test4()
  124. self.assertIn("mock.test1().test2.test3().test4", str(cm.exception))
  125. def test_seal_with_autospec(self):
  126. # https://bugs.python.org/issue45156
  127. class Foo:
  128. foo = 0
  129. def bar1(self):
  130. return 1
  131. def bar2(self):
  132. return 2
  133. class Baz:
  134. baz = 3
  135. def ban(self):
  136. return 4
  137. for spec_set in (True, False):
  138. with self.subTest(spec_set=spec_set):
  139. foo = mock.create_autospec(Foo, spec_set=spec_set)
  140. foo.bar1.return_value = 'a'
  141. foo.Baz.ban.return_value = 'b'
  142. mock.seal(foo)
  143. self.assertIsInstance(foo.foo, mock.NonCallableMagicMock)
  144. self.assertIsInstance(foo.bar1, mock.MagicMock)
  145. self.assertIsInstance(foo.bar2, mock.MagicMock)
  146. self.assertIsInstance(foo.Baz, mock.MagicMock)
  147. self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock)
  148. self.assertIsInstance(foo.Baz.ban, mock.MagicMock)
  149. self.assertEqual(foo.bar1(), 'a')
  150. foo.bar1.return_value = 'new_a'
  151. self.assertEqual(foo.bar1(), 'new_a')
  152. self.assertEqual(foo.Baz.ban(), 'b')
  153. foo.Baz.ban.return_value = 'new_b'
  154. self.assertEqual(foo.Baz.ban(), 'new_b')
  155. with self.assertRaises(TypeError):
  156. foo.foo()
  157. with self.assertRaises(AttributeError):
  158. foo.bar = 1
  159. with self.assertRaises(AttributeError):
  160. foo.bar2()
  161. foo.bar2.return_value = 'bar2'
  162. self.assertEqual(foo.bar2(), 'bar2')
  163. with self.assertRaises(AttributeError):
  164. foo.missing_attr
  165. with self.assertRaises(AttributeError):
  166. foo.missing_attr = 1
  167. with self.assertRaises(AttributeError):
  168. foo.missing_method()
  169. with self.assertRaises(TypeError):
  170. foo.Baz.baz()
  171. with self.assertRaises(AttributeError):
  172. foo.Baz.missing_attr
  173. with self.assertRaises(AttributeError):
  174. foo.Baz.missing_attr = 1
  175. with self.assertRaises(AttributeError):
  176. foo.Baz.missing_method()
  177. if __name__ == "__main__":
  178. unittest.main()