Re: [sqlalchemy] Queries comparison

2018-06-22 Thread Mike Bayer
Traverse each statement using visitors.traverse() is one way, however your
best approach is to get that string to be deterministic.   Where you
generate that where clause, make sure you use either ordered dictionaries /
sets or perform a sorted() on the records you send into and_(), ordering by
column name or something like that.As a last resort, in your tests,
include the two different versions of the string and compare to both, pass
the test if it matches one or the other.

On Fri, Jun 22, 2018, 6:49 AM Антонио Антуан  wrote:

> Hi there!
>
> I have a very complicated queries generator and I want to test it.
> Currently it looks like that:
>
> def test():
> query = generate_query()
> compiled = compile_query(query)
> assert compiled == 'SELECT * FROM table'
>
> Problems begin when I add some filters to query:
>
> def test():
> query = generate_query_with_filters()
> compiled = compile_query(query)
> assert compiled == 'SELECT * FROM table WHERE a = 1 and b = 2'
>
> This test can fail sometimes because condition in compiled query can be
> swaped: "SELECT * FROM table WHERE b = 2 and a = 1".
> Is there any way to compare Query objects?
>
> I tried to do this (does't work):
> a = Model.query
> b = Model.query
> # assert a == b, 'failed'
>
> a = Model.query.filter(Model.col == 1)
> b = Model.query.filter(Model.col == 1)
> # assert a.statement == b.statement
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Queries comparison

2018-06-22 Thread Антонио Антуан
Hi there!

I have a very complicated queries generator and I want to test it. 
Currently it looks like that:

def test():
query = generate_query()
compiled = compile_query(query)
assert compiled == 'SELECT * FROM table'

Problems begin when I add some filters to query:

def test():
query = generate_query_with_filters()
compiled = compile_query(query)
assert compiled == 'SELECT * FROM table WHERE a = 1 and b = 2'

This test can fail sometimes because condition in compiled query can be 
swaped: "SELECT * FROM table WHERE b = 2 and a = 1". 
Is there any way to compare Query objects?

I tried to do this (does't work):
a = Model.query
b = Model.query
# assert a == b, 'failed'

a = Model.query.filter(Model.col == 1)
b = Model.query.filter(Model.col == 1)
# assert a.statement == b.statement

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.