[IMP] tracking_manager: Test compatibility

Previously the bank_ids field was used but account adds messages in partner when
a related bank account is created/modified/deleted, causing tests to fail.

The user_ids field of res.partner is now used to avoid the side effect in tests
and the fake model is removed.

TT50571
pull/3020/head
Víctor Martínez 2024-08-23 12:09:36 +02:00
parent cd62db982c
commit 92b022e2a7
2 changed files with 52 additions and 64 deletions

View File

@ -1,13 +0,0 @@
# Copyright 2023 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models
class ResPartnerBank(models.Model):
_inherit = "res.partner.bank"
category_ids = fields.Many2many(
comodel_name="res.partner.category", string="Categories"
)

View File

@ -1,24 +1,15 @@
# Copyright 2022 Akretion (https://www.akretion.com). # Copyright 2022 Akretion (https://www.akretion.com).
# @author Kévin Roche <kevin.roche@akretion.com> # @author Kévin Roche <kevin.roche@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo_test_helper import FakeModelLoader
from odoo import Command from odoo import Command
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger
class TestTrackingManager(TransactionCase): class TestTrackingManager(TransactionCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
# Load fake models ->/
cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .models import ResPartnerBank
cls.loader.update_registry((ResPartnerBank,))
cls.partner_categ_1, cls.partner_categ_2, cls.partner_categ_3 = cls.env[ cls.partner_categ_1, cls.partner_categ_2, cls.partner_categ_3 = cls.env[
"res.partner.category" "res.partner.category"
].create( ].create(
@ -31,20 +22,15 @@ class TestTrackingManager(TransactionCase):
cls.partner = cls.env["res.partner"].create( cls.partner = cls.env["res.partner"].create(
{ {
"name": "Foo", "name": "Foo",
"bank_ids": [(Command.CREATE, 0, {"acc_number": "007"})], "user_ids": [(Command.CREATE, 0, {"login": "007"})],
"category_id": [(Command.SET, 0, [cls.partner_categ_1.id])], "category_id": [(Command.SET, 0, [cls.partner_categ_1.id])],
} }
) )
cls.partner_model = cls.env.ref("base.model_res_partner") cls.partner_model = cls.env.ref("base.model_res_partner")
cls._active_tracking(["bank_ids", "category_id"]) cls._active_tracking(["user_ids", "category_id"])
cls.flush_tracking() cls.flush_tracking()
cls.partner.message_ids.unlink() cls.partner.message_ids.unlink()
@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()
@classmethod @classmethod
def _active_tracking(cls, fields_list): def _active_tracking(cls, fields_list):
cls.partner_model.active_custom_tracking = True cls.partner_model.active_custom_tracking = True
@ -56,7 +42,7 @@ class TestTrackingManager(TransactionCase):
return cls.partner_model.field_id.filtered(lambda s: s.name in fields_list) return cls.partner_model.field_id.filtered(lambda s: s.name in fields_list)
def test_not_tracked(self): def test_not_tracked(self):
field = self._get_fields(["name"])[0] field = self._get_fields(["mobile"])[0]
self.assertFalse(field.native_tracking) self.assertFalse(field.native_tracking)
self.assertFalse(field.custom_tracking) self.assertFalse(field.custom_tracking)
@ -66,7 +52,7 @@ class TestTrackingManager(TransactionCase):
self.assertTrue(field.custom_tracking) self.assertTrue(field.custom_tracking)
def test_update_tracked(self): def test_update_tracked(self):
field = self._get_fields(["name"])[0] field = self._get_fields(["mobile"])[0]
self.assertFalse(field.native_tracking) self.assertFalse(field.native_tracking)
self.partner_model.automatic_custom_tracking = True self.partner_model.automatic_custom_tracking = True
self.partner_model.update_custom_tracking() self.partner_model.update_custom_tracking()
@ -127,15 +113,14 @@ class TestTrackingManager(TransactionCase):
self.assertEqual(tracking.new_value_text, "BAR; TOOH") self.assertEqual(tracking.new_value_text, "BAR; TOOH")
def test_o2m_create_indirectly(self): def test_o2m_create_indirectly(self):
self.partner.write( self.partner.write({"user_ids": [(Command.CREATE, 0, {"login": "1234567890"})]})
{"bank_ids": [(Command.CREATE, 0, {"acc_number": "1234567890"})]}
)
self.assertEqual(len(self.messages), 2) self.assertEqual(len(self.messages), 2)
self.assertEqual(self.messages[0].body.count("New"), 1) self.assertEqual(self.messages[0].body.count("New"), 1)
@mute_logger("odoo.models.unlink")
def test_o2m_unlink_indirectly(self): def test_o2m_unlink_indirectly(self):
self.partner.write( self.partner.write(
{"bank_ids": [(Command.DELETE, self.partner.bank_ids[0].id)]} {"user_ids": [(Command.DELETE, self.partner.user_ids[0].id)]}
) )
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertIn("Delete", self.messages.body) self.assertIn("Delete", self.messages.body)
@ -143,8 +128,8 @@ class TestTrackingManager(TransactionCase):
def test_o2m_write_indirectly(self): def test_o2m_write_indirectly(self):
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
(Command.UPDATE, self.partner.bank_ids[0].id, {"acc_number": "123"}) (Command.UPDATE, self.partner.user_ids[0].id, {"login": "123"})
], ],
} }
) )
@ -152,26 +137,27 @@ class TestTrackingManager(TransactionCase):
self.assertIn("Change", self.messages.body) self.assertIn("Change", self.messages.body)
def test_o2m_write_indirectly_on_not_tracked_fields(self): def test_o2m_write_indirectly_on_not_tracked_fields(self):
# Active custom tracking on res.partner.bank and remove tracking on acc_number # Active custom tracking on res.users and remove tracking on login
bank_model = self.env["ir.model"].search([("model", "=", "res.partner.bank")]) res_users_model = self.env["ir.model"].search([("model", "=", "res.users")])
bank_model.active_custom_tracking = True res_users_model.active_custom_tracking = True
acc_number = bank_model.field_id.filtered(lambda x: x.name == "acc_number") login_field = res_users_model.field_id.filtered(lambda x: x.name == "login")
acc_number.custom_tracking = False login_field.custom_tracking = False
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
(Command.UPDATE, self.partner.bank_ids[0].id, {"acc_number": "123"}) (Command.UPDATE, self.partner.user_ids[0].id, {"login": "123"})
], ],
} }
) )
self.assertEqual(len(self.messages), 0) self.assertEqual(len(self.messages), 0)
@mute_logger("odoo.models.unlink")
def test_o2m_create_and_unlink_indirectly(self): def test_o2m_create_and_unlink_indirectly(self):
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
(Command.DELETE, self.partner.bank_ids[0].id, 0), (Command.DELETE, self.partner.user_ids[0].id, 0),
(Command.CREATE, 0, {"acc_number": "1234567890"}), (Command.CREATE, 0, {"login": "1234567890"}),
] ]
} }
) )
@ -180,18 +166,22 @@ class TestTrackingManager(TransactionCase):
self.assertEqual(self.messages.body.count("Delete"), 1) self.assertEqual(self.messages.body.count("Delete"), 1)
def test_o2m_update_m2m_indirectly(self): def test_o2m_update_m2m_indirectly(self):
self.group_extra = self.env["res.groups"].create({"name": "Test group"})
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
( (
Command.UPDATE, Command.UPDATE,
self.partner.bank_ids[0].id, self.partner.user_ids[0].id,
{ {
"category_ids": [ "groups_id": [
( (
6, 6,
0, 0,
[self.partner_categ_1.id, self.partner_categ_2.id], [
self.env.ref("base.group_user").id,
self.group_extra.id,
],
) )
] ]
}, },
@ -205,11 +195,15 @@ class TestTrackingManager(TransactionCase):
def test_o2m_update_m2o_indirectly(self): def test_o2m_update_m2o_indirectly(self):
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
( (
Command.UPDATE, Command.UPDATE,
self.partner.bank_ids[0].id, self.partner.user_ids[0].id,
{"bank_id": self.env["res.bank"].create({"name": "DOO"}).id}, {
"action_id": self.env["ir.actions.actions"]
.create({"name": "test", "type": "ir.actions.act_window"})
.id
},
), ),
] ]
} }
@ -217,6 +211,7 @@ class TestTrackingManager(TransactionCase):
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertEqual(self.messages.body.count("Changed"), 1) self.assertEqual(self.messages.body.count("Changed"), 1)
@mute_logger("odoo.models.unlink")
def test_o2m_write_and_unlink_indirectly(self): def test_o2m_write_and_unlink_indirectly(self):
# when editing a o2m in some special case # when editing a o2m in some special case
# like the computed field amount_tax of purchase order line # like the computed field amount_tax of purchase order line
@ -228,14 +223,14 @@ class TestTrackingManager(TransactionCase):
# and no error # and no error
self.partner.write( self.partner.write(
{ {
"bank_ids": [ "user_ids": [
(Command.UPDATE, self.partner.bank_ids[0].id, {"acc_number": "123"}) (Command.UPDATE, self.partner.user_ids[0].id, {"login": "123"})
], ],
} }
) )
self.partner.write( self.partner.write(
{ {
"bank_ids": [(Command.DELETE, self.partner.bank_ids[0].id, 0)], "user_ids": [(Command.DELETE, self.partner.user_ids[0].id, 0)],
} }
) )
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
@ -243,29 +238,35 @@ class TestTrackingManager(TransactionCase):
self.assertEqual(self.messages.body.count("Delete"), 1) self.assertEqual(self.messages.body.count("Delete"), 1)
def test_o2m_create_directly(self): def test_o2m_create_directly(self):
self.env["res.partner.bank"].create( # Add custom context to prevent message from mail addon
self.env["res.users"].with_context(
mail_create_nolog=True, mail_notrack=True
).create(
{ {
"acc_number": "1234567890", "name": "1234567890",
"login": "1234567890",
"partner_id": self.partner.id, "partner_id": self.partner.id,
} }
) )
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertEqual(self.messages.body.count("New"), 1) self.assertEqual(self.messages.body.count("New"), 1)
@mute_logger("odoo.models.unlink")
def test_o2m_unlink_directly(self): def test_o2m_unlink_directly(self):
self.partner.bank_ids.unlink() self.partner.user_ids.unlink()
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertEqual(self.messages.body.count("Delete"), 1) self.assertEqual(self.messages.body.count("Delete"), 1)
def test_o2m_update_directly(self): def test_o2m_update_directly(self):
self.partner.bank_ids.write({"acc_number": "0987654321"}) self.partner.user_ids.write({"login": "0987654321"})
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertEqual(self.messages.body.count("Change :"), 1) self.assertEqual(self.messages.body.count("Change :"), 1)
@mute_logger("odoo.models.unlink")
def test_o2m_write_and_unlink_directly(self): def test_o2m_write_and_unlink_directly(self):
# see explanation of test_o2m_write_and_unlink_indirectly # see explanation of test_o2m_write_and_unlink_indirectly
self.partner.bank_ids.write({"acc_number": "0987654321"}) self.partner.user_ids.write({"login": "0987654321"})
self.partner.bank_ids.unlink() self.partner.user_ids.unlink()
self.assertEqual(len(self.messages), 1) self.assertEqual(len(self.messages), 1)
self.assertEqual(self.messages.body.count("Change"), 0) self.assertEqual(self.messages.body.count("Change"), 0)
self.assertEqual(self.messages.body.count("Delete"), 1) self.assertEqual(self.messages.body.count("Delete"), 1)