Avinash Santhanagopalan
03/14/2024, 3:57 PMflow.with_options
? I have an async flow refresh_data_for_plugin
that I used to patch like this
patch("refresh_data_for_plugin", new_callable=AsyncMock)
But after adding refresh_data_for_plugin.with_options
I am not able to patch it in a similar way
patch("refresh_data_for_plugin.with_options", new_callable=AsyncMock)
I’m getting TypeError: 'coroutine' object is not callable
Any help appreciated!Dominic Tarro
03/14/2024, 5:21 PMTask.with_options
will return an awaitable. It is not an awaitable itself.Dominic Tarro
03/14/2024, 5:22 PMreturn_value
for with_options
that returns your AsyncMock
.Avinash Santhanagopalan
03/14/2024, 5:37 PMrefresh_data_for_plugin
first I wouldn’t be able to patch with_options
TypeError: An asyncio.Future, a coroutine or an awaitable is required
But I’m not too familiar with async pytesting so not sure how to proceedDominic Tarro
03/14/2024, 5:43 PMpatch("refresh_data_for_plugin", Mock(with_options=Mock(return_value=...)))
Let me know if that worksDominic Tarro
03/14/2024, 5:56 PMfrom unittest.mock import Mock, patch
from prefect import task
@task
def unmocked_func(*args, **kwargs):
print("I'm not mocked!")
def test_unmocked_func():
def mocked_func():
print("I'm mocked!")
with patch(
"test.unmocked_func",
Mock(
with_options=Mock(
return_value=mocked_func
)
)
) as mocked:
unmocked_func.with_options()()
Avinash Santhanagopalan
03/14/2024, 6:29 PMassert_has_calls
on unmocked_func
Dominic Tarro
03/14/2024, 6:31 PMasync_mock = AsyncMock()
with patch(..., ...return_value=async_mock...):
# stuff
async_mock.assert_has_calls()