[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089) stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 13 Apr 2011 09:46:29 +0200
branchstable
changeset 7214 70538ea2532d
parent 7213 7644e68c4e9f
child 7215 2da7f3ec4e6f
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
dataimport.py
devtools/__init__.py
--- a/dataimport.py	Wed Apr 13 08:38:24 2011 +0200
+++ b/dataimport.py	Wed Apr 13 09:46:29 2011 +0200
@@ -581,7 +581,7 @@
                     self.tell(pformat(sorted(error[1])))
 
     def _print_stats(self):
-        nberrors = sum(len(err[1]) for err in self.errors.values())
+        nberrors = sum(len(err) for err in self.errors.values())
         self.tell('\nImport statistics: %i entities, %i types, %i relations and %i errors'
                   % (self.store.nb_inserted_entities,
                      self.store.nb_inserted_types,
--- a/devtools/__init__.py	Wed Apr 13 08:38:24 2011 +0200
+++ b/devtools/__init__.py	Wed Apr 13 09:46:29 2011 +0200
@@ -232,6 +232,11 @@
 
 # XXX merge with BaseApptestConfiguration ?
 class ApptestConfiguration(BaseApptestConfiguration):
+    # `skip_db_create_and_restore` controls wether or not the test database
+    # should be created / backuped / restored. If set to True, those
+    # steps are completely skipped, the database is used as is and is
+    # considered initialized
+    skip_db_create_and_restore = False
 
     def __init__(self, appid, apphome=None,
                  log_threshold=logging.CRITICAL, sourcefile=None):
@@ -260,6 +265,7 @@
               self.view('foaf', rset)
 
     """
+    skip_db_create_and_restore = True
     read_instance_schema = True # read schema from database
 
 
@@ -477,13 +483,38 @@
             cnx.close()
         self.backup_database(test_db_id)
 
+
+class NoCreateDropDatabaseHandler(TestDataBaseHandler):
+    """This handler is used if config.skip_db_create_and_restore is True
+
+    This is typically the case with RealDBConfig. In that case,
+    we explicitely want to skip init / backup / restore phases.
+
+    This handler redefines the three corresponding methods and delegates
+    to original handler for any other method / attribute
+    """
+
+    def __init__(self, base_handler):
+        self.base_handler = base_handler
+
+    # override init / backup / restore methods
+    def init_test_database(self):
+        pass
+
+    def backup_database(self, db_id):
+        pass
+
+    def restore_database(self, db_id):
+        pass
+
+    # delegate to original handler in all other cases
+    def __getattr__(self, attrname):
+        return getattr(self.base_handler, attrname)
+
+
 ### postgres test database handling ############################################
 
 class PostgresTestDataBaseHandler(TestDataBaseHandler):
-
-    # XXX
-    # XXX PostgresTestDataBaseHandler Have not been tested at all.
-    # XXX
     DRIVER = 'postgres'
 
     @property
@@ -504,14 +535,19 @@
     def cursor(self):
         return self.dbcnx.cursor()
 
+    def process_cache_entry(self, directory, dbname, db_id, entry):
+        backup_name = self._backup_name(db_id)
+        if backup_name in self.helper.list_databases(self.cursor):
+            return backup_name
+        return None
+
     def init_test_database(self):
-        """initialize a fresh postgresql databse used for testing purpose"""
+        """initialize a fresh postgresql database used for testing purpose"""
         from cubicweb.server import init_repository
         from cubicweb.server.serverctl import system_source_cnx, createdb
         # connect on the dbms system base to create our base
         try:
             self._drop(self.dbname)
-
             createdb(self.helper, self.system_source, self.dbcnx, self.cursor)
             self.dbcnx.commit()
             cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C',
@@ -555,7 +591,6 @@
 
     def _drop(self, db_name):
         if db_name in self.helper.list_databases(self.cursor):
-            #print 'dropping overwritted database:', db_name
             self.cursor.execute('DROP DATABASE %s' % db_name)
             self.dbcnx.commit()
 
@@ -567,7 +602,6 @@
         orig_name = self.system_source['db-name']
         try:
             backup_name = self._backup_name(db_id)
-            #print 'storing postgres backup as', backup_name
             self._drop(backup_name)
             self.system_source['db-name'] = backup_name
             createdb(self.helper, self.system_source, self.dbcnx, self.cursor, template=orig_name)
@@ -581,7 +615,6 @@
         """Actual restore of the current database.
 
         Use the value tostored in db_cache as input """
-        #print 'restoring postgrest backup from', backup_coordinates
         self._drop(self.dbname)
         createdb(self.helper, self.system_source, self.dbcnx, self.cursor,
                  template=backup_coordinates)
@@ -647,7 +680,6 @@
         # remove database file if it exists ?
         dbfile = self.absolute_dbfile()
         self._cleanup_database(dbfile)
-        #print 'resto from', backup_coordinates
         shutil.copy(backup_coordinates, dbfile)
         repo = self.get_repo()
 
@@ -754,6 +786,8 @@
     handlerkls = HANDLERS.get(driver, None)
     if handlerkls is not None:
         handler = handlerkls(config)
+        if config.skip_db_create_and_restore:
+            handler = NoCreateDropDatabaseHandler(handler)
         HCACHE.set(config, handler)
         return handler
     else: