[fix] use cnx instead of request
authorNoe Gaumont <ngaumont@logilab.fr>
Fri, 12 Apr 2019 09:22:36 +0200
changeset 198 474c8a28e45b
parent 197 1fcffb09447e
child 199 a793b5493eda
[fix] use cnx instead of request
test/unittest_classification.py
--- a/test/unittest_classification.py	Thu Apr 11 17:59:00 2019 +0200
+++ b/test/unittest_classification.py	Fri Apr 12 09:22:36 2019 +0200
@@ -6,47 +6,50 @@
 class ClassificationHooksTC(CubicWebTC):
 
     def setup_database(self):
-        req = self.request()
-        # classification for CWGroup
-        cwgroup = self.execute('Any U WHERE U is ET, U name "CWGroup"').get_entity(0,0)
-        self.classif1 = self.request().create_entity('Classification', name=u"classif1", classifies=cwgroup)
-        self.kwgroup = req.create_entity('Keyword', name=u'kwgroup', included_in=self.classif1)
-        # classification for CWUser
-        cwuser = self.execute('Any U WHERE U is ET, U name "CWUser"').get_entity(0,0)
-        self.classif2 = self.request().create_entity('Classification', name=u"classif2", classifies=cwuser)
-        self.kwuser = req.create_entity('Keyword', name=u'kwuser', included_in=self.classif2)
+        with self.admin_access.cnx() as cnx:
+            # classification for CWGroup
+            cwgroup = cnx.execute('Any U WHERE U is ET, U name "CWGroup"').one()
+            self.classif1_eid = cnx.create_entity('Classification', name=u"classif1", classifies=cwgroup).eid
+            self.kwgroup_eid = cnx.create_entity('Keyword', name=u'kwgroup', included_in=self.classif1_eid).eid
+            # classification for CWUser
+            cwuser = cnx.execute('Any U WHERE U is ET, U name "CWUser"').one()
+            self.classif2_eid = cnx.create_entity('Classification', name=u"classif2", classifies=cwuser).eid
+            self.kwuser_eid = cnx.create_entity('Keyword', name=u'kwuser', included_in=self.classif2_eid).eid
+            cnx.commit()
 
     def test_application_of_bad_keyword_fails(self):
-        self.execute('SET K applied_to G WHERE G is CWGroup, K eid %(k)s',
-                     {'k':self.kwuser.eid})
-        self.assertRaises(ValidationError, self.commit)
+        with self.admin_access.cnx() as cnx:
+            cnx.execute('SET K applied_to G WHERE G is CWGroup, K eid %(k)s',
+                        {'k': self.kwuser_eid})
+            self.assertRaises(ValidationError, cnx.commit)
 
     def test_creating_a_new_subkeyword_sets_included_in(self):
-        req = self.request()
-        kwgroup2 = req.create_entity('Keyword', name=u'kwgroup2', subkeyword_of=self.kwgroup)
-        self.commit()
-        rset = self.execute('Any N WHERE C name N, K included_in C, K eid %(k)s', {'k':kwgroup2.eid})
-        self.assertEqual(len(rset), 1)
-        self.assertEqual(rset[0][0], 'classif1')
+        with self.admin_access.cnx() as cnx:
+            kwgroup2_eid = cnx.create_entity('Keyword', name=u'kwgroup2', subkeyword_of=self.kwgroup_eid).eid
+            cnx.commit()
+            rset = cnx.execute('Any N WHERE C name N, K included_in C, K eid %(k)s', {'k': kwgroup2_eid})
+            self.assertEqual(len(rset), 1)
+            self.assertEqual(rset[0][0], 'classif1')
 
 
     def test_cannot_create_subkeyword_from_other_classification(self):
-        self.execute('SET K1 subkeyword_of K2 WHERE K1 eid %(k1)s, K2 eid %(k2)s',
-                     {'k1':self.kwgroup.eid, 'k2':self.kwuser.eid})
-        self.assertRaises(ValidationError, self.commit)
+        with self.admin_access.cnx() as cnx:
+            cnx.execute('SET K1 subkeyword_of K2 WHERE K1 eid %(k1)s, K2 eid %(k2)s',
+                        {'k1': self.kwgroup_eid, 'k2': self.kwuser_eid})
+            self.assertRaises(ValidationError, cnx.commit)
 
 
     def test_cannot_create_cycles(self):
-        req = self.request()
-        # direct obvious cycle
-        self.execute('SET K1 subkeyword_of K2 WHERE K1 eid %(k1)s, K2 eid %(k2)s',
-                          {'k1':self.kwgroup.eid, 'k2':self.kwuser.eid})
-        self.assertRaises(ValidationError, self.commit)
-        # testing indirect cycles
-        kwgroup2 =  req.create_entity('Keyword', name=u'kwgroup2', included_in=self.classif1, subkeyword_of=self.kwgroup)
-        self.execute('SET K subkeyword_of K2 WHERE K eid %(k)s, K2 eid %(k2)s',
-                     {'k':self.kwgroup.eid, 'k2':kwgroup2.eid})
-        self.assertRaises(ValidationError, self.commit)
+        with self.admin_access.cnx() as cnx:
+            # direct obvious cycle
+            cnx.execute('SET K1 subkeyword_of K2 WHERE K1 eid %(k1)s, K2 eid %(k2)s',
+                        {'k1': self.kwgroup_eid, 'k2': self.kwuser_eid})
+            self.assertRaises(ValidationError, cnx.commit)
+            # testing indirect cycles
+            kwgroup2_eid = cnx.create_entity('Keyword', name=u'kwgroup2', included_in=self.classif1_eid, subkeyword_of=self.kwgroup_eid).eid
+            cnx.execute('SET K subkeyword_of K2 WHERE K eid %(k)s, K2 eid %(k2)s',
+                        {'k': self.kwgroup_eid, 'k2': kwgroup2_eid})
+            self.assertRaises(ValidationError, cnx.commit)
 
 
 if __name__ == '__main__':