summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-10-25 13:13:24 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-10-25 13:13:24 -0400
commit2b1c8eabb10c932f6e83d08147c75bb05f96a161 (patch)
treeb54f3b71a420d77016554f9458d022c94016f6cd /test
parent3a4567a718c2f9f3d8b65acb81f0caefb4f1a2b5 (diff)
downloadsqlalchemy-2b1c8eabb10c932f6e83d08147c75bb05f96a161.tar.gz
- :func:`.attributes.get_history()` when used with a scalar column-mapped
attribute will now honor the "passive" flag passed to it; as this defaults to ``PASSIVE_OFF``, the function will by default query the database if the value is not present. This is a behavioral change vs. 0.8. [ticket:2787] - Added new method :meth:`.AttributeState.load_history`, works like :attr:`.AttributeState.history` but also fires loader callables.
Diffstat (limited to 'test')
-rw-r--r--test/orm/test_attributes.py33
-rw-r--r--test/orm/test_inspect.py44
2 files changed, 72 insertions, 5 deletions
diff --git a/test/orm/test_attributes.py b/test/orm/test_attributes.py
index de44e4be3..c282bc44c 100644
--- a/test/orm/test_attributes.py
+++ b/test/orm/test_attributes.py
@@ -1339,7 +1339,7 @@ class PendingBackrefTest(fixtures.ORMTest):
]
)
- def test_lazy_history(self):
+ def test_lazy_history_collection(self):
Post, Blog, lazy_posts = self._fixture()
p1, p2, p3 = Post("post 1"), Post("post 2"), Post("post 3")
@@ -1511,6 +1511,12 @@ class HistoryTest(fixtures.TestBase):
return Foo, Bar
def _someattr_history(self, f, **kw):
+ passive = kw.pop('passive', None)
+ if passive is True:
+ kw['passive'] = attributes.PASSIVE_NO_INITIALIZE
+ elif passive is False:
+ kw['passive'] = attributes.PASSIVE_OFF
+
return attributes.get_state_history(
attributes.instance_state(f),
'someattr', **kw)
@@ -1685,19 +1691,19 @@ class HistoryTest(fixtures.TestBase):
Foo = self._fixture(uselist=True, useobject=True,
active_history=True)
f = Foo()
- eq_(self._someattr_history(f, passive=True), ((), (), ()))
+ eq_(self._someattr_history(f, passive=True), (None, None, None))
def test_scalar_obj_never_set(self):
Foo = self._fixture(uselist=False, useobject=True,
active_history=True)
f = Foo()
- eq_(self._someattr_history(f, passive=True), ((), (), ()))
+ eq_(self._someattr_history(f, passive=True), (None, None, None))
def test_scalar_never_set(self):
Foo = self._fixture(uselist=False, useobject=False,
active_history=True)
f = Foo()
- eq_(self._someattr_history(f, passive=True), ((), (), ()))
+ eq_(self._someattr_history(f, passive=True), (None, None, None))
def test_scalar_active_set(self):
Foo = self._fixture(uselist=False, useobject=False,
@@ -1793,6 +1799,24 @@ class HistoryTest(fixtures.TestBase):
eq_(self._someattr_history(f), (['two'], (), ()))
+ def test_scalar_passive_flag(self):
+ Foo = self._fixture(uselist=False, useobject=False,
+ active_history=True)
+ f = Foo()
+ f.someattr = 'one'
+ eq_(self._someattr_history(f), (['one'], (), ()))
+
+ self._commit_someattr(f)
+
+ state = attributes.instance_state(f)
+ state._expire_attribute_pre_commit(state.dict, 'someattr')
+
+ def scalar_loader(state, toload):
+ state.dict['someattr'] = 'one'
+ state.manager.deferred_scalar_loader = scalar_loader
+
+ eq_(self._someattr_history(f), ((), ['one'], ()))
+
def test_scalar_inplace_mutation_set(self):
Foo = self._fixture(uselist=False, useobject=False,
@@ -1848,6 +1872,7 @@ class HistoryTest(fixtures.TestBase):
f.someattr = ['a']
eq_(self._someattr_history(f), ([['a']], (), ()))
+
def test_use_object_init(self):
Foo, Bar = self._two_obj_fixture(uselist=False)
f = Foo()
diff --git a/test/orm/test_inspect.py b/test/orm/test_inspect.py
index 61c1fd93e..ed8ab32e9 100644
--- a/test/orm/test_inspect.py
+++ b/test/orm/test_inspect.py
@@ -365,7 +365,7 @@ class TestORMInspection(_fixtures.FixtureTest):
[]
)
- def test_instance_state_attr_hist(self):
+ def test_instance_state_collection_attr_hist(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
@@ -379,6 +379,48 @@ class TestORMInspection(_fixtures.FixtureTest):
hist.unchanged, []
)
+ def test_instance_state_scalar_attr_hist(self):
+ User = self.classes.User
+ u1 = User(name='ed')
+ sess = Session()
+ sess.add(u1)
+ sess.commit()
+ assert 'name' not in u1.__dict__
+ insp = inspect(u1)
+ hist = insp.attrs.name.history
+ eq_(
+ hist.unchanged, None
+ )
+ assert 'name' not in u1.__dict__
+
+ def test_instance_state_collection_attr_load_hist(self):
+ User = self.classes.User
+ u1 = User(name='ed')
+ insp = inspect(u1)
+ hist = insp.attrs.addresses.load_history()
+ eq_(
+ hist.unchanged, ()
+ )
+ u1.addresses
+ hist = insp.attrs.addresses.load_history()
+ eq_(
+ hist.unchanged, []
+ )
+
+ def test_instance_state_scalar_attr_hist_load(self):
+ User = self.classes.User
+ u1 = User(name='ed')
+ sess = Session()
+ sess.add(u1)
+ sess.commit()
+ assert 'name' not in u1.__dict__
+ insp = inspect(u1)
+ hist = insp.attrs.name.load_history()
+ eq_(
+ hist.unchanged, ['ed']
+ )
+ assert 'name' in u1.__dict__
+
def test_instance_state_ident_transient(self):
User = self.classes.User
u1 = User(name='ed')