summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-12-18 00:24:03 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-12-18 00:24:03 +0000
commitf6068a3522bb92fac18c930eb26798fc4cb1889f (patch)
tree2ce96db7758621f605ea605342a106309e134d56 /test
parent420c098d56e9925b51067711c6817475165c6af2 (diff)
downloadsqlalchemy-f6068a3522bb92fac18c930eb26798fc4cb1889f.tar.gz
- select().as_scalar() will raise an exception if the select does not have
exactly one expression in its columns clause. - added "helper exception" to select.type access, generic functions raise the chance of this happening - a slight behavioral change to attributes is, del'ing an attribute does *not* cause the lazyloader of that attribute to fire off again; the "del" makes the effective value of the attribute "None". To re-trigger the "loader" for an attribute, use session.expire(instance, [attrname]). - fix ormtutorial for IS NULL
Diffstat (limited to 'test')
-rw-r--r--test/orm/attributes.py50
-rw-r--r--test/sql/select.py15
2 files changed, 63 insertions, 2 deletions
diff --git a/test/orm/attributes.py b/test/orm/attributes.py
index 088e33623..a756566d5 100644
--- a/test/orm/attributes.py
+++ b/test/orm/attributes.py
@@ -905,10 +905,48 @@ class HistoryTest(PersistTest):
f = Foo()
f.bars.append(bar2)
self.assertEquals(attributes.get_history(f._state, 'bars'), ([bar2], [], []))
-
+
def test_scalar_via_lazyload(self):
class Foo(fixtures.Base):
pass
+
+ lazy_load = None
+ def lazyload(instance):
+ def load():
+ return lazy_load
+ return load
+
+ attributes.register_class(Foo)
+ attributes.register_attribute(Foo, 'bar', uselist=False, callable_=lazyload, useobject=False)
+ lazy_load = "hi"
+
+ # with scalar non-object, the lazy callable is only executed on gets, not history
+ # operations
+
+ f = Foo()
+ self.assertEquals(f.bar, "hi")
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([], ["hi"], []))
+
+ f = Foo()
+ f.bar = None
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([None], [], []))
+
+ f = Foo()
+ f.bar = "there"
+ self.assertEquals(attributes.get_history(f._state, 'bar'), (["there"], [], []))
+ f.bar = "hi"
+ self.assertEquals(attributes.get_history(f._state, 'bar'), (["hi"], [], []))
+
+ f = Foo()
+ self.assertEquals(f.bar, "hi")
+ del f.bar
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([], [], ["hi"]))
+ assert f.bar is None
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([None], [], ["hi"]))
+
+ def test_scalar_object_via_lazyload(self):
+ class Foo(fixtures.Base):
+ pass
class Bar(fixtures.Base):
pass
@@ -924,6 +962,9 @@ class HistoryTest(PersistTest):
bar1, bar2 = [Bar(id=1), Bar(id=2)]
lazy_load = bar1
+ # with scalar object, the lazy callable is only executed on gets and history
+ # operations
+
f = Foo()
self.assertEquals(attributes.get_history(f._state, 'bar'), ([], [bar1], []))
@@ -937,5 +978,12 @@ class HistoryTest(PersistTest):
f.bar = bar1
self.assertEquals(attributes.get_history(f._state, 'bar'), ([], [bar1], []))
+ f = Foo()
+ self.assertEquals(f.bar, bar1)
+ del f.bar
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([None], [], [bar1]))
+ assert f.bar is None
+ self.assertEquals(attributes.get_history(f._state, 'bar'), ([None], [], [bar1]))
+
if __name__ == "__main__":
testbase.main()
diff --git a/test/sql/select.py b/test/sql/select.py
index 10e92a083..3f613596e 100644
--- a/test/sql/select.py
+++ b/test/sql/select.py
@@ -60,7 +60,7 @@ class SelectTest(SQLCompileTest):
assert hasattr(table1.select(), 'c')
assert not hasattr(table1.c.myid.self_group(), 'columns')
assert hasattr(table1.select().self_group(), 'columns')
- assert not hasattr(table1.select().as_scalar().self_group(), 'columns')
+ assert not hasattr(select([table1.c.myid]).as_scalar().self_group(), 'columns')
assert not hasattr(table1.c.myid, 'columns')
assert not hasattr(table1.c.myid, 'c')
assert not hasattr(table1.select().c.myid, 'c')
@@ -245,6 +245,19 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
def test_scalar_select(self):
+ try:
+ s = select([table1.c.myid, table1.c.name]).as_scalar()
+ assert False
+ except exceptions.InvalidRequestError, err:
+ assert str(err) == "Scalar select can only be created from a Select object that has exactly one column expression.", str(err)
+
+ try:
+ # generic function which will look at the type of expression
+ func.coalesce(select([table1.c.myid]))
+ assert False
+ except exceptions.InvalidRequestError, err:
+ assert str(err) == "Select objects don't have a type. Call as_scalar() on this Select object to return a 'scalar' version of this Select.", str(err)
+
s = select([table1.c.myid], scalar=True, correlate=False)
self.assert_compile(select([table1, s]), "SELECT mytable.myid, mytable.name, mytable.description, (SELECT mytable.myid FROM mytable) AS anon_1 FROM mytable")