cubicweb/devtools/testlib.py
changeset 12027 c62c80f20a82
parent 11929 fcbd6b251d81
child 12028 08c866d2f11d
equal deleted inserted replaced
12026:c21b399c9269 12027:c62c80f20a82
   221 
   221 
   222     A repo access can create three type of object:
   222     A repo access can create three type of object:
   223 
   223 
   224     .. automethod:: cubicweb.testlib.RepoAccess.cnx
   224     .. automethod:: cubicweb.testlib.RepoAccess.cnx
   225     .. automethod:: cubicweb.testlib.RepoAccess.web_request
   225     .. automethod:: cubicweb.testlib.RepoAccess.web_request
   226 
       
   227     The RepoAccess need to be closed to destroy the associated Session.
       
   228     TestCase usually take care of this aspect for the user.
       
   229 
       
   230     .. automethod:: cubicweb.testlib.RepoAccess.close
       
   231     """
   226     """
   232 
   227 
   233     def __init__(self, repo, login, requestcls):
   228     def __init__(self, repo, login, requestcls):
   234         self._repo = repo
   229         self._repo = repo
   235         self._login = login
   230         self._login = login
   277                               method=method, form=kwargs)
   272                               method=method, form=kwargs)
   278         with self._session.new_cnx() as cnx:
   273         with self._session.new_cnx() as cnx:
   279             req.set_cnx(cnx)
   274             req.set_cnx(cnx)
   280             yield req
   275             yield req
   281 
   276 
   282     def close(self):
       
   283         """Close the session associated to the RepoAccess"""
       
   284         self._session.close()
       
   285 
       
   286     @contextmanager
   277     @contextmanager
   287     def shell(self):
   278     def shell(self):
   288         from cubicweb.server.migractions import ServerMigrationHelper
   279         from cubicweb.server.migractions import ServerMigrationHelper
   289         with self._session.new_cnx() as cnx:
   280         with self._session.new_cnx() as cnx:
   290             mih = ServerMigrationHelper(None, repo=self._repo, cnx=cnx,
   281             mih = ServerMigrationHelper(None, repo=self._repo, cnx=cnx,
   358         return access
   349         return access
   359 
   350 
   360     def _close_access(self):
   351     def _close_access(self):
   361         while self._open_access:
   352         while self._open_access:
   362             try:
   353             try:
   363                 self._open_access.pop().close()
   354                 self._open_access.pop()
   364             except BadConnectionId:
   355             except BadConnectionId:
   365                 continue  # already closed
   356                 continue  # already closed
   366 
   357 
   367     @property
   358     @property
   368     def session(self):
   359     def session(self):
   456         MAILBOX[:] = []  # reset mailbox
   447         MAILBOX[:] = []  # reset mailbox
   457 
   448 
   458     def tearDown(self):
   449     def tearDown(self):
   459         # XXX hack until logilab.common.testlib is fixed
   450         # XXX hack until logilab.common.testlib is fixed
   460         if self._admin_session is not None:
   451         if self._admin_session is not None:
   461             self._admin_session.close()
       
   462             self._admin_session = None
   452             self._admin_session = None
   463         while self._cleanups:
   453         while self._cleanups:
   464             cleanup, args, kwargs = self._cleanups.pop(-1)
   454             cleanup, args, kwargs = self._cleanups.pop(-1)
   465             cleanup(*args, **kwargs)
   455             cleanup(*args, **kwargs)
   466         self.repo.turn_repo_off()
   456         self.repo.turn_repo_off()