rajivsarvepalli / mock-alchemy

SQLAlchemy mock helpers.
https://mock-alchemy.rtfd.io
MIT License
78 stars 10 forks source link

Mock query with two DataModels #343

Open nadwir opened 1 year ago

nadwir commented 1 year ago

Describe the bug When mocking the query

session.query(Ist, Soll).select_from(Ist) \
    .join(Soll, Soll.knIdent == Ist.knIdent) \
    .filter(Ist.strasse=='1', or_(Ist.eintrageDate != None, Ist.austrageDate != None)) \
    .all()

only an empty List is returned.

Python-Code to Reproduce

import datetime
from unittest import mock
from mock_alchemy.mocking import UnifiedAlchemyMagicMock
from sqlalchemy import or_

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Ist(db.Model):
    id = db.Column('id', db.Integer, primary_key=True)
    strasse = db.Column('strasse', db.String)
    knIdent = db.Column('knIdent', db.Integer)
    eintrageDate = db.Column('eintrageDate', db.DateTime)
    austrageDate = db.Column('austrageDate', db.DateTime)

class Soll(db.Model):
    id = db.Column('id', db.Integer, primary_key=True)
    knIdent = db.Column('knIdent', db.Integer)
    querschnitt = db.Column('querschnitt', db.Integer)

mocked_session = UnifiedAlchemyMagicMock(data=[
(
    [mock.call.query(Ist, Soll),
     mock.call.select_from(Ist),
     mock.call.join(Soll, Soll.knIdent == Ist.knIdent),
     mock.call.filter(Ist.strasse == '1', or_(Ist.eintrageDate != None, Ist.austrageDate != None)),
    ],
    [(Ist(id=1, strasse='1', knIdent=11,
          eintrageDate=datetime.datetime(2023, 4, 17, 6, 1,tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))),
          austrageDate=None),
      Soll(id=7, knIdent=11, querschnitt=10)
      )
    ]
)
])

print(mocked_session.query(Ist, Soll).select_from(Ist) \
    .join(Soll, Soll.knIdent == Ist.knIdent) \
    .filter(Ist.strasse=='1', or_(Ist.eintrageDate != None, Ist.austrageDate != None)) \
    .all())

Expected behavior I would expect, that the result of the mock is the tuple-element which was given to UnifiedAlchemyMagicMock.

Desktop

Additional context Result of pip freeze: click==8.1.3 Flask==2.2.3 Flask-SQLAlchemy==3.0.3 greenlet==2.0.2 importlib-metadata==6.4.1 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.2 mock-alchemy==0.2.6 SQLAlchemy==2.0.9 typing_extensions==4.5.0 Werkzeug==2.2.3 zipp==3.15.0