Rollback 'uncommitable' cnx
authorChristophe de Vienne <christophe@unlish.com>
Fri, 08 May 2015 11:38:07 +0200
changeset 11568 a9940c6cd693
parent 11567 4f8aa5fcd5da
child 11569 42d6d4dcd153
Rollback 'uncommitable' cnx Closes #5343870
pyramid_cubicweb/core.py
pyramid_cubicweb/tests/test_core.py
--- a/pyramid_cubicweb/core.py	Wed Apr 29 13:09:06 2015 +0200
+++ b/pyramid_cubicweb/core.py	Fri May 08 11:38:07 2015 +0200
@@ -248,6 +248,8 @@
                 httpexceptions.HTTPSuccessful,
                 httpexceptions.HTTPRedirection))):
             cnx.rollback()
+        elif cnx._cnx.commit_state == 'uncommitable':
+            cnx.rollback()
         else:
             cnx.commit()
         cnx.__exit__(None, None, None)
--- a/pyramid_cubicweb/tests/test_core.py	Wed Apr 29 13:09:06 2015 +0200
+++ b/pyramid_cubicweb/tests/test_core.py	Fri May 08 11:38:07 2015 +0200
@@ -2,6 +2,7 @@
 
 from cubicweb.view import View
 from cubicweb.web import Redirect
+from cubicweb import ValidationError
 
 
 class Redirector(View):
@@ -12,9 +13,22 @@
         raise Redirect('http://example.org')
 
 
+def put_in_uncommitable_state(request):
+    try:
+        request.cw_cnx.execute('SET U login NULL WHERE U login "anon"')
+    except ValidationError:
+        pass
+    request.response.body = 'OK'
+    return request.response
+
+
 class CoreTest(PyramidCWTest):
     anonymous_allowed = True
 
+    def includeme(self, config):
+        config.add_route('uncommitable', '/uncommitable')
+        config.add_view(put_in_uncommitable_state, route_name='uncommitable')
+
     def test_cw_to_pyramid_copy_headers_on_redirect(self):
         self.vreg.register(Redirector)
         try:
@@ -23,3 +37,8 @@
             self.assertEqual(res.headers['Cache-Control'], 'no-cache')
         finally:
             self.vreg.unregister(Redirector)
+
+    def test_uncommitable_cnx(self):
+        res = self.webapp.get('/uncommitable')
+        self.assertEqual(res.text, 'OK')
+        self.assertEqual(res.status_int, 200)