[test] New Handling of database for test.
This patch adds a new TestDataBaseHandler class.
TestDataBaseHandler are in charge of Setup, backup, restore, connection,
repository caching and cleanup for database used during the test.
TestDataBaseHandler reuse code and logic previously found in cubicweb.devtools
functions and devtools.testlib.CubicwebTC. TestDataBaseHandler is an abstract
class and must be subclassed to implement functionalities specific to each
driver.
TestDataBaseHandler can store and restore various database setups.
devtools.testlib.CubicwebTC gains a test_db_id class attribute to specify that
its TestCase uses a specific database that should be cached. The
pre_setup_database class method is used to setup the database that will be
cached. The setup_database method is kept uncached.
The same TestDataBaseHandler are reused for every test using the same config
object. TestDataBaseHandler try to reuse Repository objects as much as
possible.
All cubicweb test have been updated.
from cubicweb import ValidationError
from cubicweb.selectors import is_instance
from cubicweb.server import hook
class SourceHook(hook.Hook):
__abstract__ = True
category = 'cw.sources'
class SourceAddedOp(hook.Operation):
def precommit_event(self):
self.session.repo.add_source(self.entity)
class SourceAddedHook(SourceHook):
__regid__ = 'cw.sources.added'
__select__ = SourceHook.__select__ & is_instance('CWSource')
events = ('after_add_entity',)
def __call__(self):
SourceAddedOp(self._cw, entity=self.entity)
class SourceRemovedOp(hook.Operation):
def precommit_event(self):
self.session.repo.remove_source(self.uri)
class SourceRemovedHook(SourceHook):
__regid__ = 'cw.sources.removed'
__select__ = SourceHook.__select__ & is_instance('CWSource')
events = ('before_delete_entity',)
def __call__(self):
if self.entity.name == 'system':
raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
SourceRemovedOp(self._cw, uri=self.entity.name)