diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-27 16:44:34 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-27 16:44:34 -0400 |
| commit | 656cb6461b264935027580a32ce3820a7d73bd7e (patch) | |
| tree | 49f1489773a87e3bc8542e53f6d58ceb0386fa2e /test/ext/declarative | |
| parent | 640625bc9e98dd4060a1e61c717ddc98f8b3808b (diff) | |
| download | sqlalchemy-656cb6461b264935027580a32ce3820a7d73bd7e.tar.gz | |
- [feature] declared_attr can now be used with
attributes that are not Column or MapperProperty;
including any user-defined value as well
as association proxy objects. [ticket:2517]
Diffstat (limited to 'test/ext/declarative')
| -rw-r--r-- | test/ext/declarative/test_mixin.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/test/ext/declarative/test_mixin.py b/test/ext/declarative/test_mixin.py index a77d6be81..7bc1e1d15 100644 --- a/test/ext/declarative/test_mixin.py +++ b/test/ext/declarative/test_mixin.py @@ -963,6 +963,62 @@ class DeclarativeMixinTest(DeclarativeTestBase): assert C().x() == 'hi' + def test_arbitrary_attrs_one(self): + class HasMixin(object): + @declared_attr + def some_attr(cls): + return cls.__name__ + "SOME ATTR" + + class Mapped(HasMixin, Base): + __tablename__ = 't' + id = Column(Integer, primary_key=True) + + eq_(Mapped.some_attr, "MappedSOME ATTR") + eq_(Mapped.__dict__['some_attr'], "MappedSOME ATTR") + + def test_arbitrary_attrs_two(self): + from sqlalchemy.ext.associationproxy import association_proxy + + class FilterA(Base): + __tablename__ = 'filter_a' + id = Column(Integer(), primary_key=True) + parent_id = Column(Integer(), + ForeignKey('type_a.id')) + filter = Column(String()) + def __init__(self, filter_, **kw): + self.filter = filter_ + + class FilterB(Base): + __tablename__ = 'filter_b' + id = Column(Integer(), primary_key=True) + parent_id = Column(Integer(), + ForeignKey('type_b.id')) + filter = Column(String()) + def __init__(self, filter_, **kw): + self.filter = filter_ + + class FilterMixin(object): + @declared_attr + def _filters(cls): + return relationship(cls.filter_class, + cascade='all,delete,delete-orphan') + + @declared_attr + def filters(cls): + return association_proxy('_filters', 'filter') + + class TypeA(Base, FilterMixin): + __tablename__ = 'type_a' + filter_class = FilterA + id = Column(Integer(), primary_key=True) + + class TypeB(Base, FilterMixin): + __tablename__ = 'type_b' + filter_class = FilterB + id = Column(Integer(), primary_key=True) + + TypeA(filters=[u'foo']) + TypeB(filters=[u'foo']) class DeclarativeMixinPropertyTest(DeclarativeTestBase): |
