Mocking scalar() #153
Replies: 5 comments 4 replies
-
|
Beta Was this translation helpful? Give feedback.
-
One question I had based on your |
Beta Was this translation helpful? Give feedback.
-
Hi Rajiv, Yes you are correct that it should just return the first value. And if it is something that is going to be added to the library the way you have done it is best. The only reason I was returning the first non null value was to be a bit lazy so that I could have a table:
Then have a unified alchemy magic mock:
Then by calling But of course the better way to do this is to have a function like what you have suggested in PR #155 and then in my UnifiedAlchemyMagicMock have a return data value with only SomeTable.val1 defined as this is then more representative of what SQLAlchemy actually does. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi Rajiv, I just realised I completely stuffed up on my solution for .scalar()
When querying on the full Item, scalar returns the whole first item if there is only 1 row or This makes the definition slightly tricky. Something like this seems to work. I'm not a huge fan of doing this why a try, except but it is simple and works.
|
Beta Was this translation helpful? Give feedback.
-
Hi,
We are using a lot of scalar() in our project: https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.scalar
Usually to get a single value as per SQLAlchemys example
session.query(Item.id, Item.name).scalar()
Is there currently any way to do this with mock-alchemy?
Currently the best option I seem to have seems to be after creating a mock_session to make a new boundary lambda for scalar() such as:
Is there some better way to be mocking scalar with the UnifiedAlchemyMagicMocks?
Or if there is the best way, could we get scalar() added to the standard boundary dict?
Beta Was this translation helpful? Give feedback.
All reactions