[testlib] move repo and related attribute back on Instance instead of Class
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 20 Jun 2013 16:00:07 +0200
changeset 9043 97c3bb9a7c99
parent 9042 6cc13a0a9145
child 9044 cfec5cc46008
[testlib] move repo and related attribute back on Instance instead of Class The repo and cnx was hold by the TestCase class to work as a cache and avoid recreation of the repo from scratch of each test. However since bad26a22fe29 the caching is done by the DatabaseHandler object and it is not necessary have a second layer of cache on the TestCase itself. We move the repo and cnx attribute back on the TestCase instance itself and make several class methods instance methods again. This will helps to change dbapi access in test from dbapi to repoapi. related to #2503918
devtools/testlib.py
doc/4.0.rst
server/test/unittest_migractions.py
server/test/unittest_multisources.py
--- a/devtools/testlib.py	Mon Jun 24 14:53:19 2013 +0200
+++ b/devtools/testlib.py	Thu Jun 20 16:00:07 2013 +0200
@@ -178,17 +178,22 @@
     tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
     test_db_id = DEFAULT_EMPTY_DB_ID
     _cnxs = set() # establised connection
-    _cnx  = None  # current connection
+                  # stay on connection for leak detection purpose
+
+    def __init__(self, *args, **kwargs):
+        self._cnx = None  # current connection
+        self.repo = None
+        self.websession = None
+        super(CubicWebTC, self).__init__(*args, **kwargs)
 
     # Too much complicated stuff. the class doesn't need to bear the repo anymore
-    @classmethod
-    def set_cnx(cls, cnx):
-        cls._cnxs.add(cnx)
-        cls._cnx = cnx
+    def set_cnx(self, cnx):
+        self._cnxs.add(cnx)
+        self._cnx = cnx
 
     @property
     def cnx(self):
-        return self.__class__._cnx
+        return self._cnx
 
     @classproperty
     def config(cls):
@@ -237,27 +242,26 @@
             pass
 
     #XXX this doesn't need to a be classmethod anymore
-    @classmethod
-    def _init_repo(cls):
+    def _init_repo(self):
         """init the repository and connection to it.
         """
         # setup configuration for test
-        cls.init_config(cls.config)
+        self.init_config(self.config)
         # get or restore and working db.
-        db_handler = devtools.get_test_db_handler(cls.config)
-        db_handler.build_db_cache(cls.test_db_id, cls.pre_setup_database)
+        db_handler = devtools.get_test_db_handler(self.config)
+        db_handler.build_db_cache(self.test_db_id, self.pre_setup_database)
 
-        cls.repo, cnx = db_handler.get_repo_and_cnx(cls.test_db_id)
+        self.repo, cnx = db_handler.get_repo_and_cnx(self.test_db_id)
         # no direct assignation to cls.cnx anymore.
         # cnx is now an instance property that use a class protected attributes.
-        cls.set_cnx(cnx)
-        cls.websession = dbapi.DBAPISession(cnx, cls.admlogin)
-        cls._orig_cnx = (cnx, cls.websession)
-        cls.config.repository = lambda x=None: cls.repo
+        self.set_cnx(cnx)
+        self.websession = dbapi.DBAPISession(cnx, self.admlogin)
+        self._orig_cnx = (cnx, self.websession)
+        self.config.repository = lambda x=None: self.repo
 
-    @classproperty
-    def vreg(cls):
-        return cls.repo.vreg
+    @property
+    def vreg(self):
+        return self.repo.vreg
 
     def _close_cnx(self):
         for cnx in list(self._cnxs):
--- a/doc/4.0.rst	Mon Jun 24 14:53:19 2013 +0200
+++ b/doc/4.0.rst	Thu Jun 20 16:00:07 2013 +0200
@@ -31,6 +31,10 @@
 
 * ``request.set_session`` no longer takes an optional ``user`` argument.
 
+* CubicwebTC does not have repo and cnx as class attributes anymore. They are
+  standard instance attributes. ``set_cnx`` and ``_init_repo`` class methods
+  become instance methods.
+
 
 Deprecated Code Drops
 ----------------------
--- a/server/test/unittest_migractions.py	Mon Jun 24 14:53:19 2013 +0200
+++ b/server/test/unittest_migractions.py	Thu Jun 20 16:00:07 2013 +0200
@@ -45,19 +45,18 @@
 
     tags = CubicWebTC.tags | Tags(('server', 'migration', 'migractions'))
 
-    @classmethod
-    def _init_repo(cls):
-        super(MigrationCommandsTC, cls)._init_repo()
+    def _init_repo(self):
+        super(MigrationCommandsTC, self)._init_repo()
         # we have to read schema from the database to get eid for schema entities
-        cls.repo.set_schema(cls.repo.deserialize_schema(), resetvreg=False)
+        self.repo.set_schema(self.repo.deserialize_schema(), resetvreg=False)
         # hack to read the schema from data/migrschema
-        config = cls.config
+        config = self.config
         config.appid = join('data', 'migratedapp')
-        config._apphome = cls.datapath('migratedapp')
+        config._apphome = self.datapath('migratedapp')
         global migrschema
         migrschema = config.load_schema()
         config.appid = 'data'
-        config._apphome = cls.datadir
+        config._apphome = self.datadir
         assert 'Folder' in migrschema
 
     def setUp(self):
--- a/server/test/unittest_multisources.py	Mon Jun 24 14:53:19 2013 +0200
+++ b/server/test/unittest_multisources.py	Thu Jun 20 16:00:07 2013 +0200
@@ -70,40 +70,29 @@
     test_db_id= 'cw-server-multisources'
     tags = CubicWebTC.tags | Tags(('multisources'))
 
-    @classmethod
-    def setUpClass(cls):
-        cls._cfg2 = ExternalSource1Configuration('data', apphome=TwoSourcesTC.datadir)
-        cls._cfg3 = ExternalSource2Configuration('data', apphome=TwoSourcesTC.datadir)
+
+    def _init_repo(self):
+        repo2_handler = get_test_db_handler(self._cfg2)
+        repo2_handler.build_db_cache('4cards-1affaire',pre_setup_func=pre_setup_database_extern)
+        self.repo2, self.cnx2 = repo2_handler.get_repo_and_cnx('4cards-1affaire')
+
+        repo3_handler = get_test_db_handler(self._cfg3)
+        repo3_handler.build_db_cache('multisource',pre_setup_func=pre_setup_database_multi)
+        self.repo3, self.cnx3 = repo3_handler.get_repo_and_cnx('multisource')
+
+
+        super(TwoSourcesTC, self)._init_repo()
+
+    def setUp(self):
+        self._cfg2 = ExternalSource1Configuration('data', apphome=TwoSourcesTC.datadir)
+        self._cfg3 = ExternalSource2Configuration('data', apphome=TwoSourcesTC.datadir)
         TestServerConfiguration.no_sqlite_wrap = True
         # hi-jack PyroRQLSource.get_connection to access existing connection (no
         # pyro connection)
-        PyroRQLSource.get_connection = lambda x: x.uri == 'extern-multi' and cls.cnx3 or cls.cnx2
+        PyroRQLSource.get_connection = lambda x: x.uri == 'extern-multi' and self.cnx3 or self.cnx2
         # also necessary since the repository is closing its initial connections
         # pool though we want to keep cnx2 valid
         Connection.close = lambda x: None
-
-    @classmethod
-    def tearDowncls(cls):
-        PyroRQLSource.get_connection = PyroRQLSource_get_connection
-        Connection.close = Connection_close
-        cls.cnx2.close()
-        cls.cnx3.close()
-        TestServerConfiguration.no_sqlite_wrap = False
-
-    @classmethod
-    def _init_repo(cls):
-        repo2_handler = get_test_db_handler(cls._cfg2)
-        repo2_handler.build_db_cache('4cards-1affaire',pre_setup_func=pre_setup_database_extern)
-        cls.repo2, cls.cnx2 = repo2_handler.get_repo_and_cnx('4cards-1affaire')
-
-        repo3_handler = get_test_db_handler(cls._cfg3)
-        repo3_handler.build_db_cache('multisource',pre_setup_func=pre_setup_database_multi)
-        cls.repo3, cls.cnx3 = repo3_handler.get_repo_and_cnx('multisource')
-
-
-        super(TwoSourcesTC, cls)._init_repo()
-
-    def setUp(self):
         CubicWebTC.setUp(self)
         self.addCleanup(self.cnx2.close)
         self.addCleanup(self.cnx3.close)
@@ -112,9 +101,10 @@
     def tearDown(self):
         for source in self.repo.sources[1:]:
             self.repo.remove_source(source.uri)
-        CubicWebTC.tearDown(self)
-        self.cnx2.close()
-        self.cnx3.close()
+        super(TwoSourcesTC, self).tearDown()
+        PyroRQLSource.get_connection = PyroRQLSource_get_connection
+        Connection.close = Connection_close
+        TestServerConfiguration.no_sqlite_wrap = False
         undo_monkey_patch()
 
     @staticmethod