diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-07-24 17:51:01 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-07-24 17:51:01 -0400 |
| commit | 8a483dbf38168ff43ca0652229b1d46afb23235d (patch) | |
| tree | 0707d1e1f3912bda8262e32bd1e4e008a7c67b88 /test | |
| parent | 9867086943d60e347695930dd7f442f9e95e4577 (diff) | |
| download | sqlalchemy-8a483dbf38168ff43ca0652229b1d46afb23235d.tar.gz | |
- rewrite cloned_traverse() and replacement_traverse() to use a straight
recursive descent with clone() + _copy_internals(). This is essentially
what it was doing anyway with lots of unnecessary steps.
Fix Alias() to honor the given clone() function which may have been the
reason the traversal hadn't been fixed sooner. Alias._copy_internals()
will specifically skip an alias of a Table
as a more specific form of what it was doing before. This may need to
be further improved such that ClauseAdapter or replacement_traverse()
send it some specific hints what not to dig into; **kw has been added
to all _copy_internals() to support this. replacement/clone traversal
is at least clear now.
- apply new no_replacement_traverse annotation to join created by
_create_joins(), fixes [ticket:2195]
- can replace orm.query "_halt_adapt" with "no_replacement_traverse"
Diffstat (limited to 'test')
| -rw-r--r-- | test/orm/test_joins.py | 42 | ||||
| -rw-r--r-- | test/sql/test_generative.py | 13 | ||||
| -rw-r--r-- | test/sql/test_selectable.py | 7 |
3 files changed, 61 insertions, 1 deletions
diff --git a/test/orm/test_joins.py b/test/orm/test_joins.py index a3f08a025..6c90dbf73 100644 --- a/test/orm/test_joins.py +++ b/test/orm/test_joins.py @@ -1438,6 +1438,48 @@ class SelfRefMixedTest(fixtures.MappedTest, AssertsCompiledSQL): "assoc_table_1.right_id JOIN sub_table ON nodes.id = sub_table.node_id", ) +class CreateJoinsTest(fixtures.ORMTest, AssertsCompiledSQL): + + def _inherits_fixture(self): + m = MetaData() + base = Table('base', m, Column('id', Integer, primary_key=True)) + a = Table('a', m, + Column('id', Integer, ForeignKey('base.id'), primary_key=True), + Column('b_id', Integer, ForeignKey('b.id'))) + b = Table('b', m, + Column('id', Integer, ForeignKey('base.id'), primary_key=True), + Column('c_id', Integer, ForeignKey('c.id'))) + c = Table('c', m, + Column('id', Integer, ForeignKey('base.id'), primary_key=True)) + class Base(object): + pass + class A(Base): + pass + class B(Base): + pass + class C(Base): + pass + mapper(Base, base) + mapper(A, a, inherits=Base, properties={'b':relationship(B, primaryjoin=a.c.b_id==b.c.id)}) + mapper(B, b, inherits=Base, properties={'c':relationship(C, primaryjoin=b.c.c_id==c.c.id)}) + mapper(C, c, inherits=Base) + return A, B, C, Base + + def test_double_level_aliased_exists(self): + A, B, C, Base = self._inherits_fixture() + s = Session() + self.assert_compile( + s.query(A).filter(A.b.has(B.c.has(C.id==5))), + "SELECT a.id AS a_id, base.id AS base_id, a.b_id AS a_b_id " + "FROM base JOIN a ON base.id = a.id WHERE " + "EXISTS (SELECT 1 FROM (SELECT base.id AS base_id, b.id AS " + "b_id, b.c_id AS b_c_id FROM base JOIN b ON base.id = b.id) " + "AS anon_1 WHERE a.b_id = anon_1.b_id AND (EXISTS " + "(SELECT 1 FROM (SELECT base.id AS base_id, c.id AS c_id " + "FROM base JOIN c ON base.id = c.id) AS anon_2 " + "WHERE anon_1.b_c_id = anon_2.c_id AND anon_2.c_id = ?" + ")))" + ) class SelfReferentialTest(fixtures.MappedTest, AssertsCompiledSQL): run_setup_mappers = 'once' diff --git a/test/sql/test_generative.py b/test/sql/test_generative.py index 47e45bbb9..f9333dbf5 100644 --- a/test/sql/test_generative.py +++ b/test/sql/test_generative.py @@ -779,7 +779,7 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): s2 = s2._clone() assert s2.is_derived_from(s1) - def test_aliasedselect_to_aliasedselect(self): + def test_aliasedselect_to_aliasedselect_straight(self): # original issue from ticket #904 @@ -791,6 +791,10 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): 'AS col2, table1.col3 AS col3 FROM table1) ' 'AS foo LIMIT :param_1 OFFSET :param_2', {'param_1': 5, 'param_2': 10}) + + def test_aliasedselect_to_aliasedselect_join(self): + s1 = select([t1]).alias('foo') + s2 = select([s1]).limit(5).offset(10).alias() j = s1.outerjoin(t2, s1.c.col1 == t2.c.col1) self.assert_compile(sql_util.ClauseAdapter(s2).traverse(j).select(), 'SELECT anon_1.col1, anon_1.col2, ' @@ -803,8 +807,15 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ':param_2) AS anon_1 LEFT OUTER JOIN ' 'table2 ON anon_1.col1 = table2.col1', {'param_1': 5, 'param_2': 10}) + + def test_aliasedselect_to_aliasedselect_join_nested_table(self): + s1 = select([t1]).alias('foo') + s2 = select([s1]).limit(5).offset(10).alias() talias = t1.alias('bar') + + assert not s2.is_derived_from(talias) j = s1.outerjoin(talias, s1.c.col1 == talias.c.col1) + self.assert_compile(sql_util.ClauseAdapter(s2).traverse(j).select(), 'SELECT anon_1.col1, anon_1.col2, ' 'anon_1.col3, bar.col1, bar.col2, bar.col3 ' diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index 63be50a97..555271f16 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -914,6 +914,13 @@ class AnnotationsTest(fixtures.TestBase): b5 = visitors.cloned_traverse(b3, {}, {'binary':visit_binary}) assert str(b5) == ":bar = table1.col2" + def test_annotate_aliased(self): + t1 = table('t1', column('c1')) + s = select([(t1.c.c1 + 3).label('bat')]) + a = s.alias() + a = sql_util._deep_annotate(a, {'foo': 'bar'}) + eq_(a._annotations['foo'], 'bar') + eq_(a.element._annotations['foo'], 'bar') def test_annotate_expressions(self): table1 = table('table1', column('col1'), column('col2')) |
