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
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:
def get_scalar(rows: list[Any]) -> Any:
if len(rows) == 1:
for key in rows[0].__mapper__.c.keys():
if getattr(rows[0], key) is not None:
return getattr(rows[0], key)
elif len(rows) > 1:
raise MultipleResultsFound("Multiple rows were found for scalar()")
else:
return None
mock_session = UnifiedAlchemyMagicMock(
data=[...]
)
mock_session.boundary['scalar']= lambda x: get_scalar(x)
mock_session.scalar = AlchemyMagicMock(side_effect=partial(mock_session._get_data, _mock_name='scalar'))
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?
The text was updated successfully, but these errors were encountered:
Discussed in #153
Originally posted by jonyscathe October 6, 2021
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?
The text was updated successfully, but these errors were encountered: