alchemy-mock icon indicating copy to clipboard operation
alchemy-mock copied to clipboard

#Issue filter_by does not affect the output of the query

Open tweigel-dev opened this issue 3 years ago • 1 comments

Hay, I found a bug. The filter_by does not affect the output of the query. It is no issue if not using the mock. https://github.com/sqlalchemy/sqlalchemy/issues/6873

from sqlalchemy.orm import registry, Session
from sqlalchemy import Column, Integer
Base = registry().generate_base()
class Hardware(Base):
    """
    parent Hardwaretable
    """
    __tablename__   = 'hardware'
    hardware_id     = Column(Integer, primary_key=True)
session = UnifiedAlchemyMagicMock()
session.add(Hardware(hardware_id=0))
session.add(Hardware(hardware_id=1))
session.add(Hardware(hardware_id=2))
hardware = session.query(Hardware).filter_by(hardware_id=1).all()
print(hardware)

prints all 3 hardware objects and behaves like

hardware = session.query(Hardware).all()

tweigel-dev avatar Aug 10 '21 13:08 tweigel-dev

the library does not actually filter anything. as mentioned in the readme it does very limited logic to mimick actual database. it only does that for .get() which gets the model instance by its PK. for filter you would have to stub the data:

>>> session = UnifiedAlchemyMagicMock(data=[
...     (
...         [mock.call.query(Model),
...          mock.call.filter_by(foo='bar')],
...         [Model(foo=5, bar=11)]
...     ),
... ])
>>> session.query(Model).filter_by(foo='bar').all()

miki725 avatar Aug 10 '21 13:08 miki725