[IMP] tests: use addCleanup and addClassCleanup where useful

This commit takes advantages of the features added in the parent commit
to have better/cleaner tests.

closes odoo/odoo#39368

Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
This commit is contained in:
Adrian Torres
2019-11-07 08:30:59 +00:00
parent ec587297eb
commit e0b5a0cdb6
+21 -32
View File
@@ -510,19 +510,16 @@ class TransactionCase(BaseCase):
def setUp(self):
super(TransactionCase, self).setUp()
self.registry = odoo.registry(get_db_name())
self.addCleanup(self.registry.reset_changes)
self.addCleanup(self.registry.clear_caches)
#: current transaction's cursor
self.cr = self.cursor()
self.addCleanup(self.cr.close)
#: :class:`~odoo.api.Environment` for the current test case
self.env = api.Environment(self.cr, odoo.SUPERUSER_ID, {})
@self.addCleanup
def reset():
# rollback and close the cursor, and reset the environments
self.registry.clear_caches()
self.registry.reset_changes()
self.env.reset()
self.cr.rollback()
self.cr.close()
self.addCleanup(self.env.reset)
self.patch(type(self.env['res.partner']), '_get_gravatar_image', lambda *a: False)
@@ -545,24 +542,20 @@ class SingleTransactionCase(BaseCase):
@classmethod
def setUpClass(cls):
super(SingleTransactionCase, cls).setUpClass()
super().setUpClass()
cls.registry = odoo.registry(get_db_name())
cls.addClassCleanup(cls.registry.clear_caches)
cls.cr = cls.registry.cursor()
cls.addClassCleanup(cls.cr.close)
cls.env = api.Environment(cls.cr, odoo.SUPERUSER_ID, {})
cls.addClassCleanup(cls.env.reset)
def setUp(self):
super(SingleTransactionCase, self).setUp()
self.env.user.flush()
@classmethod
def tearDownClass(cls):
# rollback and close the cursor, and reset the environments
cls.registry.clear_caches()
cls.env.reset()
cls.cr.rollback()
cls.cr.close()
super(SingleTransactionCase, cls).tearDownClass()
savepoint_seq = itertools.count()
class SavepointCase(SingleTransactionCase):
@@ -577,20 +570,20 @@ class SavepointCase(SingleTransactionCase):
the test data either.
"""
def setUp(self):
super(SavepointCase, self).setUp()
self._savepoint_id = next(savepoint_seq)
self.cr.execute('SAVEPOINT test_%d' % self._savepoint_id)
super().setUp()
# restore environments after the test to avoid invoking flush() with an
# invalid environment (inexistent user id) from another test
envs = self.env.all.envs
self.addCleanup(envs.update, list(envs))
self.addCleanup(envs.clear)
def tearDown(self):
self.cr.execute('ROLLBACK TO SAVEPOINT test_%d' % self._savepoint_id)
self.env.clear()
self.registry.clear_caches()
super(SavepointCase, self).tearDown()
self.addCleanup(self.registry.clear_caches)
self.addCleanup(self.env.clear)
self._savepoint_id = next(savepoint_seq)
self.cr.execute('SAVEPOINT test_%d' % self._savepoint_id)
self.addCleanup(self.cr.execute, 'ROLLBACK TO SAVEPOINT test_%d' % self._savepoint_id)
class ChromeBrowserException(Exception):
@@ -1044,6 +1037,7 @@ class HttpCase(TransactionCase):
# start browser on demand
if cls.browser is None:
cls.browser = ChromeBrowser(cls._logger, cls.browser_size, cls.__name__)
cls.addClassCleanup(cls.terminate_browser)
@classmethod
def terminate_browser(cls):
@@ -1051,11 +1045,6 @@ class HttpCase(TransactionCase):
cls.browser.stop()
cls.browser = None
@classmethod
def tearDownClass(cls):
cls.terminate_browser()
super(HttpCase, cls).tearDownClass()
def setUp(self):
super(HttpCase, self).setUp()
if not self.env.registry.loaded: