diff options
Diffstat (limited to 'test/ext/test_declarative.py')
-rw-r--r-- | test/ext/test_declarative.py | 107 |
1 files changed, 75 insertions, 32 deletions
diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index f628d1dc7..c9159f953 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -14,6 +14,7 @@ from sqlalchemy.orm import relationship, create_session, class_mapper, \ from sqlalchemy.test.testing import eq_ from sqlalchemy.util import classproperty from test.orm._base import ComparableEntity, MappedTest +from sqlalchemy.ext.declarative import mapperproperty class DeclarativeTestBase(testing.TestBase, testing.AssertsExecutionResults): def setup(self): @@ -693,7 +694,7 @@ class DeclarativeTest(DeclarativeTestBase): eq_(sess.query(User).all(), [User(name='u1', address_count=2, addresses=[Address(email='one'), Address(email='two')])]) - def test_useless_classproperty(self): + def test_useless_mapperproperty(self): class Address(Base, ComparableEntity): __tablename__ = 'addresses' @@ -710,7 +711,7 @@ class DeclarativeTest(DeclarativeTestBase): name = Column('name', String(50)) addresses = relationship('Address', backref='user') - @classproperty + @mapperproperty def address_count(cls): # this doesn't really gain us anything. but if # one is used, lets have it function as expected... @@ -2197,7 +2198,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): def test_table_name_inherited(self): class MyMixin: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() id = Column(Integer, primary_key=True) @@ -2206,11 +2207,23 @@ class DeclarativeMixinTest(DeclarativeTestBase): pass eq_(MyModel.__table__.name, 'mymodel') + + def test_classproperty_still_works(self): + class MyMixin(object): + @classproperty + def __tablename__(cls): + return cls.__name__.lower() + id = Column(Integer, primary_key=True) + + class MyModel(Base, MyMixin): + __tablename__ = 'overridden' + eq_(MyModel.__table__.name, 'overridden') + def test_table_name_not_inherited(self): class MyMixin: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() id = Column(Integer, primary_key=True) @@ -2223,12 +2236,12 @@ class DeclarativeMixinTest(DeclarativeTestBase): def test_table_name_inheritance_order(self): class MyMixin1: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() + '1' class MyMixin2: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() + '2' @@ -2240,7 +2253,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): def test_table_name_dependent_on_subclass(self): class MyHistoryMixin: - @classproperty + @mapperproperty def __tablename__(cls): return cls.parent_name + '_changelog' @@ -2264,7 +2277,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): def test_table_args_inherited_descriptor(self): class MyMixin: - @classproperty + @mapperproperty def __table_args__(cls): return {'info': cls.__name__} @@ -2303,10 +2316,10 @@ class DeclarativeMixinTest(DeclarativeTestBase): eq_(MyModel.__table__.kwargs, {'mysql_engine': 'InnoDB'}) - def test_mapper_args_classproperty(self): + def test_mapper_args_mapperproperty(self): class ComputedMapperArgs: - @classproperty + @mapperproperty def __mapper_args__(cls): if cls.__name__ == 'Person': return {'polymorphic_on': cls.discriminator} @@ -2326,13 +2339,13 @@ class DeclarativeMixinTest(DeclarativeTestBase): is Person.__table__.c.type eq_(class_mapper(Engineer).polymorphic_identity, 'Engineer') - def test_mapper_args_classproperty_two(self): + def test_mapper_args_mapperproperty_two(self): - # same as test_mapper_args_classproperty, but we repeat + # same as test_mapper_args_mapperproperty, but we repeat # ComputedMapperArgs on both classes for no apparent reason. class ComputedMapperArgs: - @classproperty + @mapperproperty def __mapper_args__(cls): if cls.__name__ == 'Person': return {'polymorphic_on': cls.discriminator} @@ -2367,7 +2380,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): __tablename__ = 'test' - @classproperty + @mapperproperty def __table_args__(self): info = {} args = dict(info=info) @@ -2395,7 +2408,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): class MyMixin: - @classproperty + @mapperproperty def __mapper_args__(cls): # tenuous, but illustrates the problem! @@ -2457,7 +2470,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): __tablename__ = 'test' - @classproperty + @mapperproperty def __mapper_args__(cls): args = {} args.update(MyMixin1.__mapper_args__) @@ -2484,15 +2497,15 @@ class DeclarativeMixinTest(DeclarativeTestBase): def test_mapper_args_property(self): class MyModel(Base): - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() - @classproperty + @mapperproperty def __table_args__(cls): return {'mysql_engine':'InnoDB'} - @classproperty + @mapperproperty def __mapper_args__(cls): args = {} args['polymorphic_identity'] = cls.__name__ @@ -2513,6 +2526,36 @@ class DeclarativeMixinTest(DeclarativeTestBase): eq_(MySubModel2.__table__.kwargs['mysql_engine'], 'InnoDB') eq_(MyModel.__table__.name, 'mymodel') eq_(MySubModel.__table__.name, 'mysubmodel') + + def test_mapper_args_custom_base(self): + """test the @mapperproperty approach from a custom base.""" + + class Base(object): + @mapperproperty + def __tablename__(cls): + return cls.__name__.lower() + + @mapperproperty + def __table_args__(cls): + return {'mysql_engine':'InnoDB'} + + @mapperproperty + def id(self): + return Column(Integer, primary_key=True) + + Base = decl.declarative_base(cls=Base) + + class MyClass(Base): + pass + + class MyOtherClass(Base): + pass + + eq_(MyClass.__table__.kwargs['mysql_engine'], 'InnoDB') + eq_(MyClass.__table__.name, 'myclass') + eq_(MyOtherClass.__table__.name, 'myotherclass') + assert MyClass.__table__.c.id.table is MyClass.__table__ + assert MyOtherClass.__table__.c.id.table is MyOtherClass.__table__ def test_single_table_no_propagation(self): @@ -2541,7 +2584,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): class CommonMixin: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() __table_args__ = {'mysql_engine': 'InnoDB'} @@ -2571,7 +2614,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): class CommonMixin: - @classproperty + @mapperproperty def __tablename__(cls): return cls.__name__.lower() __table_args__ = {'mysql_engine': 'InnoDB'} @@ -2608,7 +2651,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): class NoJoinedTableNameMixin: - @classproperty + @mapperproperty def __tablename__(cls): if decl.has_inherited_table(cls): return None @@ -2636,7 +2679,7 @@ class DeclarativeMixinTest(DeclarativeTestBase): class TableNameMixin: - @classproperty + @mapperproperty def __tablename__(cls): if decl.has_inherited_table(cls) and TableNameMixin \ not in cls.__bases__: @@ -2761,7 +2804,7 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): class MyMixin(object): - @classproperty + @mapperproperty def prop_hoho(cls): return column_property(Column('prop', String(50))) @@ -2800,20 +2843,20 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): def test_doc(self): """test documentation transfer. - the documentation situation with @classproperty is problematic. + the documentation situation with @mapperproperty is problematic. at least see if mapped subclasses get the doc. """ class MyMixin(object): - @classproperty + @mapperproperty def type_(cls): """this is a document.""" return Column(String(50)) - @classproperty + @mapperproperty def t2(cls): """this is another document.""" @@ -2832,7 +2875,7 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): class MyMixin(object): - @classproperty + @mapperproperty def type_(cls): return Column(String(50)) __mapper_args__ = {'polymorphic_on': type_} @@ -2851,7 +2894,7 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): class MyMixin(object): - @classproperty + @mapperproperty def data(cls): return deferred(Column('data', String(50))) @@ -2875,19 +2918,19 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): class RefTargetMixin(object): - @classproperty + @mapperproperty def target_id(cls): return Column('target_id', ForeignKey('target.id')) if usestring: - @classproperty + @mapperproperty def target(cls): return relationship('Target', primaryjoin='Target.id==%s.target_id' % cls.__name__) else: - @classproperty + @mapperproperty def target(cls): return relationship('Target') |