diff options
| author | Sébastien Theys <seb@odoo.com> | 2019-08-12 15:16:10 +0000 |
|---|---|---|
| committer | Sébastien Theys <seb@odoo.com> | 2019-08-20 14:09:01 +0000 |
| commit | 064ebe3b1aa359655f7e4966e682c15cc711e9f9 (patch) | |
| tree | 603b51b77aa2f8dcb5c39922b9fab28ed61d6fe0 /odoo/addons/test_new_api/tests/test_new_fields.py | |
| parent | 72d9167c3af2a18700f607913b420e5a0ddb9a52 (diff) | |
[IMP] fields, test_new_api: add ondelete support for m2m
Each model of the relation can define `ondelete` for its `column2` foreign key.
This way the behavior is similar to m2o where the target of the `ondelete` is
the `comodel` foreign key.
Some models were already defining `ondelete` 'restrict' on m2m fields and after
review it appears the behavior introduced in this commit is the expected
behavior for them.
The 'restrict' from product.product has to be removed however because the rest
of the code needs adaptation now that it is properly restricted. These changes
will be done on a separate PR.
closes odoo/odoo#35654
Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
Diffstat (limited to 'odoo/addons/test_new_api/tests/test_new_fields.py')
| -rw-r--r-- | odoo/addons/test_new_api/tests/test_new_fields.py | 29 |
1 files changed, 29 insertions, 0 deletions
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 af34c5b6027..9b17933397a 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -5,6 +5,7 @@ import base64 from datetime import date, datetime, time import io from PIL import Image +import psycopg2 from odoo import fields from odoo.exceptions import AccessError, UserError @@ -1496,6 +1497,34 @@ class TestX2many(common.TransactionCase): self.assertEqual(field.column1, 'test_new_api_multi_line2_id') self.assertEqual(field.column2, 'test_new_api_multi_tag_id') + def test_10_ondelete_many2many(self): + """Test A can't be deleted when used on the relation.""" + record_a = self.env['test_new_api.model_a'].create({'name': 'a'}) + record_b = self.env['test_new_api.model_b'].create({'name': 'b'}) + record_a.write({ + 'a_restricted_b_ids': [(6, 0, record_b.ids)], + }) + with self.assertRaises(psycopg2.IntegrityError): + with mute_logger('odoo.sql_db'), self.cr.savepoint(): + record_a.unlink() + # Test B is still cascade. + record_b.unlink() + self.assertFalse(record_b.exists()) + + def test_11_ondelete_many2many(self): + """Test B can't be deleted when used on the relation.""" + record_a = self.env['test_new_api.model_a'].create({'name': 'a'}) + record_b = self.env['test_new_api.model_b'].create({'name': 'b'}) + record_a.write({ + 'b_restricted_b_ids': [(6, 0, record_b.ids)], + }) + with self.assertRaises(psycopg2.IntegrityError): + with mute_logger('odoo.sql_db'), self.cr.savepoint(): + record_b.unlink() + # Test A is still cascade. + record_a.unlink() + self.assertFalse(record_a.exists()) + def test_search_many2many(self): """ Tests search on many2many fields. """ tags = self.env['test_new_api.multi.tag'] |
