Skip to content

Commit eb75d77

Browse files
committed
PYTHON-5071 Fix AsyncPyMongoTestCase implementation
1 parent e455fa6 commit eb75d77

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

test/__init__.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import asyncio
1919
import gc
20+
import inspect
2021
import logging
2122
import multiprocessing
2223
import os
@@ -898,19 +899,34 @@ def tearDown(self):
898899
def addCleanup(self, func, /, *args, **kwargs):
899900
self.addCleanup(*(func, *args), **kwargs)
900901

901-
def run(self, result=None):
902-
if result is None:
903-
result = self.defaultTestResult()
904-
result.startTest(self)
902+
def _callSetUp(self):
903+
self._callAsync(self.setUp)
905904

906-
try:
907-
self.setUp()
908-
self.loop.run_until_complete(self.setUp())
909-
self.loop.run_until_complete(getattr(self, self._testMethodName)())
910-
self.loop.run_until_complete(self.tearDown())
911-
finally:
912-
self.tearDown()
913-
result.stopTest(self)
905+
def _callTestMethod(self, method):
906+
if self._callMaybeAsync(method) is not None:
907+
warnings.warn(
908+
f"It is deprecated to return a value that is not None from a "
909+
f"test case ({method})",
910+
DeprecationWarning,
911+
stacklevel=4,
912+
)
913+
914+
def _callTearDown(self):
915+
self._callAsync(self.tearDown)
916+
self.tearDown()
917+
918+
def _callCleanup(self, function, *args, **kwargs):
919+
self._callMaybeAsync(function, *args, **kwargs)
920+
921+
def _callAsync(self, func, /, *args, **kwargs):
922+
assert inspect.iscoroutinefunction(func), f"{func!r} is not an async function"
923+
return self.loop.run_until_complete(func(*args, **kwargs))
924+
925+
def _callMaybeAsync(self, func, /, *args, **kwargs):
926+
if inspect.iscoroutinefunction(func):
927+
return self.loop.run_until_complete(func(*args, **kwargs))
928+
else:
929+
return func(*args, **kwargs)
914930

915931
def assertEqualCommand(self, expected, actual, msg=None):
916932
self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg)

test/asynchronous/__init__.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import asyncio
1919
import gc
20+
import inspect
2021
import logging
2122
import multiprocessing
2223
import os
@@ -900,19 +901,34 @@ async def asyncTearDown(self):
900901
def addAsyncCleanup(self, func, /, *args, **kwargs):
901902
self.addCleanup(*(func, *args), **kwargs)
902903

903-
def run(self, result=None):
904-
if result is None:
905-
result = self.defaultTestResult()
906-
result.startTest(self)
904+
def _callSetUp(self):
905+
self._callAsync(self.asyncSetUp)
907906

908-
try:
909-
self.setUp()
910-
self.loop.run_until_complete(self.asyncSetUp())
911-
self.loop.run_until_complete(getattr(self, self._testMethodName)())
912-
self.loop.run_until_complete(self.asyncTearDown())
913-
finally:
914-
self.tearDown()
915-
result.stopTest(self)
907+
def _callTestMethod(self, method):
908+
if self._callMaybeAsync(method) is not None:
909+
warnings.warn(
910+
f"It is deprecated to return a value that is not None from a "
911+
f"test case ({method})",
912+
DeprecationWarning,
913+
stacklevel=4,
914+
)
915+
916+
def _callTearDown(self):
917+
self._callAsync(self.asyncTearDown)
918+
self.tearDown()
919+
920+
def _callCleanup(self, function, *args, **kwargs):
921+
self._callMaybeAsync(function, *args, **kwargs)
922+
923+
def _callAsync(self, func, /, *args, **kwargs):
924+
assert inspect.iscoroutinefunction(func), f"{func!r} is not an async function"
925+
return self.loop.run_until_complete(func(*args, **kwargs))
926+
927+
def _callMaybeAsync(self, func, /, *args, **kwargs):
928+
if inspect.iscoroutinefunction(func):
929+
return self.loop.run_until_complete(func(*args, **kwargs))
930+
else:
931+
return func(*args, **kwargs)
916932

917933
def assertEqualCommand(self, expected, actual, msg=None):
918934
self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg)

0 commit comments

Comments
 (0)