I have a function returning SQLAlchemy query object and I want to test this function that it builds correct query.
For example:
import sqlalchemy
metadata = sqlalchemy.MetaData()
users = sqlalchemy.Table(
"users",
metadata,
sqlalchemy.Column("email", sqlalchemy.String(255), nullable=False, unique=True),
sqlalchemy.Column("username", sqlalchemy.String(50), nullable=False, unique=True),
)
def select_first_users(n):
return users.select().limit(n)
def test_queries_are_equal(self):
expected_query = users.select().limit(10)
assert select_first_users(10) == expected_query # fails here
assert select_first_users(10).compare(expected_query) # fails here too
I have no idea on how to compare two queries for equality. == doesn't work here because as far as I can see this objects do not have __eq__ method defined, so it compares objects by address in memory and surely fails. The compare method also does is comparison.
The only solution I see is like:
assert str(q1.compile()) == str(q2.compile())
, but is is strange and contains placeholders instead of actual values.
So how can I compare two SQLAlchemy queries for equality?
I use Python 3.7.4, SQLAlchemy==1.3.10.
from How can I compare two SQLAlchemy queries if they are the same?
No comments:
Post a Comment