From: Mike Bayer Date: Tue, 28 Dec 2010 22:11:01 +0000 (-0500) Subject: - add standalone tests for descriptor instrumentation. these begin to replace X-Git-Tag: rel_0_7b1~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f8ca36036d9ab1f8c06ada19a2cd69349864a45;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - add standalone tests for descriptor instrumentation. these begin to replace what we're testing with synonyms and comparables in test_mapper --- diff --git a/test/orm/test_descriptor.py b/test/orm/test_descriptor.py new file mode 100644 index 0000000000..b36735e8e7 --- /dev/null +++ b/test/orm/test_descriptor.py @@ -0,0 +1,106 @@ +from sqlalchemy.orm import descriptor_props +from sqlalchemy.orm.interfaces import PropComparator +from sqlalchemy.sql import column +from sqlalchemy import Column, Integer, func +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.util import partial +from test.orm import _base +from test.lib.testing import eq_ + +class TestDescriptor(descriptor_props.DescriptorProperty): + def __init__(self, cls, key, descriptor=None, doc=None, + comparator_factory = None): + self.parent = cls.__mapper__ + self.key = key + self.doc = doc + self.descriptor = descriptor + if comparator_factory: + self._comparator_factory = partial(comparator_factory, self) + else: + self._comparator_factory = lambda mapper: None + +class DescriptorInstrumentationTest(_base.ORMTest): + def _fixture(self): + Base = declarative_base() + + class Foo(Base): + __tablename__ = 'foo' + id = Column(Integer, primary_key=True) + + return Foo + + def test_fixture(self): + Foo = self._fixture() + + d = TestDescriptor(Foo, 'foo') + d.instrument_class(Foo.__mapper__) + + assert Foo.foo + + def test_property_wrapped_classlevel(self): + Foo = self._fixture() + prop = property(lambda self:None) + Foo.foo = prop + + d = TestDescriptor(Foo, 'foo') + d.instrument_class(Foo.__mapper__) + + assert Foo().foo is None + assert Foo.foo is not prop + + def test_property_subclass_wrapped_classlevel(self): + Foo = self._fixture() + + class myprop(property): + attr = 'bar' + + def method1(self): + return "method1" + + def __getitem__(self, key): + return 'value' + + prop = myprop(lambda self:None) + Foo.foo = prop + + d = TestDescriptor(Foo, 'foo') + d.instrument_class(Foo.__mapper__) + + assert Foo().foo is None + assert Foo.foo is not prop + assert Foo.foo.attr == 'bar' + assert Foo.foo.method1() == 'method1' + assert Foo.foo['bar'] == 'value' + + def test_comparator(self): + class Comparator(PropComparator): + __hash__ = None + + attr = 'bar' + + def method1(self): + return "method1" + + def method2(self, other): + return "method2" + + # TODO ? + #def __getitem__(self, key): + # return 'value' + + def __eq__(self, other): + return column('foo') == func.upper(other) + + Foo = self._fixture() + d = TestDescriptor(Foo, 'foo', comparator_factory=Comparator) + d.instrument_class(Foo.__mapper__) + eq_(Foo.foo.method1(), "method1") + eq_(Foo.foo.method2('x'), "method2") + assert Foo.foo.attr == 'bar' + # TODO ? + #assert Foo.foo['bar'] == 'value' + eq_( + (Foo.foo == 'bar').__str__(), + "foo = upper(:upper_1)" + ) +