From 138810965297d67609cb458754c9ac5dc7f096be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Fri, 29 Nov 2024 09:06:02 +0100 Subject: [PATCH] [FIX] dms_field: Test compatibility We need to avoid applying a template except when testing functionality with dms_field* modules to avoid the error that a directory with the same name already exists (example: create partner). Related to https://github.com/OCA/dms/pull/378 --- dms_field/models/dms_field_mixin.py | 13 +++++++++++-- dms_field/tests/test_dms_field.py | 17 +++++------------ .../tests/test_dms_field_auto_classification.py | 1 + hr_dms_field/tests/test_hr_dms_field.py | 1 + 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/dms_field/models/dms_field_mixin.py b/dms_field/models/dms_field_mixin.py index e366e1bcb..8c6c3ab13 100644 --- a/dms_field/models/dms_field_mixin.py +++ b/dms_field/models/dms_field_mixin.py @@ -3,6 +3,7 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). from odoo import api, fields, models +from odoo.tools import config class DMSFieldMixin(models.AbstractModel): @@ -30,10 +31,18 @@ def models_to_track_dms_field_template(self): @api.model_create_multi def create(self, vals_list): - """Create a dms directory when creating the record if exist a template.""" + """Create a dms directory when creating the record if exist a template. + We need to avoid applying a template except when testing functionality + with dms_field* modules to avoid the error that a directory with the same + name already exists (example: create partner). + """ result = super().create(vals_list) + test_condition = not config["test_enable"] or self.env.context.get( + "test_dms_field" + ) if ( - not self.env.context.get("skip_track_dms_field_template") + test_condition + and not self.env.context.get("skip_track_dms_field_template") and self._name in self.models_to_track_dms_field_template() ): template = self.env["dms.field.template"].with_context(res_model=self._name) diff --git a/dms_field/tests/test_dms_field.py b/dms_field/tests/test_dms_field.py index 5ea9ef4ed..eb9ce8a4a 100644 --- a/dms_field/tests/test_dms_field.py +++ b/dms_field/tests/test_dms_field.py @@ -4,24 +4,17 @@ from odoo import fields from odoo.exceptions import UserError, ValidationError -from odoo.tests import TransactionCase, new_test_user +from odoo.tests import new_test_user from odoo.tools import mute_logger +from odoo.addons.base.tests.common import BaseCommon -class TestDmsField(TransactionCase): + +class TestDmsField(BaseCommon): @classmethod def setUpClass(cls): super().setUpClass() - cls.env = cls.env( - context=dict( - cls.env.context, - mail_create_nolog=True, - mail_create_nosubscribe=True, - mail_notrack=True, - no_reset_password=True, - tracking_disable=True, - ) - ) + cls.env = cls.env(context=dict(cls.env.context, test_dms_field=True)) cls.user_a = new_test_user(cls.env, login="test-user-a") cls.group = cls.env["res.groups"].create( {"name": "Test group", "users": [(4, cls.user_a.id)]} diff --git a/dms_field_auto_classification/tests/test_dms_field_auto_classification.py b/dms_field_auto_classification/tests/test_dms_field_auto_classification.py index 68aae6a02..9d66aa9ba 100644 --- a/dms_field_auto_classification/tests/test_dms_field_auto_classification.py +++ b/dms_field_auto_classification/tests/test_dms_field_auto_classification.py @@ -14,6 +14,7 @@ class TestDmsFieldAutoClassification(BaseCommon): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, test_dms_field=True)) cls.template = cls.env.ref( "dms_field_auto_classification.dms_classification_template_partners" ) diff --git a/hr_dms_field/tests/test_hr_dms_field.py b/hr_dms_field/tests/test_hr_dms_field.py index d41e2b40e..bf15b814c 100644 --- a/hr_dms_field/tests/test_hr_dms_field.py +++ b/hr_dms_field/tests/test_hr_dms_field.py @@ -11,6 +11,7 @@ class TestHrDmsField(BaseCommon): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, test_dms_field=True)) cls.template = cls.env.ref("hr_dms_field.field_template_employee") cls.storage = cls.template.storage_id cls.access_group = cls.template.group_ids