summaryrefslogtreecommitdiff
path: root/test/sql/test_compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-02-05 16:58:32 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2012-02-05 16:58:32 -0500
commit2dbeeff50b7ccc6f47b2816a59f99f051fdabc8c (patch)
treec23ba02c34a7fec923bdddd99ff46b670e30bac2 /test/sql/test_compiler.py
parent28765734826f2619fcfd40f047a5980c3af49010 (diff)
downloadsqlalchemy-2dbeeff50b7ccc6f47b2816a59f99f051fdabc8c.tar.gz
- [bug] Added support for using the .key
of a Column as a string identifier in a result set row. The .key is currently listed as an "alternate" name for a column, and is superseded by the name of a column which has that key value as its regular name. For the next major release of SQLAlchemy we may reverse this precedence so that .key takes precedence, but this is not decided on yet. [ticket:2392]
Diffstat (limited to 'test/sql/test_compiler.py')
-rw-r--r--test/sql/test_compiler.py64
1 files changed, 59 insertions, 5 deletions
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py
index d9fad9493..6330ee34e 100644
--- a/test/sql/test_compiler.py
+++ b/test/sql/test_compiler.py
@@ -64,6 +64,12 @@ addresses = table('addresses',
column('zip')
)
+keyed = Table('keyed', metadata,
+ Column('x', Integer, key='colx'),
+ Column('y', Integer, key='coly'),
+ Column('z', Integer),
+)
+
class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = 'default'
@@ -242,6 +248,20 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
"SELECT sum(lala(mytable.myid)) AS bar FROM mytable"
)
+ # changes with #2397
+ self.assert_compile(
+ select([keyed]),
+ "SELECT keyed.x, keyed.y"
+ ", keyed.z FROM keyed"
+ )
+
+ # changes with #2397
+ self.assert_compile(
+ select([keyed]).apply_labels(),
+ "SELECT keyed.x AS keyed_x, keyed.y AS "
+ "keyed_y, keyed.z AS keyed_z FROM keyed"
+ )
+
def test_paramstyles(self):
stmt = text("select :foo, :bar, :bat from sometable")
@@ -272,7 +292,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
)
def test_dupe_columns(self):
- """test that deduping is performed against clause element identity, not rendered result."""
+ """test that deduping is performed against clause
+ element identity, not rendered result."""
self.assert_compile(
select([column('a'), column('a'), column('a')]),
@@ -294,6 +315,17 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
, dialect=default.DefaultDialect()
)
+ # using alternate keys.
+ # this will change with #2397
+ a, b, c = Column('a', Integer, key='b'), \
+ Column('b', Integer), \
+ Column('c', Integer, key='a')
+ self.assert_compile(
+ select([a, b, c, a, b, c]),
+ "SELECT a, b, c"
+ , dialect=default.DefaultDialect()
+ )
+
self.assert_compile(
select([bindparam('a'), bindparam('b'), bindparam('c')]),
"SELECT :a AS anon_1, :b AS anon_2, :c AS anon_3"
@@ -315,12 +347,10 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
s = s.compile(dialect=default.DefaultDialect(paramstyle='qmark'))
eq_(s.positiontup, ['a', 'b', 'c'])
- def test_nested_uselabels(self):
- """test nested anonymous label generation. this
- essentially tests the ANONYMOUS_LABEL regex.
+ def test_nested_label_targeting(self):
+ """test nested anonymous label generation.
"""
-
s1 = table1.select()
s2 = s1.alias()
s3 = select([s2], use_labels=True)
@@ -339,6 +369,30 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
'AS description FROM mytable) AS anon_2) '
'AS anon_1')
+ def test_nested_label_targeting_keyed(self):
+ # this behavior chagnes with #2397
+ s1 = keyed.select()
+ s2 = s1.alias()
+ s3 = select([s2], use_labels=True)
+ self.assert_compile(s3,
+ "SELECT anon_1.x AS anon_1_x, "
+ "anon_1.y AS anon_1_y, "
+ "anon_1.z AS anon_1_z FROM "
+ "(SELECT keyed.x AS x, keyed.y "
+ "AS y, keyed.z AS z FROM keyed) AS anon_1")
+
+ s4 = s3.alias()
+ s5 = select([s4], use_labels=True)
+ self.assert_compile(s5,
+ "SELECT anon_1.anon_2_x AS anon_1_anon_2_x, "
+ "anon_1.anon_2_y AS anon_1_anon_2_y, "
+ "anon_1.anon_2_z AS anon_1_anon_2_z "
+ "FROM (SELECT anon_2.x AS anon_2_x, anon_2.y AS anon_2_y, "
+ "anon_2.z AS anon_2_z FROM "
+ "(SELECT keyed.x AS x, keyed.y AS y, keyed.z "
+ "AS z FROM keyed) AS anon_2) AS anon_1"
+ )
+
def test_dont_overcorrelate(self):
self.assert_compile(select([table1], from_obj=[table1,
table1.select()]),