server/test/unittest_session.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 08 Jan 2015 12:22:43 +0100
changeset 10146 78f541f50369
parent 9934 e32964e28a45
child 10345 ef54ea75a642
permissions -rw-r--r--
[doc] remove nonexistent css from theme
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9799
5362310d46da [tests/session] remove unused import
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9478
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5197
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7364
aff846ce1050 missing future import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7350
diff changeset
    18
5193
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    19
from cubicweb.devtools.testlib import CubicWebTC
9799
5362310d46da [tests/session] remove unused import
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9478
diff changeset
    20
from cubicweb.server.session import HOOKS_ALLOW_ALL, HOOKS_DENY_ALL
9934
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
    21
from cubicweb.server import hook
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
    22
from cubicweb.predicates import is_instance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
5193
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    24
class InternalSessionTC(CubicWebTC):
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    25
    def test_dbapi_query(self):
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    26
        session = self.repo.internal_session()
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    27
        self.assertFalse(session.running_dbapi_query)
5197
98dc05495e3e [test] properly close session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5193
diff changeset
    28
        session.close()
5193
c9671feff5e2 [session] no way for queries from an InternalSession to be a dbapi query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    29
9240
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    30
    def test_integrity_hooks(self):
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    31
        with self.repo.internal_session() as session:
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    32
            self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
9411
6aec72169ee1 Disable security hooks for internal sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9240
diff changeset
    33
            self.assertEqual(set(('integrity', 'security')), session.disabled_hook_categories)
9240
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    34
            self.assertEqual(set(), session.enabled_hook_categories)
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    35
            session.commit()
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    36
            self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
9411
6aec72169ee1 Disable security hooks for internal sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9240
diff changeset
    37
            self.assertEqual(set(('integrity', 'security')), session.disabled_hook_categories)
9240
6356adcb652c [server] Make internal sessions not reset 'safe'-ness on first commit
Julien Cristau <julien.cristau@logilab.fr>
parents: 8846
diff changeset
    38
            self.assertEqual(set(), session.enabled_hook_categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    39
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    40
class SessionTC(CubicWebTC):
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    41
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    42
    def test_hooks_control(self):
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    43
        session = self.session
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    44
        # this test check the "old" behavior of session with automatic connection management
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    45
        # close the default cnx, we do nto want it to interfer with the test
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    46
        self.cnx.close()
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    47
        # open a dedicated one
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    48
        session.set_cnx('Some-random-cnx-unrelated-to-the-default-one')
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9020
diff changeset
    49
        # go test go
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    50
        self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    51
        self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    52
        self.assertEqual(set(), session.enabled_hook_categories)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8846
diff changeset
    53
        self.assertEqual(1, len(session._cnxs))
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
    54
        with session.deny_all_hooks_but('metadata'):
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    55
            self.assertEqual(HOOKS_DENY_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    56
            self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    57
            self.assertEqual(set(('metadata',)), session.enabled_hook_categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    58
            session.commit()
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    59
            self.assertEqual(HOOKS_DENY_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    60
            self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    61
            self.assertEqual(set(('metadata',)), session.enabled_hook_categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    62
            session.rollback()
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    63
            self.assertEqual(HOOKS_DENY_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    64
            self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    65
            self.assertEqual(set(('metadata',)), session.enabled_hook_categories)
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
    66
            with session.allow_all_hooks_but('integrity'):
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    67
                self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    68
                self.assertEqual(set(('integrity',)), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    69
                self.assertEqual(set(('metadata',)), session.enabled_hook_categories) # not changed in such case
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    70
            self.assertEqual(HOOKS_DENY_ALL, session.hooks_mode)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    71
            self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    72
            self.assertEqual(set(('metadata',)), session.enabled_hook_categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    73
        # leaving context manager with no transaction running should reset the
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7364
diff changeset
    74
        # transaction local storage (and associated cnxset)
9020
cb87e831c183 rename server.session.transaction into server.session.connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8846
diff changeset
    75
        self.assertEqual({}, session._cnxs)
8846
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    76
        self.assertEqual(None, session.cnxset)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    77
        self.assertEqual(HOOKS_ALLOW_ALL, session.hooks_mode, session.HOOKS_ALLOW_ALL)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    78
        self.assertEqual(set(), session.disabled_hook_categories)
f31e2d6f1b53 [test] use the right order in some assertEqual
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8771
diff changeset
    79
        self.assertEqual(set(), session.enabled_hook_categories)
7350
c2452cd57026 [session] enhance session's transaction storage handling to fix cases where commit/rollback is done while in the context of hooks_control/security_enabled managers. Closes #1412648: commit or rollback during postcreate reset hooks control state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    80
9934
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
    81
    def test_explicit_connection(self):
9110
ed8b383d94fd [session] add a new_cnx factory
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9109
diff changeset
    82
        with self.session.new_cnx() as cnx:
9109
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    83
            rset = cnx.execute('Any X LIMIT 1 WHERE X is CWUser')
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    84
            self.assertEqual(1, len(rset))
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    85
            user = rset.get_entity(0, 0)
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    86
            user.cw_delete()
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    87
            cnx.rollback()
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    88
            new_user = cnx.entity_from_eid(user.eid)
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
    89
            self.assertIsNotNone(new_user.login)
9113
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    90
        self.assertFalse(cnx._open)
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    91
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    92
    def test_internal_cnx(self):
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    93
        with self.repo.internal_cnx() as cnx:
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    94
            rset = cnx.execute('Any X LIMIT 1 WHERE X is CWUser')
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    95
            self.assertEqual(1, len(rset))
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    96
            user = rset.get_entity(0, 0)
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    97
            user.cw_delete()
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    98
            cnx.rollback()
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
    99
            new_user = cnx.entity_from_eid(user.eid)
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
   100
            self.assertIsNotNone(new_user.login)
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9110
diff changeset
   101
        self.assertFalse(cnx._open)
9109
499db4fd03f8 [connection] allow simple instantiation of standalone Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9070
diff changeset
   102
9934
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   103
    def test_connection_exit(self):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   104
        """exiting a connection should roll back the transaction, including any
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   105
        pending operations"""
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   106
        self.rollbacked = False
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   107
        class RollbackOp(hook.Operation):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   108
            _test = self
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   109
            def rollback_event(self):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   110
                self._test.rollbacked = True
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   111
        class RollbackHook(hook.Hook):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   112
            __regid__ = 'rollback'
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   113
            events = ('after_update_entity',)
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   114
            __select__ = hook.Hook.__select__ & is_instance('CWGroup')
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   115
            def __call__(self):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   116
                RollbackOp(self._cw)
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   117
        with self.temporary_appobjects(RollbackHook):
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   118
            with self.admin_access.client_cnx() as cnx:
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   119
                cnx.execute('SET G name "foo" WHERE G is CWGroup, G name "managers"')
e32964e28a45 [session] call rollback in Connection.__exit__
Julien Cristau <julien.cristau@logilab.fr>
parents: 9799
diff changeset
   120
            self.assertTrue(self.rollbacked)
7730
8d0edec31aaf [querier] we should not insert None but skip the line when some unknown eid is detected (closes #1892491)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7398
diff changeset
   121
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
if __name__ == '__main__':
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8554
diff changeset
   123
    from logilab.common.testlib import unittest_main
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    unittest_main()