equal
deleted
inserted
replaced
187 |
187 |
188 Each RepoAccess have it own session. |
188 Each RepoAccess have it own session. |
189 |
189 |
190 A repo access can create three type of object: |
190 A repo access can create three type of object: |
191 |
191 |
192 .. automethod:: cubicweb.testlib.RepoAccess.repo_cnx |
192 .. automethod:: cubicweb.testlib.RepoAccess.cnx |
193 .. automethod:: cubicweb.testlib.RepoAccess.client_cnx |
|
194 .. automethod:: cubicweb.testlib.RepoAccess.web_request |
193 .. automethod:: cubicweb.testlib.RepoAccess.web_request |
195 |
194 |
196 The RepoAccess need to be closed to destroy the associated Session. |
195 The RepoAccess need to be closed to destroy the associated Session. |
197 TestCase usually take care of this aspect for the user. |
196 TestCase usually take care of this aspect for the user. |
198 |
197 |
223 # done during `session_open` hooks |
222 # done during `session_open` hooks |
224 cnx.commit() |
223 cnx.commit() |
225 return session |
224 return session |
226 |
225 |
227 @contextmanager |
226 @contextmanager |
228 def repo_cnx(self): |
227 def cnx(self): |
229 """Context manager returning a server side connection for the user""" |
228 """Context manager returning a server side connection for the user""" |
230 with self._session.new_cnx() as cnx: |
229 with self._session.new_cnx() as cnx: |
231 yield cnx |
230 yield cnx |
232 |
231 |
233 @contextmanager |
232 # aliases for bw compat |
234 def client_cnx(self): |
233 client_cnx = repo_cnx = cnx |
235 """Context manager returning a client side connection for the user""" |
|
236 with repoapi.ClientConnection(self._session) as cnx: |
|
237 yield cnx |
|
238 |
234 |
239 @contextmanager |
235 @contextmanager |
240 def web_request(self, url=None, headers={}, method='GET', **kwargs): |
236 def web_request(self, url=None, headers={}, method='GET', **kwargs): |
241 """Context manager returning a web request pre-linked to a client cnx |
237 """Context manager returning a web request pre-linked to a client cnx |
242 |
238 |
245 req.cnx.commit() |
241 req.cnx.commit() |
246 req.cnx.rolback() |
242 req.cnx.rolback() |
247 """ |
243 """ |
248 req = self.requestcls(self._repo.vreg, url=url, headers=headers, |
244 req = self.requestcls(self._repo.vreg, url=url, headers=headers, |
249 method=method, form=kwargs) |
245 method=method, form=kwargs) |
250 clt_cnx = repoapi.ClientConnection(self._session) |
246 with self._session.new_cnx() as cnx: |
251 req.set_cnx(clt_cnx) |
247 if 'ecache' in cnx.transaction_data: |
252 with clt_cnx: |
248 del cnx.transaction_data['ecache'] |
|
249 req.set_cnx(cnx) |
253 yield req |
250 yield req |
254 |
251 |
255 def close(self): |
252 def close(self): |
256 """Close the session associated to the RepoAccess""" |
253 """Close the session associated to the RepoAccess""" |
257 if self._session is not None: |
254 if self._session is not None: |
347 return clt_cnx |
344 return clt_cnx |
348 |
345 |
349 def _close_cnx(self): |
346 def _close_cnx(self): |
350 """ensure that all cnx used by a test have been closed""" |
347 """ensure that all cnx used by a test have been closed""" |
351 for cnx in list(self._cnxs): |
348 for cnx in list(self._cnxs): |
352 if cnx._open and not cnx._session.closed: |
349 cnx.rollback() |
353 cnx.rollback() |
|
354 cnx.close() |
|
355 self._cnxs.remove(cnx) |
350 self._cnxs.remove(cnx) |
356 |
351 |
357 @property |
352 @property |
358 @deprecated('[3.19] explicitly use RepoAccess object in test instead') |
353 @deprecated('[3.19] explicitly use RepoAccess object in test instead') |
359 def session(self): |
354 def session(self): |
414 self.repo = db_handler.get_repo(startup=True) |
409 self.repo = db_handler.get_repo(startup=True) |
415 # get an admin session (without actual login) |
410 # get an admin session (without actual login) |
416 login = unicode(db_handler.config.default_admin_config['login']) |
411 login = unicode(db_handler.config.default_admin_config['login']) |
417 self.admin_access = self.new_access(login) |
412 self.admin_access = self.new_access(login) |
418 self._admin_session = self.admin_access._session |
413 self._admin_session = self.admin_access._session |
419 self._admin_clt_cnx = repoapi.ClientConnection(self._admin_session) |
414 self._admin_clt_cnx = repoapi.Connection(self._admin_session) |
420 self._cnxs.add(self._admin_clt_cnx) |
415 self._cnxs.add(self._admin_clt_cnx) |
421 self._admin_clt_cnx.__enter__() |
416 self._admin_clt_cnx.__enter__() |
422 self.config.repository = lambda x=None: self.repo |
417 self.config.repository = lambda x=None: self.repo |
423 |
418 |
424 |
419 |
526 MAILBOX[:] = [] # reset mailbox |
521 MAILBOX[:] = [] # reset mailbox |
527 |
522 |
528 def tearDown(self): |
523 def tearDown(self): |
529 # XXX hack until logilab.common.testlib is fixed |
524 # XXX hack until logilab.common.testlib is fixed |
530 if self._admin_clt_cnx is not None: |
525 if self._admin_clt_cnx is not None: |
531 if self._admin_clt_cnx._open: |
|
532 self._admin_clt_cnx.close() |
|
533 self._admin_clt_cnx = None |
526 self._admin_clt_cnx = None |
534 if self._admin_session is not None: |
527 if self._admin_session is not None: |
535 self.repo.close(self._admin_session.sessionid) |
528 self.repo.close(self._admin_session.sessionid) |
536 self._admin_session = None |
529 self._admin_session = None |
537 while self._cleanups: |
530 while self._cleanups: |
974 return req, self.session |
967 return req, self.session |
975 |
968 |
976 def assertAuthSuccess(self, req, origsession, nbsessions=1): |
969 def assertAuthSuccess(self, req, origsession, nbsessions=1): |
977 sh = self.app.session_handler |
970 sh = self.app.session_handler |
978 session = self.app.get_session(req) |
971 session = self.app.get_session(req) |
979 clt_cnx = repoapi.ClientConnection(session) |
972 clt_cnx = repoapi.Connection(session) |
980 req.set_cnx(clt_cnx) |
973 req.set_cnx(clt_cnx) |
981 self.assertEqual(len(self.open_sessions), nbsessions, self.open_sessions) |
974 self.assertEqual(len(self.open_sessions), nbsessions, self.open_sessions) |
982 self.assertEqual(session.login, origsession.login) |
975 self.assertEqual(session.login, origsession.login) |
983 self.assertEqual(session.anonymous_session, False) |
976 self.assertEqual(session.anonymous_session, False) |
984 |
977 |
1261 @nocoverage |
1254 @nocoverage |
1262 def auto_populate(self, how_many): |
1255 def auto_populate(self, how_many): |
1263 """this method populates the database with `how_many` entities |
1256 """this method populates the database with `how_many` entities |
1264 of each possible type. It also inserts random relations between them |
1257 of each possible type. It also inserts random relations between them |
1265 """ |
1258 """ |
1266 with self.admin_access.repo_cnx() as cnx: |
1259 with self.admin_access.cnx() as cnx: |
1267 with cnx.security_enabled(read=False, write=False): |
1260 with cnx.security_enabled(read=False, write=False): |
1268 self._auto_populate(cnx, how_many) |
1261 self._auto_populate(cnx, how_many) |
1269 cnx.commit() |
1262 cnx.commit() |
1270 |
1263 |
1271 def _auto_populate(self, cnx, how_many): |
1264 def _auto_populate(self, cnx, how_many): |