9

I'm writing some unittests for some code that uses SQLAlchemy. I want to test filter calls, but it seems that SQLAlchemy BinaryExpression objects created with the same arguments don't compare equal:

AssertionError: Expected call: filter(<sqlalchemy.sql.elements.BinaryExpression object at 0x1037607d0>)
Actual call: filter(<sqlalchemy.sql.elements.BinaryExpression object at 0x1037590d0>)

I suppose I can cast them both to strings and compare those, but that seems hacky, and I'd really rather not be forced to try to debug string comparisons if I don't have to. Are there any better/more-structured ways to compare BinaryExpressions in unittests?

swizzard
  • 1,037
  • 1
  • 12
  • 28

1 Answers1

12

you can use compare method

>>> binary_expression1 = Table.id==1
>>> binary_expression1
<sqlalchemy.sql.elements.BinaryExpression object at 0x7ff0b7c6f7d0>
>>> binary_expression2 = Table.id==1
>>> binary_expression2
<sqlalchemy.sql.elements.BinaryExpression object at 0x7ff0b7c4a490>
>>> binary_expression1.compare(binary_expression2)
True
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
r-m-n
  • 14,192
  • 4
  • 69
  • 68