diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-09-16 19:48:22 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-09-16 19:48:22 +0000 |
commit | 998183be6bb4575b1088b495b9078a6f3f91293f (patch) | |
tree | 94fcff1ff16509639a1d81e66541ccdccbbd0350 /test/orm/inheritance/test_basic.py | |
parent | 89a8e000ef02840961a8898982c43da369a553c3 (diff) | |
download | sqlalchemy-998183be6bb4575b1088b495b9078a6f3f91293f.tar.gz |
- contains_eager() now works with the automatically
generated subquery that results when you say
"query(Parent).join(Parent.somejoinedsubclass)", i.e.
when Parent joins to a joined-table-inheritance subclass.
Previously contains_eager() would erroneously add the
subclass table to the query separately producing a
cartesian product. An example is in the ticket
description. [ticket:1543]
Diffstat (limited to 'test/orm/inheritance/test_basic.py')
-rw-r--r-- | test/orm/inheritance/test_basic.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/test/orm/inheritance/test_basic.py b/test/orm/inheritance/test_basic.py index 713ae3b5f..5ed6d1735 100644 --- a/test/orm/inheritance/test_basic.py +++ b/test/orm/inheritance/test_basic.py @@ -447,6 +447,88 @@ class EagerTargetingTest(_base.MappedTest): eq_(node, B(id=1, name='b1',b_data='i')) eq_(node.children[0], B(id=2, name='b2',b_data='l')) +class EagerToSubclassTest(_base.MappedTest): + """Test eagerloads to subclass mappers""" + + run_setup_classes = 'once' + run_setup_mappers = 'once' + run_inserts = 'once' + run_deletes = None + + @classmethod + def define_tables(cls, metadata): + Table('parent', metadata, + Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('data', String(10)), + ) + + Table('base', metadata, + Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('type', String(10)), + ) + + Table('sub', metadata, + Column('id', Integer, ForeignKey('base.id'), primary_key=True), + Column('data', String(10)), + Column('parent_id', Integer, ForeignKey('parent.id'), nullable=False) + ) + + @classmethod + @testing.resolve_artifact_names + def setup_classes(cls): + class Parent(_base.ComparableEntity): + pass + + class Base(_base.ComparableEntity): + pass + + class Sub(Base): + pass + + @classmethod + @testing.resolve_artifact_names + def setup_mappers(cls): + mapper(Parent, parent, properties={ + 'children':relation(Sub) + }) + mapper(Base, base, polymorphic_on=base.c.type, polymorphic_identity='b') + mapper(Sub, sub, inherits=Base, polymorphic_identity='s') + + @classmethod + @testing.resolve_artifact_names + def insert_data(cls): + sess = create_session() + p1 = Parent(data='p1', children=[Sub(data='s1'), Sub(data='s2'), Sub(data='s3')]) + p2 = Parent(data='p2', children=[Sub(data='s4'), Sub(data='s5')]) + sess.add(p1) + sess.add(p2) + sess.flush() + + @testing.resolve_artifact_names + def test_eagerload(self): + sess = create_session() + def go(): + eq_( + sess.query(Parent).options(eagerload(Parent.children)).all(), + [ + Parent(data='p1', children=[Sub(data='s1'), Sub(data='s2'), Sub(data='s3')]), + Parent(data='p2', children=[Sub(data='s4'), Sub(data='s5')]) + ] + ) + self.assert_sql_count(testing.db, go, 1) + + @testing.resolve_artifact_names + def test_contains_eager(self): + sess = create_session() + def go(): + eq_( + sess.query(Parent).join(Parent.children).options(contains_eager(Parent.children)).all(), + [ + Parent(data='p1', children=[Sub(data='s1'), Sub(data='s2'), Sub(data='s3')]), + Parent(data='p2', children=[Sub(data='s4'), Sub(data='s5')]) + ] + ) + self.assert_sql_count(testing.db, go, 1) class FlushTest(_base.MappedTest): """test dependency sorting among inheriting mappers""" |