From e4fc7568c1252bb9fe68f52411f99d132ec3b94f Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 24 Sep 2019 08:45:54 +0000 Subject: [PATCH] [FIX] tests: clean up environments in `SavepointCase` The following issue occurs nondeterministically: a test fails when trying to flush pending updates with a user id that does not exist. This only happens with `SavepointCase` tests. Here is the explanation: the method `cr.savepoint()` retrieves an environment to flush pending updates, and the chosen environment has a user that was created in another test, and thus no longer exists. The problem is that `Environments.envs` contains environments that are no longer supposed to be used. The solution consists in, after the test, discarding the environments that were not present before the test. closes odoo/odoo#37341 Signed-off-by: Raphael Collet (rco) --- odoo/tests/common.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 706626394db..59c4cd334b5 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -493,6 +493,11 @@ class SavepointCase(SingleTransactionCase): super(SavepointCase, self).setUp() self._savepoint_id = next(savepoint_seq) self.cr.execute('SAVEPOINT test_%d' % self._savepoint_id) + # 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)