@@ -28,6 +28,14 @@ def a(self):
28
28
29
29
30
30
class AsyncPatchDecoratorTest (unittest .TestCase ):
31
+ def setUp (self ):
32
+ # Prevents altering the execution environment
33
+ self .old_policy = asyncio .events ._event_loop_policy
34
+
35
+ def tearDown (self ):
36
+ # Restore the original event loop policy.
37
+ asyncio .events ._event_loop_policy = self .old_policy
38
+
31
39
def test_is_coroutine_function_patch (self ):
32
40
@patch .object (AsyncClass , 'async_method' )
33
41
def test_async (mock_method ):
@@ -37,11 +45,15 @@ def test_async(mock_method):
37
45
def test_is_async_patch (self ):
38
46
@patch .object (AsyncClass , 'async_method' )
39
47
def test_async (mock_method ):
40
- self .assertTrue (inspect .isawaitable (mock_method ()))
48
+ m = mock_method ()
49
+ self .assertTrue (inspect .isawaitable (m ))
50
+ asyncio .run (m )
41
51
42
52
@patch (f'{ async_foo_name } .async_method' )
43
53
def test_no_parent_attribute (mock_method ):
44
- self .assertTrue (inspect .isawaitable (mock_method ()))
54
+ m = mock_method ()
55
+ self .assertTrue (inspect .isawaitable (m ))
56
+ asyncio .run (m )
45
57
46
58
test_async ()
47
59
test_no_parent_attribute ()
@@ -55,6 +67,13 @@ def test_async(mock_method):
55
67
56
68
57
69
class AsyncPatchCMTest (unittest .TestCase ):
70
+ def setUp (self ):
71
+ self .old_policy = asyncio .events ._event_loop_policy
72
+
73
+ def tearDown (self ):
74
+ # Restore the original event loop policy.
75
+ asyncio .events ._event_loop_policy = self .old_policy
76
+
58
77
def test_is_async_function_cm (self ):
59
78
def test_async ():
60
79
with patch .object (AsyncClass , 'async_method' ) as mock_method :
@@ -65,7 +84,9 @@ def test_async():
65
84
def test_is_async_cm (self ):
66
85
def test_async ():
67
86
with patch .object (AsyncClass , 'async_method' ) as mock_method :
68
- self .assertTrue (inspect .isawaitable (mock_method ()))
87
+ m = mock_method ()
88
+ self .assertTrue (inspect .isawaitable (m ))
89
+ asyncio .run (m )
69
90
70
91
test_async ()
71
92
@@ -78,6 +99,13 @@ def test_async():
78
99
79
100
80
101
class AsyncMockTest (unittest .TestCase ):
102
+ def setUp (self ):
103
+ self .old_policy = asyncio .events ._event_loop_policy
104
+
105
+ def tearDown (self ):
106
+ # Restore the original event loop policy.
107
+ asyncio .events ._event_loop_policy = self .old_policy
108
+
81
109
def test_iscoroutinefunction_default (self ):
82
110
mock = AsyncMock ()
83
111
self .assertTrue (asyncio .iscoroutinefunction (mock ))
@@ -90,7 +118,9 @@ async def foo(): pass
90
118
91
119
def test_isawaitable (self ):
92
120
mock = AsyncMock ()
93
- self .assertTrue (inspect .isawaitable (mock ()))
121
+ m = mock ()
122
+ self .assertTrue (inspect .isawaitable (m ))
123
+ asyncio .run (m )
94
124
self .assertIn ('assert_awaited' , dir (mock ))
95
125
96
126
def test_iscoroutinefunction_normal_function (self ):
@@ -120,35 +150,53 @@ def test_normal_method(mock_method):
120
150
121
151
122
152
class AsyncSpecTest (unittest .TestCase ):
153
+ def setUp (self ):
154
+ self .old_policy = asyncio .events ._event_loop_policy
155
+
156
+ def tearDown (self ):
157
+ # Restore the original event loop policy.
158
+ asyncio .events ._event_loop_policy = self .old_policy
123
159
def test_spec_as_async_positional_magicmock (self ):
124
160
mock = MagicMock (async_func )
125
161
self .assertIsInstance (mock , MagicMock )
126
- self .assertTrue (inspect .isawaitable (mock ()))
162
+ m = mock ()
163
+ self .assertTrue (inspect .isawaitable (m ))
164
+ asyncio .run (m )
127
165
128
166
def test_spec_as_async_kw_magicmock (self ):
129
167
mock = MagicMock (spec = async_func )
130
168
self .assertIsInstance (mock , MagicMock )
131
- self .assertTrue (inspect .isawaitable (mock ()))
169
+ m =<
10822
/span> mock ()
170
+ self .assertTrue (inspect .isawaitable (m ))
171
+ asyncio .run (m )
132
172
133
173
def test_spec_as_async_kw_AsyncMock (self ):
134
174
mock = AsyncMock (spec = async_func )
135
175
self .assertIsInstance (mock , AsyncMock )
136
- self .assertTrue (inspect .isawaitable (mock ()))
176
+ m = mock ()
177
+ self .assertTrue (inspect .isawaitable (m ))
178
+ asyncio .run (m )
137
179
138
180
def test_spec_as_async_positional_AsyncMock (self ):
139
181
mock = AsyncMock (async_func )
140
182
self .assertIsInstance (mock , AsyncMock )
141
- self .assertTrue (inspect .isawaitable (mock ()))
183
+ m = mock ()
184
+ self .assertTrue (inspect .isawaitable (m ))
185
+ asyncio .run (m )
142
186
143
187
def test_spec_as_normal_kw_AsyncMock (self ):
144
188
mock = AsyncMock (spec = normal_func )
145
189
self .assertIsInstance (mock , AsyncMock )
146
- self .assertTrue (inspect .isawaitable (mock ()))
190
+ m = mock ()
191
+ self .assertTrue (inspect .isawaitable (m ))
192
+ asyncio .run (m )
147
193
148
194
def test_spec_as_normal_positional_AsyncMock (self ):
149
195
mock = AsyncMock (normal_func )
150
196
self .assertIsInstance (mock , AsyncMock )
151
- self .assertTrue (inspect .isawaitable (mock ()))
197
+ m = mock ()
198
+ self .assertTrue (inspect .isawaitable (m ))
199
+ asyncio .run (m )
152
200
153
201
def test_spec_async_mock (self ):
154
202
@patch .object (AsyncClass , 'async_method' , spec = True )
0 commit comments