summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2008-08-24 21:52:38 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2008-08-24 21:52:38 +0000
commit24ee97c6100988a2ef80bd18d70418eb76070e4d (patch)
tree92b2a6bc49985ab47c2d43a574ebbcbe8c08faa1 /test
parent34ecc552614143fb8f90ccc74b78fe8e3bdd7fb5 (diff)
downloadsqlalchemy-24ee97c6100988a2ef80bd18d70418eb76070e4d.tar.gz
- Fixed bug whereby changing a primary key attribute on an
entity where the attribute's previous value had been expired would produce an error upon flush(). [ticket:1151]
Diffstat (limited to 'test')
-rw-r--r--test/orm/attributes.py40
-rw-r--r--test/orm/naturalpks.py19
2 files changed, 57 insertions, 2 deletions
diff --git a/test/orm/attributes.py b/test/orm/attributes.py
index eddf48cfe..4e77935d4 100644
--- a/test/orm/attributes.py
+++ b/test/orm/attributes.py
@@ -1147,7 +1147,7 @@ class HistoryTest(_base.ORMTest):
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
+ # with scalar non-object and active_history=False, the lazy callable is only executed on gets, not history
# operations
f = Foo()
@@ -1171,6 +1171,44 @@ class HistoryTest(_base.ORMTest):
assert f.bar is None
eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ([None], (), ["hi"]))
+ def test_scalar_via_lazyload_with_active(self):
+ class Foo(_base.BasicEntity):
+ 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, active_history=True)
+ lazy_load = "hi"
+
+ # active_history=True means the lazy callable is executed on set as well as get,
+ # causing the old value to appear in the history
+
+ f = Foo()
+ eq_(f.bar, "hi")
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ((), ["hi"], ()))
+
+ f = Foo()
+ f.bar = None
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ([None], (), ['hi']))
+
+ f = Foo()
+ f.bar = "there"
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), (["there"], (), ['hi']))
+ f.bar = "hi"
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ((), ["hi"], ()))
+
+ f = Foo()
+ eq_(f.bar, "hi")
+ del f.bar
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ((), (), ["hi"]))
+ assert f.bar is None
+ eq_(attributes.get_history(attributes.instance_state(f), 'bar'), ([None], (), ["hi"]))
+
def test_scalar_object_via_lazyload(self):
class Foo(_base.BasicEntity):
pass
diff --git a/test/orm/naturalpks.py b/test/orm/naturalpks.py
index d046fada6..b581141da 100644
--- a/test/orm/naturalpks.py
+++ b/test/orm/naturalpks.py
@@ -62,7 +62,7 @@ class NaturalPKTest(_base.MappedTest):
self.assertEquals(User(username='ed', fullname='jack'), u1)
@testing.resolve_artifact_names
- def test_expiry(self):
+ def test_load_after_expire(self):
mapper(User, users)
sess = create_session()
@@ -84,6 +84,23 @@ class NaturalPKTest(_base.MappedTest):
assert sess.query(User).get('jack') is None
assert sess.query(User).get('ed').fullname == 'jack'
+ @testing.resolve_artifact_names
+ def test_flush_new_pk_after_expire(self):
+ mapper(User, users)
+ sess = create_session()
+ u1 = User(username='jack', fullname='jack')
+
+ sess.add(u1)
+ sess.flush()
+ assert sess.query(User).get('jack') is u1
+
+ sess.expire(u1)
+ u1.username = 'ed'
+ sess.flush()
+ sess.clear()
+ assert sess.query(User).get('ed').fullname == 'jack'
+
+
@testing.fails_on('mysql')
@testing.fails_on('sqlite')
def test_onetomany_passive(self):