]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- add standalone tests for descriptor instrumentation. these begin to replace
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 28 Dec 2010 22:11:01 +0000 (17:11 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 28 Dec 2010 22:11:01 +0000 (17:11 -0500)
what we're testing with synonyms and comparables in test_mapper

test/orm/test_descriptor.py [new file with mode: 0644]

diff --git a/test/orm/test_descriptor.py b/test/orm/test_descriptor.py
new file mode 100644 (file)
index 0000000..b36735e
--- /dev/null
@@ -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)"
+        )
+