]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added assert_raises() to TestBase class
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Mar 2008 18:44:45 +0000 (18:44 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 6 Mar 2008 18:44:45 +0000 (18:44 +0000)
- 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
lib/sqlalchemy/orm/session.py
test/orm/expire.py
test/testlib/testing.py

diff --git a/CHANGES b/CHANGES
index edddfb9a71ceef4821f90c2484a470d45b94c1dc..12958819761fbb00a91de98e36a1666e6c2730f5 100644 (file)
--- 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().
 
index 8f85a496c4fc63543d45dabdb1fe6d9a86e12200..a01c444c085f43a24bdf5c8ea4a96bdd7c72d0b1 100644 (file)
@@ -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.
index dca56dfb8fa592dfd962f397432c0aaa2ec0cc34..9c4b98251ef283a8e6994bdafe9c7f94654bd672 100644 (file)
@@ -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()
index 1d7062c882eb62220044a4c4a1b2c3c81da644cb..3406e3504e30ce05a42f3ae75762f31cd7e1bf8f 100644 (file)
@@ -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, " +