alchemy-mock
alchemy-mock copied to clipboard
#Issue filter_by does not affect the output of the query
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()
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()