summaryrefslogtreecommitdiff
path: root/odoo/addons/test_new_api
diff options
context:
space:
mode:
authorAdrian Torres <adt@odoo.com>2021-03-04 07:42:07 +0000
committerAdrian Torres <adt@odoo.com>2021-03-10 13:12:18 +0000
commitf5c7e861ee3ca0cdeb6c2f06d227ada07f923ed0 (patch)
tree2b8eee4e4f16821b040fd67d03e6115d17ba87d6 /odoo/addons/test_new_api
parent2e238f1e0202d65a98177939dac166dd659bb39c (diff)
[FIX] base: properly execute Selection field ondelete actions
With this commit, 2 changes are made to the way that the fields.Selection.ondelete cleanup action works: 1) We manually delete ir.model.fields.selection **before** the deletion of ir.model.fields purposefully to **avoid** SQL CASCADE deletes, as we do not know which selections are deleted in cascade and thus we cannot perform the corresponding ondelete cleanup action (which is implemented within the ORM). 2) In some actions, namely 'set default' and 'set null', we write a "safe" value to the records containing the Selection being deleted, before this commit this would go through the ORM (records.write()) but this is problematic if there's a write override for the record's model that raises an error for the field being written to. With this commit, we first try to go through the ORM but if there's a failure (because of the raise in a write override) then we will bypass the ORM and set it with SQL. opw-2451126 closes odoo/odoo#67253 Signed-off-by: Adrian Torres (adt) <adt@odoo.com>
Diffstat (limited to 'odoo/addons/test_new_api')
-rw-r--r--odoo/addons/test_new_api/models/test_new_api.py23
-rw-r--r--odoo/addons/test_new_api/security/ir.model.access.csv1
-rw-r--r--odoo/addons/test_new_api/tests/test_new_fields.py11
3 files changed, 35 insertions, 0 deletions
diff --git a/odoo/addons/test_new_api/models/test_new_api.py b/odoo/addons/test_new_api/models/test_new_api.py
index df13ab303f9..b3407e40adc 100644
--- a/odoo/addons/test_new_api/models/test_new_api.py
+++ b/odoo/addons/test_new_api/models/test_new_api.py
@@ -1152,6 +1152,29 @@ class SelectionNonStored(models.Model):
], store=False)
+class SelectionRequiredForWriteOverride(models.Model):
+ _name = 'test_new_api.model_selection_required_for_write_override'
+ _description = "Model with required selection field for an extension with write override"
+
+ my_selection = fields.Selection([
+ ('foo', "Foo"),
+ ('bar', "Bar"),
+ ], required=True, default='foo')
+
+
+class SelectionRequiredWithWriteOverride(models.Model):
+ _inherit = 'test_new_api.model_selection_required_for_write_override'
+
+ my_selection = fields.Selection(selection_add=[
+ ('divinity', "Divinity: Original Sin 2"),
+ ], ondelete={'divinity': 'set default'})
+
+ def write(self, vals):
+ if 'my_selection' in vals:
+ raise ValueError("No... no no no")
+ return super().write(vals)
+
+
# Special classes to ensure the correct usage of a shared cache amongst users.
# See the method test_shared_cache_computed_field
class SharedCacheComputeParent(models.Model):
diff --git a/odoo/addons/test_new_api/security/ir.model.access.csv b/odoo/addons/test_new_api/security/ir.model.access.csv
index 09ac7f1fc83..61785a225ea 100644
--- a/odoo/addons/test_new_api/security/ir.model.access.csv
+++ b/odoo/addons/test_new_api/security/ir.model.access.csv
@@ -61,6 +61,7 @@ access_test_new_api_monetary_order_line,access_test_new_api_monetary_order_line,
access_test_new_api_model_selection_base,access_test_new_api_model_selection_base,model_test_new_api_model_selection_base,,1,1,1,1
access_test_new_api_model_selection_required,access_test_new_api_model_selection_required,model_test_new_api_model_selection_required,,1,1,1,1
access_test_new_api_model_selection_non_stored,access_test_new_api_model_selection_non_stored,model_test_new_api_model_selection_non_stored,,1,1,1,1
+access_test_new_api_model_selection_required_for_write_override,access_test_new_api_model_selection_required_for_write_override,model_test_new_api_model_selection_required_for_write_override,,1,1,1,1
access_test_new_api_move,access_test_new_api_move,model_test_new_api_move,,1,1,1,1
access_test_new_api_move_line,access_test_new_api_move_line,model_test_new_api_move_line,,1,1,1,1
access_test_new_api_model_shared_cache_compute_parent,access_test_new_api.model_shared_cache_compute_parent,model_test_new_api_model_shared_cache_compute_parent,,1,1,1,1
diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py
index ed3478af1dd..b3a83605d19 100644
--- a/odoo/addons/test_new_api/tests/test_new_fields.py
+++ b/odoo/addons/test_new_api/tests/test_new_fields.py
@@ -2925,6 +2925,7 @@ class TestSelectionOndelete(common.TransactionCase):
MODEL_BASE = 'test_new_api.model_selection_base'
MODEL_REQUIRED = 'test_new_api.model_selection_required'
MODEL_NONSTORED = 'test_new_api.model_selection_non_stored'
+ MODEL_WRITE_OVERRIDE = 'test_new_api.model_selection_required_for_write_override'
def setUp(self):
super().setUp()
@@ -3063,6 +3064,16 @@ class TestSelectionOndelete(common.TransactionCase):
self._unlink_option(self.MODEL_REQUIRED, 'foo')
self.assertEqual(rec.my_selection, 'foo')
+ @mute_logger('odoo.addons.base.models.ir_model')
+ def test_write_override_selection(self):
+ # test that on override to write that raises an error does not prevent the ondelete
+ # policy from executing and cleaning up what needs to be cleaned up
+ rec = self.env[self.MODEL_WRITE_OVERRIDE].create({'my_selection': 'divinity'})
+ self.assertEqual(rec.my_selection, 'divinity')
+
+ self._unlink_option(self.MODEL_WRITE_OVERRIDE, 'divinity')
+ self.assertEqual(rec.my_selection, 'foo')
+
@common.tagged('selection_ondelete_advanced')
class TestSelectionOndeleteAdvanced(common.TransactionCase):