[dbapi] retire repo.execute, which was used by the dbapi

We will retire or reform pieces, mostly of the Repository class, that were related to the old dbapi way of life.

Related to #3933480.

authorAurelien Campeas <aurelien.campeas@logilab.fr>
changeset3b11c536f88c
branchdefault
phasedraft
hiddenyes
parent revision#eb240ad0e41d [shared data] remove get/set_shared_data api
child revision#25e3b7987aca [dbapi] retire repo.entity_metas which was used by the dbapi
files modified by this revision
hooks/test/unittest_hooks.py
server/repository.py
server/test/unittest_repository.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@logilab.fr>
# Date 1401898884 -7200
# Wed Jun 04 18:21:24 2014 +0200
# Node ID 3b11c536f88cd60f399f5ed385b0ecb00d49143f
# Parent eb240ad0e41d11da7363a62b455dd828332facb7
[dbapi] retire repo.execute, which was used by the dbapi

We will retire or reform pieces, mostly of the Repository class,
that were related to the old dbapi way of life.

Related to #3933480.

diff --git a/hooks/test/unittest_hooks.py b/hooks/test/unittest_hooks.py
@@ -144,24 +144,10 @@
1 
2 
3 
4  class UserGroupHooksTC(CubicWebTC):
5 
6 -    def test_user_synchronization(self):
7 -        with self.admin_access.repo_cnx() as cnx:
8 -            self.create_user(cnx, 'toto', password='hop', commit=False)
9 -            self.assertRaises(AuthenticationError,
10 -                              self.repo.connect, u'toto', password='hop')
11 -            cnx.commit()
12 -            cnxid = self.repo.connect(u'toto', password='hop')
13 -            self.assertNotEqual(cnxid, cnx.sessionid)
14 -            cnx.execute('DELETE CWUser X WHERE X login "toto"')
15 -            self.repo.execute(cnxid, 'State X')
16 -            cnx.commit()
17 -            self.assertRaises(BadConnectionId,
18 -                              self.repo.execute, cnxid, 'State X')
19 -
20      def test_user_group_synchronization(self):
21          with self.admin_access.repo_cnx() as cnx:
22              user = cnx.user
23              self.assertEqual(user.groups, set(('managers',)))
24              cnx.execute('SET X in_group G WHERE X eid %s, G name "guests"' % user.eid)
diff --git a/server/repository.py b/server/repository.py
@@ -662,34 +662,10 @@
25 
26      def connect(self, login, **kwargs):
27          """open a new session for a given user and return its sessionid """
28          return self.new_session(login, **kwargs).sessionid
29 
30 -    def execute(self, sessionid, rqlstring, args=None, build_descr=True,
31 -                txid=None):
32 -        """execute a RQL query
33 -
34 -        * rqlstring should be a unicode string or a plain ascii string
35 -        * args the optional parameters used in the query
36 -        * build_descr is a flag indicating if the description should be
37 -          built on select queries
38 -        """
39 -        session = self._get_session(sessionid, setcnxset=True, txid=txid)
40 -        try:
41 -            try:
42 -                rset = self.querier.execute(session, rqlstring, args,
43 -                                            build_descr)
44 -                return rset
45 -            except (ValidationError, Unauthorized, RQLSyntaxError):
46 -                raise
47 -            except Exception:
48 -                # FIXME: check error to catch internal errors
49 -                self.exception('unexpected error while executing %s with %s', rqlstring, args)
50 -                raise
51 -        finally:
52 -            session.free_cnxset()
53 -
54      @deprecated('[3.19] use .entity_metas(sessionid, eid, txid) instead')
55      def describe(self, sessionid, eid, txid=None):
56          """return a tuple `(type, physical source uri, extid, actual source
57          uri)` for the entity of the given `eid`
58 
diff --git a/server/test/unittest_repository.py b/server/test/unittest_repository.py
@@ -90,39 +90,21 @@
59          self.assertRaises(AuthenticationError,
60                            self.repo.connect, self.admlogin)
61          self.assertRaises(AuthenticationError,
62                            self.repo.connect, None)
63 
64 -    def test_execute(self):
65 +    def test_login_upassword_accent(self):
66 +        with self.admin_access.repo_cnx() as cnx:
67 +            cnx.execute('INSERT CWUser X: X login %(login)s, X upassword %(passwd)s, '
68 +                        'X in_group G WHERE G name "users"',
69 +                        {'login': u"barnab�", 'passwd': u"h�h�h�".encode('UTF8')})
70 +            cnx.commit()
71          repo = self.repo
72 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
73 -        repo.execute(cnxid, 'Any X')
74 -        repo.execute(cnxid, 'Any X where X is Personne')
75 -        repo.execute(cnxid, 'Any X where X is Personne, X nom ~= "to"')
76 -        repo.execute(cnxid, 'Any X WHERE X has_text %(text)s', {'text': u'\xe7a'})
77 -        repo.close(cnxid)
78 -
79 -    def test_login_upassword_accent(self):
80 -        repo = self.repo
81 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
82 -        repo.execute(cnxid, 'INSERT CWUser X: X login %(login)s, X upassword %(passwd)s, X in_group G WHERE G name "users"',
83 -                     {'login': u"barnab�", 'passwd': u"h�h�h�".encode('UTF8')})
84 -        repo.commit(cnxid)
85 -        repo.close(cnxid)
86          cnxid = repo.connect(u"barnab�", password=u"h�h�h�".encode('UTF8'))
87          self.assert_(cnxid)
88          repo.close(cnxid)
89 
90 -    def test_rollback_on_commit_error(self):
91 -        cnxid = self.repo.connect(self.admlogin, password=self.admpassword)
92 -        self.repo.execute(cnxid,
93 -                          'INSERT CWUser X: X login %(login)s, X upassword %(passwd)s',
94 -                          {'login': u"tutetute", 'passwd': 'tutetute'})
95 -        self.assertRaises(ValidationError, self.repo.commit, cnxid)
96 -        self.assertFalse(self.repo.execute(cnxid, 'CWUser X WHERE X login "tutetute"'))
97 -        self.repo.close(cnxid)
98 -
99      def test_rollback_on_execute_validation_error(self):
100          class ValidationErrorAfterHook(Hook):
101              __regid__ = 'valerror-after-hook'
102              __select__ = Hook.__select__ & is_instance('CWGroup')
103              events = ('after_update_entity',)
@@ -163,93 +145,18 @@
104      def test_close(self):
105          repo = self.repo
106          cnxid = repo.connect(self.admlogin, password=self.admpassword)
107          self.assert_(cnxid)
108          repo.close(cnxid)
109 -        self.assertRaises(BadConnectionId, repo.execute, cnxid, 'Any X')
110 -
111 -    def test_invalid_cnxid(self):
112 -        self.assertRaises(BadConnectionId, self.repo.execute, 0, 'Any X')
113 -        self.assertRaises(BadConnectionId, self.repo.close, None)
114 
115      def test_check_session(self):
116          repo = self.repo
117          cnxid = repo.connect(self.admlogin, password=self.admpassword)
118          self.assertIsInstance(repo.check_session(cnxid), float)
119          repo.close(cnxid)
120          self.assertRaises(BadConnectionId, repo.check_session, cnxid)
121 
122 -    def test_transaction_base(self):
123 -        repo = self.repo
124 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
125 -        # check db state
126 -        result = repo.execute(cnxid, 'Personne X')
127 -        self.assertEqual(result.rowcount, 0)
128 -        # rollback entity insertion
129 -        repo.execute(cnxid, "INSERT Personne X: X nom 'bidule'")
130 -        result = repo.execute(cnxid, 'Personne X')
131 -        self.assertEqual(result.rowcount, 1)
132 -        repo.rollback(cnxid)
133 -        result = repo.execute(cnxid, 'Personne X')
134 -        self.assertEqual(result.rowcount, 0, result.rows)
135 -        # commit
136 -        repo.execute(cnxid, "INSERT Personne X: X nom 'bidule'")
137 -        repo.commit(cnxid)
138 -        result = repo.execute(cnxid, 'Personne X')
139 -        self.assertEqual(result.rowcount, 1)
140 -        repo.close(cnxid)
141 -
142 -    def test_transaction_base2(self):
143 -        repo = self.repo
144 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
145 -        # rollback relation insertion
146 -        repo.execute(cnxid, "SET U in_group G WHERE U login 'admin', G name 'guests'")
147 -        result = repo.execute(cnxid, "Any U WHERE U in_group G, U login 'admin', G name 'guests'")
148 -        self.assertEqual(result.rowcount, 1)
149 -        repo.rollback(cnxid)
150 -        result = repo.execute(cnxid, "Any U WHERE U in_group G, U login 'admin', G name 'guests'")
151 -        self.assertEqual(result.rowcount, 0, result.rows)
152 -        repo.close(cnxid)
153 -
154 -    def test_transaction_base3(self):
155 -        repo = self.repo
156 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
157 -        # rollback state change which trigger TrInfo insertion
158 -        session = repo._get_session(cnxid)
159 -        user = session.user
160 -        user.cw_adapt_to('IWorkflowable').fire_transition('deactivate')
161 -        rset = repo.execute(cnxid, 'TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': user.eid})
162 -        self.assertEqual(len(rset), 1)
163 -        repo.rollback(cnxid)
164 -        rset = repo.execute(cnxid, 'TrInfo T WHERE T wf_info_for X, X eid %(x)s', {'x': user.eid})
165 -        self.assertEqual(len(rset), 0)
166 -        repo.close(cnxid)
167 -
168 -    def test_close_kill_processing_request(self):
169 -        repo = self.repo
170 -        cnxid = repo.connect(self.admlogin, password=self.admpassword)
171 -        repo.execute(cnxid, 'INSERT CWUser X: X login "toto", X upassword "tutu", X in_group G WHERE G name "users"')
172 -        repo.commit(cnxid)
173 -        lock = threading.Lock()
174 -        lock.acquire()
175 -        # close has to be in the thread due to sqlite limitations
176 -        def close_in_a_few_moment():
177 -            lock.acquire()
178 -            repo.close(cnxid)
179 -        t = threading.Thread(target=close_in_a_few_moment)
180 -        t.start()
181 -        def run_transaction():
182 -            lock.release()
183 -            repo.execute(cnxid, 'DELETE CWUser X WHERE X login "toto"')
184 -            repo.commit(cnxid)
185 -        try:
186 -            with self.assertRaises(SessionClosedError) as cm:
187 -                run_transaction()
188 -            self.assertEqual(str(cm.exception), 'try to access connections set on a closed session %s' % cnxid)
189 -        finally:
190 -            t.join()
191 -
192      def test_initial_schema(self):
193          schema = self.repo.schema
194          # check order of attributes is respected
195          notin = set(('eid', 'is', 'is_instance_of', 'identity',
196                       'creation_date', 'modification_date', 'cwuri',