Skip to content

Commit

Permalink
- Fixed bug which prevented history_meta recipe from working with
Browse files Browse the repository at this point in the history
joined inheritance schemes more than one level deep.
- don't need to use _lib.py here anymore now that these features are in
sqlalchemy.testing
  • Loading branch information
zzzeek committed Nov 8, 2013
1 parent 5d0e844 commit 29a07fc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 99 deletions.
6 changes: 6 additions & 0 deletions doc/build/changelog/changelog_09.rst
Expand Up @@ -12,6 +12,12 @@
.. changelog::
:version: 0.9.0b2

.. change::
:tags: bug, examples

Fixed bug which prevented history_meta recipe from working with
joined inheritance schemes more than one level deep.

.. change::
:tags: bug, orm, sql, sqlite
:tickets: 2858
Expand Down
96 changes: 0 additions & 96 deletions examples/versioning/_lib.py

This file was deleted.

3 changes: 2 additions & 1 deletion examples/versioning/history_meta.py
Expand Up @@ -25,6 +25,7 @@ def _history_mapper(local_mapper):

polymorphic_on = None
super_fks = []

if not super_mapper or local_mapper.local_table is not super_mapper.local_table:
cols = []
for column in local_mapper.local_table.c:
Expand All @@ -43,7 +44,7 @@ def _history_mapper(local_mapper):
polymorphic_on = col

if super_mapper:
super_fks.append(('version', super_history_mapper.base_mapper.local_table.c.version))
super_fks.append(('version', super_history_mapper.local_table.c.version))
cols.append(Column('version', Integer, primary_key=True, autoincrement=False))
else:
cols.append(Column('version', Integer, primary_key=True, autoincrement=False))
Expand Down
81 changes: 79 additions & 2 deletions examples/versioning/test_versioning.py
Expand Up @@ -3,7 +3,8 @@
from .history_meta import Versioned, versioned_session
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import clear_mappers, Session, deferred, relationship
from ._lib import ComparableEntity, eq_
from sqlalchemy.testing import AssertsCompiledSQL, eq_
from sqlalchemy.testing.entities import BasicEntity, ComparableEntity

engine = None

Expand All @@ -12,7 +13,9 @@ def setup():
global engine
engine = create_engine('sqlite://', echo=True)

class TestVersioning(TestCase):
class TestVersioning(TestCase, AssertsCompiledSQL):
__dialect__ = 'default'

def setUp(self):
self.session = Session(engine)
self.Base = declarative_base()
Expand Down Expand Up @@ -218,6 +221,80 @@ class SubClassSamePk(BaseClass):
]
)

def test_joined_inheritance_multilevel(self):
class BaseClass(Versioned, self.Base, ComparableEntity):
__tablename__ = 'basetable'

id = Column(Integer, primary_key=True)
name = Column(String(50))
type = Column(String(20))

__mapper_args__ = {'polymorphic_on': type,
'polymorphic_identity': 'base'}

class SubClass(BaseClass):
__tablename__ = 'subtable'

id = Column(Integer, primary_key=True)
base_id = Column(Integer, ForeignKey('basetable.id'))
subdata1 = Column(String(50))

__mapper_args__ = {'polymorphic_identity': 'sub'}

class SubSubClass(SubClass):
__tablename__ = 'subsubtable'

id = Column(Integer, ForeignKey('subtable.id'), primary_key=True)
subdata2 = Column(String(50))

__mapper_args__ = {'polymorphic_identity': 'subsub'}

self.create_tables()

SubSubHistory = SubSubClass.__history_mapper__.class_
sess = self.session
q = sess.query(SubSubHistory)
self.assert_compile(
q,
"SELECT subsubtable_history.id AS subsubtable_history_id, "
"subtable_history.id AS subtable_history_id, "
"basetable_history.id AS basetable_history_id, "
"basetable_history.name AS basetable_history_name, "
"basetable_history.type AS basetable_history_type, "
"subsubtable_history.version AS subsubtable_history_version, "
"subtable_history.version AS subtable_history_version, "
"basetable_history.version AS basetable_history_version, "
"subtable_history.base_id AS subtable_history_base_id, "
"subtable_history.subdata1 AS subtable_history_subdata1, "
"subsubtable_history.subdata2 AS subsubtable_history_subdata2 "
"FROM basetable_history "
"JOIN subtable_history "
"ON basetable_history.id = subtable_history.base_id "
"AND basetable_history.version = subtable_history.version "
"JOIN subsubtable_history ON subtable_history.id = "
"subsubtable_history.id AND subtable_history.version = subsubtable_history.version"
)

ssc = SubSubClass(name='ss1', subdata1='sd1', subdata2='sd2')
sess.add(ssc)
sess.commit()
eq_(
sess.query(SubSubHistory).all(),
[]
)
ssc.subdata1 = 'sd11'
ssc.subdata2 = 'sd22'
sess.commit()
eq_(
sess.query(SubSubHistory).all(),
[SubSubHistory(name='ss1', subdata1='sd1',
subdata2='sd2', type='subsub', version=1)]
)
eq_(ssc, SubSubClass(name='ss1', subdata1='sd11',
subdata2='sd22', version=2))



def test_single_inheritance(self):
class BaseClass(Versioned, self.Base, ComparableEntity):
__tablename__ = 'basetable'
Expand Down

0 comments on commit 29a07fc

Please sign in to comment.