bpo-21478: Record calls to parent when autospecced objects are used a… · python/cpython@22fd679

@@ -37,6 +37,9 @@ def cmeth(cls, a, b, c, d=None): pass

3737

def smeth(a, b, c, d=None): pass

3838393940+

def something(a): pass

41+42+4043

class MockTest(unittest.TestCase):

41444245

def test_all(self):

@@ -1808,6 +1811,26 @@ def test_attach_mock_return_value(self):

18081811

self.assertEqual(m.mock_calls, call().foo().call_list())

18091812181018131814+

def test_attach_mock_patch_autospec(self):

1815+

parent = Mock()

1816+1817+

with mock.patch(f'{__name__}.something', autospec=True) as mock_func:

1818+

self.assertEqual(mock_func.mock._extract_mock_name(), 'something')

1819+

parent.attach_mock(mock_func, 'child')

1820+

parent.child(1)

1821+

something(2)

1822+

mock_func(3)

1823+1824+

parent_calls = [call.child(1), call.child(2), call.child(3)]

1825+

child_calls = [call(1), call(2), call(3)]

1826+

self.assertEqual(parent.mock_calls, parent_calls)

1827+

self.assertEqual(parent.child.mock_calls, child_calls)

1828+

self.assertEqual(something.mock_calls, child_calls)

1829+

self.assertEqual(mock_func.mock_calls, child_calls)

1830+

self.assertIn('mock.child', repr(parent.child.mock))

1831+

self.assertEqual(mock_func.mock._extract_mock_name(), 'mock.child')

1832+1833+18111834

def test_attribute_deletion(self):

18121835

for mock in (Mock(), MagicMock(), NonCallableMagicMock(),

18131836

NonCallableMock()):

@@ -1891,6 +1914,20 @@ def foo(a, b): pass

1891191418921915

self.assertRaises(TypeError, mock.child, 1)

18931916

self.assertEqual(mock.mock_calls, [call.child(1, 2)])

1917+

self.assertIn('mock.child', repr(mock.child.mock))

1918+1919+

def test_parent_propagation_with_autospec_attach_mock(self):

1920+1921+

def foo(a, b): pass

1922+1923+

parent = Mock()

1924+

parent.attach_mock(create_autospec(foo, name='bar'), 'child')

1925+

parent.child(1, 2)

1926+1927+

self.assertRaises(TypeError, parent.child, 1)

1928+

self.assertEqual(parent.child.mock_calls, [call.child(1, 2)])

1929+

self.assertIn('mock.child', repr(parent.child.mock))

1930+1894193118951932

def test_isinstance_under_settrace(self):

18961933

# bpo-36593 : __class__ is not set for a class that has __class__