[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 24 Feb 2010 12:57:30 +0100
branchstable
changeset 4691 ae468fae9965
parent 4690 0cfb6b63f90b
child 4692 11a040e2601c
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
hooks/test/unittest_syncschema.py
server/checkintegrity.py
server/test/unittest_migractions.py
server/test/unittest_multisources.py
server/test/unittest_security.py
test/unittest_cwconfig.py
test/unittest_entity.py
--- a/hooks/test/unittest_syncschema.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/hooks/test/unittest_syncschema.py	Wed Feb 24 12:57:30 2010 +0100
@@ -7,12 +7,37 @@
 
 SCHEMA_EIDS = {}
 class SchemaModificationHooksTC(CubicWebTC):
+    reset_schema = True
 
     @classmethod
     def init_config(cls, config):
         super(SchemaModificationHooksTC, cls).init_config(config)
+        # we have to read schema from the database to get eid for schema entities
         config._cubes = None
         cls.repo.fill_schema()
+        # remember them so we can reread it from the fs instead of the db (too
+        # costly) between tests
+        for x in cls.repo.schema.entities():
+            SCHEMA_EIDS[x] = x.eid
+        for x in cls.repo.schema.relations():
+            SCHEMA_EIDS[x] = x.eid
+            for rdef in x.rdefs.itervalues():
+                SCHEMA_EIDS[(rdef.subject, rdef.rtype, rdef.object)] = rdef.eid
+
+    @classmethod
+    def _refresh_repo(cls):
+        super(SchemaModificationHooksTC, cls)._refresh_repo()
+        # rebuild schema eid index
+        schema = cls.repo.schema
+        for x in schema.entities():
+            x.eid = SCHEMA_EIDS[x]
+            schema._eid_index[x.eid] = x
+        for x in cls.repo.schema.relations():
+            x.eid = SCHEMA_EIDS[x]
+            schema._eid_index[x.eid] = x
+            for rdef in x.rdefs.itervalues():
+                rdef.eid = SCHEMA_EIDS[(rdef.subject, rdef.rtype, rdef.object)]
+                schema._eid_index[rdef.eid] = rdef
 
     def index_exists(self, etype, attr, unique=False):
         self.session.set_pool()
--- a/server/checkintegrity.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/server/checkintegrity.py	Wed Feb 24 12:57:30 2010 +0100
@@ -111,6 +111,8 @@
             pb.update()
     # restore Entity.check
     Entity.check = _check
+    repo.config.disabled_hooks_categories.remove('metadata')
+    repo.config.disabled_hooks_categories.remove('integrity')
 
 
 def check_schema(schema, session, eids, fix=1):
--- a/server/test/unittest_migractions.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/server/test/unittest_migractions.py	Wed Feb 24 12:57:30 2010 +0100
@@ -34,7 +34,7 @@
     @classmethod
     def _refresh_repo(cls):
         super(MigrationCommandsTC, cls)._refresh_repo()
-        cls.repo.schema = cls.vreg.schema = deepcopy(cls.origschema)
+        cls.repo.set_schema(deepcopy(cls.origschema), resetvreg=False)
 
     def setUp(self):
         CubicWebTC.setUp(self)
--- a/server/test/unittest_multisources.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/server/test/unittest_multisources.py	Wed Feb 24 12:57:30 2010 +0100
@@ -30,13 +30,25 @@
 repo2, cnx2 = init_test_database(config=ExternalSource1Configuration('data'))
 repo3, cnx3 = init_test_database(config=ExternalSource2Configuration('data'))
 
-# XXX, access existing connection, no pyro connection
+# hi-jacking
 from cubicweb.server.sources.pyrorql import PyroRQLSource
-PyroRQLSource.get_connection = lambda x: x.uri == 'extern-multi' and cnx3 or cnx2
-# necessary since the repository is closing its initial connections pool though
-# we want to keep cnx2 valid
 from cubicweb.dbapi import Connection
-Connection.close = lambda x: None
+
+PyroRQLSource_get_connection = PyroRQLSource.get_connection
+Connection_close = Connection.close
+
+def setup_module(*args):
+    # hi-jack PyroRQLSource.get_connection to access existing connection (no
+    # pyro connection)
+    PyroRQLSource.get_connection = lambda x: x.uri == 'extern-multi' and cnx3 or cnx2
+    # also necessary since the repository is closing its initial connections
+    # pool though we want to keep cnx2 valid
+    Connection.close = lambda x: None
+
+def teardown_module(*args):
+    PyroRQLSource.get_connection = PyroRQLSource_get_connection
+    Connection.close = Connection_close
+
 
 class TwoSourcesTC(CubicWebTC):
     config = TwoSourcesConfiguration('data')
--- a/server/test/unittest_security.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/server/test/unittest_security.py	Wed Feb 24 12:57:30 2010 +0100
@@ -494,12 +494,13 @@
         # needed to avoid check_perm error
         session.set_pool()
         # needed to remove rql expr granting update perm to the user
+        affaire_perms = self.schema['Affaire'].permissions.copy()
         self.schema['Affaire'].set_action_permissions('update', self.schema['Affaire'].get_groups('update'))
-        self.assertRaises(Unauthorized,
-                          self.schema['Affaire'].check_perm, session, 'update', eid=eid)
-        cu = cnx.cursor()
-        self.schema['Affaire'].set_action_permissions('read', ('users',))
         try:
+            self.assertRaises(Unauthorized,
+                              self.schema['Affaire'].check_perm, session, 'update', eid=eid)
+            cu = cnx.cursor()
+            self.schema['Affaire'].set_action_permissions('read', ('users',))
             aff = cu.execute('Any X WHERE X ref "ARCT01"').get_entity(0, 0)
             aff.fire_transition('abort')
             cnx.commit()
@@ -510,7 +511,9 @@
             # from the current state but Unauthorized if it exists but user can't pass it
             self.assertRaises(ValidationError, user.fire_transition, 'deactivate')
         finally:
-            self.schema['Affaire'].set_action_permissions('read', ('managers',))
+            # restore orig perms
+            for action, perms in affaire_perms.iteritems():
+                self.schema['Affaire'].set_action_permissions(action, perms)
 
     def test_trinfo_security(self):
         aff = self.execute('INSERT Affaire X: X ref "ARCT01"').get_entity(0, 0)
--- a/test/unittest_cwconfig.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/test/unittest_cwconfig.py	Wed Feb 24 12:57:30 2010 +0100
@@ -9,6 +9,7 @@
 import os
 from os.path import dirname, join, abspath
 
+from logilab.common.modutils import cleanup_sys_modules
 from logilab.common.testlib import TestCase, unittest_main
 from logilab.common.changelog import Version
 
@@ -26,6 +27,7 @@
 
 class CubicWebConfigurationTC(TestCase):
     def setUp(self):
+        cleanup_sys_modules([CUSTOM_CUBES_DIR, ApptestConfiguration.CUBES_DIR])
         self.config = ApptestConfiguration('data')
         self.config._cubes = ('email', 'file')
 
--- a/test/unittest_entity.py	Wed Feb 24 11:53:36 2010 +0100
+++ b/test/unittest_entity.py	Wed Feb 24 12:57:30 2010 +0100
@@ -178,9 +178,11 @@
     def test_related_rql_base(self):
         Personne = self.vreg['etypes'].etype_class('Personne')
         Note = self.vreg['etypes'].etype_class('Note')
+        SubNote = self.vreg['etypes'].etype_class('SubNote')
         self.failUnless(issubclass(self.vreg['etypes'].etype_class('SubNote'), Note))
         Personne.fetch_attrs, Personne.fetch_order = fetch_config(('nom', 'type'))
         Note.fetch_attrs, Note.fetch_order = fetch_config(('type',))
+        SubNote.fetch_attrs, SubNote.fetch_order = fetch_config(('type',))
         p = self.request().create_entity('Personne', nom=u'pouet')
         self.assertEquals(p.related_rql('evaluee'),
                           'Any X,AA,AB ORDERBY AA ASC WHERE E eid %(x)s, E evaluee X, '