We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Describe the bug Hi, I start to apply async sqlalchemy to my project and got stuck as mocking an AsyncSession To Reproduce CRUD function:
async def get_record_by_id(db: AsyncSession, user_id: int) -> Optional[sql_models.User]: result = await db.execute(select(sql_models.User).filter(sql_models.User.id == user_id)) result = result.scalars().first() return result
Unit test function:
@pytest.mark.asyncio async def test_get_investment_record_by_id(): mock_result = AlchemyMagicMock() mock_result.scalars.return_value.first.return_value = user_obj mock_db = AlchemyMagicMock() mock_db.execute.return_value = mock_result user_id = 1234 await get__record_by_id(mock_db, user_id) mock_db.execute.assert_called_with(select(sql_models.User).filter(sql_models.User.id == user_id))
Outcome:
E TypeError: object AlchemyMagicMock can't be used in 'await' expression
Expected behavior
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered:
I would also find this feature useful 👍
Sorry, something went wrong.
I also would like this
Any updates on this?
hi, I share an alternative way to mock AsyncSession
from unittest.mock import AsyncMock from unittest.mock import MagicMock @pytest.mark.asyncio async def test_get_investment_record_by_id(): mock_result = MagicMock() mock_result.scalars.return_value.first.return_value = user_obj mock_db = AsyncMock(return_value=asyncio.Future()) mock_db.execute.return_value = mock_result user_id = 1234 await get__record_by_id(mock_db, user_id) mock_db.execute.assert_called_with(select(sql_models.User).filter(sql_models.User.id == user_id))
No branches or pull requests
Describe the bug
Hi, I start to apply async sqlalchemy to my project and got stuck as mocking an AsyncSession
To Reproduce
CRUD function:
Unit test function:
Outcome:
Expected behavior
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: