Skip to content

Commit 04168b3

Browse files
authored
feat: add ability to change mock class and sync_mock function helper
1 parent a52f3dd commit 04168b3

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

aioddd/testing.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import Any, Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Type, Union
33
from unittest.mock import MagicMock, Mock, patch
44

55
if sys.version_info.major == 3 and sys.version_info.minor >= 8:
@@ -25,20 +25,42 @@ def sanitize_objects(source: SanitizeObject, affected: SanitizeObject) -> Saniti
2525
return affected
2626

2727

28-
def mock(target: Union[str, object], attributes: Optional[List[str]] = None) -> Mock:
29-
target_async_mock = AsyncMock()
28+
def mock(
29+
target: Union[str, object],
30+
attributes: Optional[List[str]] = None,
31+
target_mock_type: Type[Mock] = AsyncMock,
32+
attribute_mock_type: Type[Mock] = AsyncMock,
33+
) -> Mock:
34+
target_async_mock = target_mock_type()
3035
if not attributes:
3136
patch(
3237
target=f'{target.__module__}.{target.__name__}' if isinstance(target, object) else target, # type: ignore
3338
side_effect=target_async_mock,
3439
)
35-
return target_async_mock # type: ignore
40+
return target_async_mock
3641
for attribute in attributes:
37-
attribute_async_mock = AsyncMock()
42+
attribute_async_mock = attribute_mock_type()
3843
patch.object(
3944
target=target,
4045
attribute=attribute,
4146
side_effect=attribute_async_mock,
4247
)
4348
target_async_mock[attribute] = attribute_async_mock
44-
return target_async_mock # type: ignore
49+
return target_async_mock
50+
51+
52+
async_mock = mock
53+
54+
55+
def sync_mock(
56+
target: Union[str, object],
57+
attributes: Optional[List[str]] = None,
58+
target_mock_type: Type[Mock] = MagicMock,
59+
attribute_mock_type: Type[Mock] = MagicMock,
60+
) -> Mock:
61+
return mock(
62+
target=target,
63+
attributes=attributes,
64+
target_mock_type=target_mock_type,
65+
attribute_mock_type=attribute_mock_type,
66+
)

0 commit comments

Comments
 (0)