aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_unittest/testmock/testhelpers.py
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2024-09-27 09:48:31 +0300
committerGitHub <noreply@github.com>2024-09-27 09:48:31 +0300
commit3a0e7f57628466aedcaaf6c5ff7c8224f5155a2c (patch)
tree9b1cefec532fff95a3296bb09645beefef4a8647 /Lib/test/test_unittest/testmock/testhelpers.py
parent08e1bbe4a329e5961716f030c6ccfe92c736bf28 (diff)
downloadcpython-3a0e7f57628466aedcaaf6c5ff7c8224f5155a2c.tar.gz
cpython-3a0e7f57628466aedcaaf6c5ff7c8224f5155a2c.zip
gh-124176: Add special support for dataclasses to `create_autospec` (#124429)
Diffstat (limited to 'Lib/test/test_unittest/testmock/testhelpers.py')
-rw-r--r--Lib/test/test_unittest/testmock/testhelpers.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/Lib/test/test_unittest/testmock/testhelpers.py b/Lib/test/test_unittest/testmock/testhelpers.py
index c9c20f008ca..f260769eb8c 100644
--- a/Lib/test/test_unittest/testmock/testhelpers.py
+++ b/Lib/test/test_unittest/testmock/testhelpers.py
@@ -8,8 +8,10 @@ from unittest.mock import (
Mock, ANY, _CallList, patch, PropertyMock, _callable
)
+from dataclasses import dataclass, field, InitVar
from datetime import datetime
from functools import partial
+from typing import ClassVar
class SomeClass(object):
def one(self, a, b): pass
@@ -1034,6 +1036,76 @@ class SpecSignatureTest(unittest.TestCase):
self.assertEqual(mock.mock_calls, [])
self.assertEqual(rv.mock_calls, [])
+ def test_dataclass_post_init(self):
+ @dataclass
+ class WithPostInit:
+ a: int = field(init=False)
+ b: int = field(init=False)
+ def __post_init__(self):
+ self.a = 1
+ self.b = 2
+
+ for mock in [
+ create_autospec(WithPostInit, instance=True),
+ create_autospec(WithPostInit()),
+ ]:
+ with self.subTest(mock=mock):
+ self.assertIsInstance(mock.a, int)
+ self.assertIsInstance(mock.b, int)
+
+ # Classes do not have these fields:
+ mock = create_autospec(WithPostInit)
+ msg = "Mock object has no attribute"
+ with self.assertRaisesRegex(AttributeError, msg):
+ mock.a
+ with self.assertRaisesRegex(AttributeError, msg):
+ mock.b
+
+ def test_dataclass_default(self):
+ @dataclass
+ class WithDefault:
+ a: int
+ b: int = 0
+
+ for mock in [
+ create_autospec(WithDefault, instance=True),
+ create_autospec(WithDefault(1)),
+ ]:
+ with self.subTest(mock=mock):
+ self.assertIsInstance(mock.a, int)
+ self.assertIsInstance(mock.b, int)
+
+ def test_dataclass_with_method(self):
+ @dataclass
+ class WithMethod:
+ a: int
+ def b(self) -> int:
+ return 1
+
+ for mock in [
+ create_autospec(WithMethod, instance=True),
+ create_autospec(WithMethod(1)),
+ ]:
+ with self.subTest(mock=mock):
+ self.assertIsInstance(mock.a, int)
+ mock.b.assert_not_called()
+
+ def test_dataclass_with_non_fields(self):
+ @dataclass
+ class WithNonFields:
+ a: ClassVar[int]
+ b: InitVar[int]
+
+ msg = "Mock object has no attribute"
+ for mock in [
+ create_autospec(WithNonFields, instance=True),
+ create_autospec(WithNonFields(1)),
+ ]:
+ with self.subTest(mock=mock):
+ with self.assertRaisesRegex(AttributeError, msg):
+ mock.a
+ with self.assertRaisesRegex(AttributeError, msg):
+ mock.b
class TestCallList(unittest.TestCase):