diff options
Diffstat (limited to 'test/sql/select.py')
-rw-r--r-- | test/sql/select.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/sql/select.py b/test/sql/select.py index 70d21798c..b4e47c3e0 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -3,6 +3,7 @@ import datetime, re, operator from sqlalchemy import * from sqlalchemy import exc, sql, util from sqlalchemy.sql import table, column, compiler +from sqlalchemy.engine import default from sqlalchemy.databases import sqlite, postgres, mysql, oracle, firebird, mssql from testlib import * @@ -326,6 +327,54 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A x = func.lala(table1.c.myid).label('foo') self.assert_compile(select([x], x==5), "SELECT lala(mytable.myid) AS foo FROM mytable WHERE lala(mytable.myid) = :param_1") + def test_labels_in_expressions(self): + """test that label() constructs in ORDER BY render as the labelname. + + Postgres' behavior was used as the guide for this, + so that only a simple label expression + and not a more complex expression involving the label + name would be rendered using the label name. + + """ + lab1 = (table1.c.myid + "12").label('foo') + lab2 = func.somefunc(table1.c.name).label('bar') + + dialect = default.DefaultDialect() + self.assert_compile(select([lab1, lab2]).order_by(lab1, desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY foo, bar DESC", + dialect=dialect + ) + + # the function embedded label renders as the function + self.assert_compile(select([lab1, lab2]).order_by(func.hoho(lab1), desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY hoho(mytable.myid + :myid_1), bar DESC", + dialect=dialect + ) + + # binary expressions render as the expression without labels + self.assert_compile(select([lab1, lab2]).order_by(lab1 + "test"), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY mytable.myid + :myid_1 + :param_1", + dialect=dialect + ) + + # labels within functions in the columns clause render with the expression + self.assert_compile( + select([lab1, func.foo(lab1)]), + "SELECT mytable.myid + :myid_1 AS foo, foo(mytable.myid + :myid_1) AS foo_1 FROM mytable", + dialect=dialect + ) + + dialect = default.DefaultDialect() + dialect.supports_simple_order_by_label = False + self.assert_compile(select([lab1, lab2]).order_by(lab1, desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY mytable.myid + :myid_1, somefunc(mytable.name) DESC", + dialect=dialect + ) + self.assert_compile(select([lab1, lab2]).order_by(func.hoho(lab1), desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY hoho(mytable.myid + :myid_1), somefunc(mytable.name) DESC", + dialect=dialect + ) + def test_conjunctions(self): self.assert_compile( and_(table1.c.myid == 12, table1.c.name=='asdf', table2.c.othername == 'foo', "sysdate() = today()"), |