You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
After @seano314's update to support sqlalchemy 2.0 I have had success migrating a lot of my code and test code over to 2.0 style form following the migration guide.
The one thing I have struggled with is with the use of scalar()
According to the migration guide, simple queries for a single value should be changed from something like:
session.query(User).count()
to
session.scalar(
select(func.count()).
select_from(User)
)
# or
session.scalar(
select(func.count(User.id))
)
Currently I don't believe that that new style method of using count() can be mocked using mock-alchemy as it currently stands.
And I'm 100% not sure that this is an easy fix with scalar() in 2.0 now being able to be the start and end of a chain rather than just the end of a chain.
If anyone has any good ideas on how to fix this I am happy to make a PR. But this isn't quite as simple
To Reproduce
Try mock data for something like:
session.scalar(
select(func.count(User.id))
)
Expected behavior
For it to be possible to make a UnifiedAlchemyMagicMock with data set such that session.scalar() calls can be mocked correctly.
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
OS: [Ubuntu]
Version [0.2.6]
Additional context
Another aspect of the new ORM that isn't yet supported is scalar_one though I believe that one is rather easy to fix as it is essentially the same as one.
And the other one is using scalars() as the beginning of a chain as in:
session.scalars(
select(User)
).all()
However it is easy enough to get the same thing with:
session.execute(
select(User)
).scalars().all()
So that one isn't super urgent.
As a workaround for now, it is possible to run scalar as something like:
Describe the bug
After @seano314's update to support sqlalchemy 2.0 I have had success migrating a lot of my code and test code over to 2.0 style form following the migration guide.
The one thing I have struggled with is with the use of scalar()
According to the migration guide, simple queries for a single value should be changed from something like:
to
Currently I don't believe that that new style method of using count() can be mocked using mock-alchemy as it currently stands.
And I'm 100% not sure that this is an easy fix with scalar() in 2.0 now being able to be the start and end of a chain rather than just the end of a chain.
If anyone has any good ideas on how to fix this I am happy to make a PR. But this isn't quite as simple
To Reproduce
Try mock data for something like:
Expected behavior
For it to be possible to make a UnifiedAlchemyMagicMock with data set such that session.scalar() calls can be mocked correctly.
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Additional context
Another aspect of the new ORM that isn't yet supported is
scalar_one
though I believe that one is rather easy to fix as it is essentially the same asone
.And the other one is using scalars() as the beginning of a chain as in:
However it is easy enough to get the same thing with:
So that one isn't super urgent.
As a workaround for now, it is possible to run scalar as something like:
But given that is not the suggested method in the sqlalchemy docs I'd like to be able to use session.scalar() directly
The text was updated successfully, but these errors were encountered: