diff options
| author | Adrian Torres <adt@odoo.com> | 2019-11-04 08:50:12 +0000 |
|---|---|---|
| committer | Adrian Torres <adt@odoo.com> | 2019-11-07 08:30:59 +0000 |
| commit | e0b5a0cdb65057d42deabfa9b8b6e588b31f9af5 (patch) | |
| tree | 4803ca1c0307ce734ca577fe14f9920bb1667761 /odoo | |
| parent | ec587297eb9e245db6fe2b6ca1953a5f9bbda27e (diff) | |
[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>
Diffstat (limited to 'odoo')
| -rw-r--r-- | odoo/tests/common.py | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/odoo/tests/common.py b/odoo/tests/common.py index fcf02041518..5fe012528b6 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -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: |
