Commit c7c5981b authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[session+test] Stop storing / accessing session when it's not necessary

parent 3c60180481ac
......@@ -21,9 +21,9 @@ This module contains functions to initialize a new repository.
"""
from __future__ import print_function
from contextlib import contextmanager
from pprint import pprint
from logilab.common.decorators import cachedproperty
from logilab.common.testlib import SkipTest
from cubicweb.devtools.testlib import RepoAccess
......@@ -39,7 +39,7 @@ def snippet_key(a):
def check_plan(self, rql, expected, kwargs=None):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
plan = self._prepare_plan(cnx, rql, kwargs)
self.planner.build_plan(plan)
try:
......@@ -138,7 +138,6 @@ from cubicweb.devtools.testlib import BaseTestCase
from cubicweb.devtools.fake import FakeRepo, FakeConfig, FakeRequest, FakeConnection
from cubicweb.server import set_debug, debugged
from cubicweb.server.querier import QuerierHelper
from cubicweb.server.session import Session
from cubicweb.server.sources.rql2sql import SQLGenerator, remove_unused_solutions
class RQLGeneratorTC(BaseTestCase):
......@@ -193,14 +192,10 @@ class RQLGeneratorTC(BaseTestCase):
class BaseQuerierTC(TestCase):
repo = None # set this in concrete class
@cachedproperty
def session(self):
return self._access._session
def setUp(self):
self.o = self.repo.querier
self._access = RepoAccess(self.repo, 'admin', FakeRequest)
self.ueid = self.session.user.eid
self.ueid = self._access._user.eid
assert self.ueid != -1
self.repo._type_cache = {} # clear cache
self.maxeid = self.get_max_eid()
......@@ -208,18 +203,18 @@ class BaseQuerierTC(TestCase):
self._dumb_sessions = []
def get_max_eid(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
return cnx.execute('Any MAX(X)')[0][0]
def cleanup(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
cnx.execute('DELETE Any X WHERE X eid > %s' % self.maxeid)
cnx.commit()
def tearDown(self):
undo_monkey_patch()
self.cleanup()
assert self.session.user.eid != -1
assert self._access._user.eid != -1
def set_debug(self, debug):
set_debug(debug)
......@@ -250,17 +245,17 @@ class BaseQuerierTC(TestCase):
rqlst.solutions = remove_unused_solutions(rqlst, rqlst.solutions, self.repo.schema)[0]
return rqlst
@contextmanager
def user_groups_session(self, *groups):
"""lightweight session using the current user with hi-jacked groups"""
# use self.session.user.eid to get correct owned_by relation, unless explicit eid
with self.session.new_cnx() as cnx:
user_eid = self.session.user.eid
session = Session(self.repo._build_user(cnx, user_eid), self.repo)
session.data['%s-groups' % user_eid] = set(groups)
return session
# use cnx.user.eid to get correct owned_by relation, unless explicit eid
with self._access.cnx() as cnx:
user_eid = cnx.user.eid
cnx.user._cw.data['groups-%s' % user_eid] = set(groups)
yield cnx
def qexecute(self, rql, args=None, build_descr=True):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
try:
return self.o.execute(cnx, rql, args, build_descr)
finally:
......
......@@ -309,7 +309,6 @@ class CubicWebTC(BaseTestCase):
cls.config.mode = 'test'
def __init__(self, *args, **kwargs):
self._admin_session = None
self.repo = None
self._open_access = set()
super(CubicWebTC, self).__init__(*args, **kwargs)
......@@ -340,11 +339,6 @@ class CubicWebTC(BaseTestCase):
except BadConnectionId:
continue # already closed
@property
def session(self):
"""return admin session"""
return self._admin_session
def _init_repo(self):
"""init the repository and connection to it.
"""
......@@ -356,7 +350,6 @@ class CubicWebTC(BaseTestCase):
# get an admin session (without actual login)
login = text_type(db_handler.config.default_admin_config['login'])
self.admin_access = self.new_access(login)
self._admin_session = self.admin_access._session
# config management ########################################################
......@@ -432,9 +425,6 @@ class CubicWebTC(BaseTestCase):
MAILBOX[:] = [] # reset mailbox
def tearDown(self):
# XXX hack until logilab.common.testlib is fixed
if self._admin_session is not None:
self._admin_session = None
while self._cleanups:
cleanup, args, kwargs = self._cleanups.pop(-1)
cleanup(*args, **kwargs)
......
......@@ -96,12 +96,9 @@ class ServerMigrationHelper(MigrationHelper):
assert repo
self.cnx = cnx
self.repo = repo
self.session = cnx.session
elif connect:
self.repo = config.repository()
self.set_cnx()
else:
self.session = None
# no config on shell to a remote instance
if config is not None and (cnx or connect):
repo = self.repo
......@@ -154,7 +151,6 @@ class ServerMigrationHelper(MigrationHelper):
except (KeyboardInterrupt, EOFError):
print('aborting...')
sys.exit(0)
self.session = self.cnx.session
def cube_upgraded(self, cube, version):
self.cmd_set_property('system.version.%s' % cube.lower(),
......
......@@ -291,12 +291,12 @@ class Connection(RequestSessionBase):
@_open_only
def get_schema(self):
"""Return the schema currently used by the repository."""
return self.session.repo.source_defs()
return self.repo.source_defs()
@_open_only
def get_option_value(self, option):
"""Return the value for `option` in the configuration."""
return self.session.repo.get_option_value(option)
return self.repo.get_option_value(option)
# transaction api
......
......@@ -52,7 +52,7 @@ class PostgresFTITC(CubicWebTC):
def test_eid_range(self):
# concurrent allocation of eid ranges
source = self.session.repo.sources_by_uri['system']
source = self.repo.sources_by_uri['system']
range1 = []
range2 = []
def allocate_eid_ranges(session, target):
......
......@@ -120,7 +120,7 @@ class UtilsTC(BaseQuerierTC):
pass
def test_preprocess_1(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
reid = cnx.execute('Any X WHERE X is CWRType, X name "owned_by"')[0][0]
rqlst = self._prepare(cnx, 'Any COUNT(RDEF) WHERE RDEF relation_type X, X eid %(x)s',
{'x': reid})
......@@ -128,7 +128,7 @@ class UtilsTC(BaseQuerierTC):
rqlst.solutions)
def test_preprocess_2(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
teid = cnx.execute("INSERT Tag X: X name 'tag'")[0][0]
#geid = self.execute("CWGroup G WHERE G name 'users'")[0][0]
#self.execute("SET X tags Y WHERE X eid %(t)s, Y eid %(g)s",
......@@ -144,8 +144,7 @@ class UtilsTC(BaseQuerierTC):
text_type(parse(got)))
def test_preprocess_security(self):
s = self.user_groups_session('users')
with s.new_cnx() as cnx:
with self.user_groups_session('users') as cnx:
plan = self._prepare_plan(cnx, 'Any ETN,COUNT(X) GROUPBY ETN '
'WHERE X is ET, ET name ETN')
union = plan.rqlst
......@@ -241,8 +240,7 @@ class UtilsTC(BaseQuerierTC):
self.assertEqual(solutions, [{'X': 'Basket', 'ET': 'CWEType', 'ETN': 'String'}])
def test_preprocess_security_aggregat(self):
s = self.user_groups_session('users')
with s.new_cnx() as cnx:
with self.user_groups_session('users') as cnx:
plan = self._prepare_plan(cnx, 'Any MAX(X)')
union = plan.rqlst
plan.preprocess(union)
......@@ -254,13 +252,13 @@ class UtilsTC(BaseQuerierTC):
['MAX(X)'])
def test_preprocess_nonregr(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
rqlst = self._prepare(cnx, 'Any S ORDERBY SI WHERE NOT S ecrit_par O, S para SI')
self.assertEqual(len(rqlst.solutions), 1)
def test_build_description(self):
# should return an empty result set
rset = self.qexecute('Any X WHERE X eid %(x)s', {'x': self.session.user.eid})
rset = self.qexecute('Any X WHERE X eid %(x)s', {'x': self._access._user.eid})
self.assertEqual(rset.description[0][0], 'CWUser')
rset = self.qexecute('Any 1')
self.assertEqual(rset.description[0][0], 'Int')
......@@ -289,10 +287,9 @@ class UtilsTC(BaseQuerierTC):
self.assertEqual(rset.description[0][0], 'String')
def test_build_descr1(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
rset = cnx.execute('(Any U,L WHERE U login L) UNION '
'(Any G,N WHERE G name N, G is CWGroup)')
# rset.req = self.session
orig_length = len(rset)
rset.rows[0][0] = 9999999
description = manual_build_descr(cnx, rset.syntax_tree(), None, rset.rows)
......@@ -493,7 +490,7 @@ class QuerierTC(BaseQuerierTC):
rset = self.qexecute('DISTINCT Any G WHERE U? in_group G')
self.assertEqual(len(rset), 4)
rset = self.qexecute('DISTINCT Any G WHERE U? in_group G, U eid %(x)s',
{'x': self.session.user.eid})
{'x': self._access._user.eid})
self.assertEqual(len(rset), 4)
def test_select_ambigous_outer_join(self):
......@@ -687,7 +684,7 @@ class QuerierTC(BaseQuerierTC):
self.assertEqual(rset.rows[0][0], 12)
## def test_select_simplified(self):
## ueid = self.session.user.eid
## ueid = self._access._user.eid
## rset = self.qexecute('Any L WHERE %s login L'%ueid)
## self.assertEqual(rset.rows[0][0], 'admin')
## rset = self.qexecute('Any L WHERE %(x)s login L', {'x':ueid})
......@@ -840,7 +837,7 @@ class QuerierTC(BaseQuerierTC):
def test_select_explicit_eid(self):
rset = self.qexecute('Any X,E WHERE X owned_by U, X eid E, U eid %(u)s',
{'u': self.session.user.eid})
{'u': self._access._user.eid})
self.assertTrue(rset)
self.assertEqual(rset.description[0][1], 'Int')
......@@ -891,7 +888,7 @@ class QuerierTC(BaseQuerierTC):
'Password', 'String',
'TZDatetime', 'TZTime',
'Time'])
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
cnx.create_entity('Personne', nom=u'louis', test=True)
self.assertEqual(len(cnx.execute('Any X WHERE X test %(val)s', {'val': True})), 1)
self.assertEqual(len(cnx.execute('Any X WHERE X test TRUE')), 1)
......@@ -936,7 +933,7 @@ class QuerierTC(BaseQuerierTC):
'(Any N,COUNT(X) GROUPBY N ORDERBY 2 WHERE X login N)')
def test_select_union_aggregat_independant_group(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
cnx.execute('INSERT State X: X name "hop"')
cnx.execute('INSERT State X: X name "hop"')
cnx.execute('INSERT Transition X: X name "hop"')
......@@ -1185,8 +1182,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
self.assertEqual(len(rset.rows), 0, rset.rows)
def test_delete_3(self):
s = self.user_groups_session('users')
with s.new_cnx() as cnx:
with self.user_groups_session('users') as cnx:
peid, = self.o.execute(cnx, "INSERT Personne P: P nom 'toto'")[0]
seid, = self.o.execute(cnx, "INSERT Societe S: S nom 'logilab'")[0]
self.o.execute(cnx, "SET P travaille S")
......@@ -1224,7 +1220,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
eeid, = self.qexecute('INSERT Email X: X messageid "<1234>", X subject "test", '
'X sender Y, X recipients Y WHERE Y is EmailAddress')[0]
self.qexecute("DELETE Email X")
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
sqlc = cnx.cnxset.cu
sqlc.execute('SELECT * FROM recipients_relation')
self.assertEqual(len(sqlc.fetchall()), 0)
......@@ -1294,7 +1290,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
self.assertEqual(self.qexecute('Any X WHERE X nom "tutu"').rows, [[peid2]])
def test_update_multiple2(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
ueid = cnx.execute("INSERT CWUser X: X login 'bob', X upassword 'toto'")[0][0]
peid1 = cnx.execute("INSERT Personne Y: Y nom 'turlu'")[0][0]
peid2 = cnx.execute("INSERT Personne Y: Y nom 'tutu'")[0][0]
......@@ -1375,7 +1371,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
self.assertEqual(rset.description, [('CWUser',)])
self.assertRaises(Unauthorized,
self.qexecute, "Any P WHERE X is CWUser, X login 'bob', X upassword P")
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
cursor = cnx.cnxset.cu
cursor.execute("SELECT %supassword from %sCWUser WHERE %slogin='bob'"
% (SQL_PREFIX, SQL_PREFIX, SQL_PREFIX))
......@@ -1387,7 +1383,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
self.assertEqual(rset.description, [('CWUser',)])
def test_update_upassword(self):
with self.session.new_cnx() as cnx:
with self._access.cnx() as cnx:
rset = cnx.execute("INSERT CWUser X: X login 'bob', X upassword %(pwd)s",
{'pwd': 'toto'})
self.assertEqual(rset.description[0][0], 'CWUser')
......@@ -1495,7 +1491,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
'creation_date': '2000/07/03 11:00'})
rset = self.qexecute('Any lower(N) ORDERBY LOWER(N) WHERE X is Tag, X name N,'
'X owned_by U, U eid %(x)s',
{'x':self.session.user.eid})
{'x':self._access._user.eid})
self.assertEqual(rset.rows, [[u'\xe9name0']])
def test_nonregr_description(self):
......@@ -1543,7 +1539,7 @@ Any P1,B,E WHERE P1 identity P2 WITH
self.qexecute('Any X ORDERBY D DESC WHERE X creation_date D')
def test_nonregr_extra_joins(self):
ueid = self.session.user.eid
ueid = self._access._user.eid
teid1 = self.qexecute("INSERT Folder X: X name 'folder1'")[0][0]
teid2 = self.qexecute("INSERT Folder X: X name 'folder2'")[0][0]
neid1 = self.qexecute("INSERT Note X: X para 'note1'")[0][0]
......
......@@ -23,8 +23,9 @@ from cubicweb.web import facet
class InsertAttrRelationTC(CubicWebTC):
def parse(self, query):
rqlst = self.vreg.parse(self.session, query)
select = rqlst.children[0]
with self.admin_access.cnx() as cnx:
rqlst = self.vreg.parse(cnx, query)
rqlst.children[0]
return rqlst
def _generate(self, rqlst, rel, role, attr):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment