From 4f35e8120fa7269f4090cef3b5e19fe16ce6c15a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 6 Mar 2008 18:44:45 +0000 Subject: [PATCH] - added assert_raises() to TestBase class - session.refresh() and session.expire() raise an error when called on instances which are not persistent within the session - session._validate_persistent() properly raises an error for false check --- CHANGES | 3 +++ lib/sqlalchemy/orm/session.py | 3 ++- test/orm/expire.py | 16 +++++++++++++++- test/testlib/testing.py | 12 ++++++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index edddfb9a71..1295881976 100644 --- a/CHANGES +++ b/CHANGES @@ -53,6 +53,9 @@ CHANGES session.save()'ed the pending item explicitly, the attribute/collection removal still knocks it out. + - session.refresh() and session.expire() raise an error when + called on instances which are not persistent within the session + - Fixed potential generative bug when the same Query was used to generate multiple Query objects using join(). diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 8f85a496c4..a01c444c08 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -1118,7 +1118,8 @@ class Session(object): ``Session``. """ - return instance in self + if instance not in self: + raise exceptions.InvalidRequestError("Instance '%s' is not persistent within this Session" % mapperutil.instance_str(instance)) def __contains__(self, instance): """Return True if the given instance is associated with this session. diff --git a/test/orm/expire.py b/test/orm/expire.py index dca56dfb8f..9c4b98251e 100644 --- a/test/orm/expire.py +++ b/test/orm/expire.py @@ -50,6 +50,13 @@ class ExpireTest(FixtureTest): assert u.name == 'jack' self.assert_sql_count(testing.db, go, 0) + def test_persistence_check(self): + mapper(User, users) + s = create_session() + u = s.get(User, 7) + s.clear() + self.assert_raises(lambda: s.expire(u), exceptions.InvalidRequestError, r"is not persistent within this Session") + def test_expire_doesntload_on_set(self): mapper(User, users) @@ -678,7 +685,14 @@ class RefreshTest(FixtureTest): # print u._state.callables assert u.name == 'jack' assert id(a) not in [id(x) for x in u.addresses] - + + def test_persistence_check(self): + mapper(User, users) + s = create_session() + u = s.get(User, 7) + s.clear() + self.assert_raises(lambda: s.refresh(u), exceptions.InvalidRequestError, r"is not persistent within this Session") + def test_refresh_expired(self): mapper(User, users) s = create_session() diff --git a/test/testlib/testing.py b/test/testlib/testing.py index 1d7062c882..3406e3504e 100644 --- a/test/testlib/testing.py +++ b/test/testlib/testing.py @@ -458,7 +458,15 @@ class TestBase(unittest.TestCase): def shortDescription(self): """overridden to not return docstrings""" return None - + + def assert_raises(self, callable_, except_cls, reg): + try: + callable_() + assert False, "Callable did not raise expected exception" + except Exception, e: + assert isinstance(e, except_cls), "Exception was not an instance of '%s' ('%s')" % (except_cls, type(e)) + assert re.search(reg, str(e)), "Callable raised non-matching exception: '%s'" % str(e) + if not hasattr(unittest.TestCase, 'assertTrue'): assertTrue = unittest.TestCase.failUnless if not hasattr(unittest.TestCase, 'assertFalse'): @@ -524,7 +532,7 @@ class AssertsExecutionResults(object): result = list(result) print repr(result) self.assert_list(result, class_, objects) - + def assert_list(self, result, class_, list): self.assert_(len(result) == len(list), "result list is not the same size as test list, " + -- 2.47.3