From 1c85a7b8ebad1867e989fd920bfc5391ce75fff4 Mon Sep 17 00:00:00 2001 From: kobros-tech Date: Wed, 25 Dec 2024 00:15:27 +0300 Subject: [PATCH] [18.0][FIX] web_pivot_computed_measure: fix fake fields in tests --- web_pivot_computed_measure/README.rst | 4 ++ .../readme/CONTRIBUTORS.md | 4 ++ .../static/description/index.html | 4 ++ .../static/src/pivot/pivot_model.esm.js | 67 +++++++++++++------ .../tests/res_users_fake.py | 9 +-- .../tests/test_ui_pivot.py | 35 +++++++++- 6 files changed, 92 insertions(+), 31 deletions(-) diff --git a/web_pivot_computed_measure/README.rst b/web_pivot_computed_measure/README.rst index 5f895abf97cc..250aa9555d41 100644 --- a/web_pivot_computed_measure/README.rst +++ b/web_pivot_computed_measure/README.rst @@ -109,6 +109,10 @@ Contributors - Ernesto Tejeda - Carlos Roca +- `Kencove `__: + + - Mohamed Alkobrosli + Maintainers ----------- diff --git a/web_pivot_computed_measure/readme/CONTRIBUTORS.md b/web_pivot_computed_measure/readme/CONTRIBUTORS.md index 5d3768b54ddf..12a4cec67df2 100644 --- a/web_pivot_computed_measure/readme/CONTRIBUTORS.md +++ b/web_pivot_computed_measure/readme/CONTRIBUTORS.md @@ -3,3 +3,7 @@ - Pedro M. Baeza - Ernesto Tejeda - Carlos Roca + +- [Kencove](https://www.kencove.com/): + - Mohamed Alkobrosli + \ No newline at end of file diff --git a/web_pivot_computed_measure/static/description/index.html b/web_pivot_computed_measure/static/description/index.html index 203a83951fd0..9dc5012c2b0c 100644 --- a/web_pivot_computed_measure/static/description/index.html +++ b/web_pivot_computed_measure/static/description/index.html @@ -460,6 +460,10 @@

Contributors

  • Carlos Roca
  • +
  • Kencove:
      +
    • Mohamed Alkobrosli
    • +
    +
  • diff --git a/web_pivot_computed_measure/static/src/pivot/pivot_model.esm.js b/web_pivot_computed_measure/static/src/pivot/pivot_model.esm.js index 3b1cfe3d4011..4ca4f5647e9f 100644 --- a/web_pivot_computed_measure/static/src/pivot/pivot_model.esm.js +++ b/web_pivot_computed_measure/static/src/pivot/pivot_model.esm.js @@ -1,3 +1,5 @@ +/* global console */ + /* Copyright 2020 Tecnativa - Alexandre Díaz * Copyright 2022 Tecnativa - Carlos Roca * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) */ @@ -216,20 +218,31 @@ patch(PivotModel.prototype, { const afield = toAnalyze.shift(); const fieldDef = this.metaData.fields[afield]; // Need to check if fieldDef exists to avoid problems with __count - if (fieldDef && fieldDef.__computed_id) { - const cm = this._computed_measures.find((item) => { - return item.id === fieldDef.__computed_id; - }); - toAnalyze.push(cm.field1, cm.field2); - const toEnableFields = []; - if (!this.metaData.fields[cm.field1].__computed_id) { - toEnableFields.push(cm.field1); - } - if (!this.metaData.fields[cm.field2].__computed_id) { - toEnableFields.push(cm.field2); + if (fieldDef) { + if (fieldDef.__computed_id) { + const cm = this._computed_measures.find((item) => { + return item.id === fieldDef.__computed_id; + }); + toAnalyze.push(cm.field1, cm.field2); + const toEnableFields = []; + if (!this.metaData.fields[cm.field1].__computed_id) { + toEnableFields.push(cm.field1); + } + if (!this.metaData.fields[cm.field2].__computed_id) { + toEnableFields.push(cm.field2); + } + toEnableFields.push(afield); + toEnable.push(toEnableFields); + } else { + console.warn( + `Field "${fieldDef}" is missing __computed_id`, + fieldDef + ); } - toEnableFields.push(afield); - toEnable.push(toEnableFields); + } else { + console.error( + `Field "${fieldDef}" is undefined in metaData.fields` + ); } } if (toEnable.length) { @@ -275,17 +288,27 @@ patch(PivotModel.prototype, { const fieldNames = Object.keys(this.metaData.fields); for (const fieldName of fieldNames) { const field = this.metaData.fields[fieldName]; - if (field.__computed_id) { - const cm = this._computed_measures.find((item) => { - return item.id === field.__computed_id; - }); - if (!cm) { - delete this.metaData.fields[fieldName]; - delete this.metaData.measures[fieldName]; - this.metaData.activeMeasures = this.metaData.activeMeasures.filter( - (item) => item !== fieldName + if (field) { + if (field.__computed_id) { + const cm = this._computed_measures.find( + (item) => item.id === field.__computed_id + ); + if (!cm) { + delete this.metaData.fields[fieldName]; + delete this.metaData.measures[fieldName]; + this.metaData.activeMeasures = + this.metaData.activeMeasures.filter( + (item) => item !== fieldName + ); + } + } else { + console.warn( + `Field "${fieldName}" is missing __computed_id`, + field ); } + } else { + console.error(`Field "${fieldName}" is undefined in metaData.fields`); } } return _super(...arguments); diff --git a/web_pivot_computed_measure/tests/res_users_fake.py b/web_pivot_computed_measure/tests/res_users_fake.py index 4561b79a9c58..41a13768d1a9 100644 --- a/web_pivot_computed_measure/tests/res_users_fake.py +++ b/web_pivot_computed_measure/tests/res_users_fake.py @@ -1,12 +1,7 @@ -# Copyright 2022 Tecnativa - Carlos Roca -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) - - from odoo import fields, models class ResUsersFake(models.Model): _inherit = "res.users" - - user_year_born = fields.Integer() - user_year_now = fields.Integer() + x_user_year_born = fields.Integer() + x_user_year_now = fields.Integer() diff --git a/web_pivot_computed_measure/tests/test_ui_pivot.py b/web_pivot_computed_measure/tests/test_ui_pivot.py index 1dc01aebdaf7..4f4bdf419279 100644 --- a/web_pivot_computed_measure/tests/test_ui_pivot.py +++ b/web_pivot_computed_measure/tests/test_ui_pivot.py @@ -1,5 +1,6 @@ # Copyright 2022 Tecnativa - Carlos Roca # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + from odoo_test_helper import FakeModelLoader from odoo.tests import common, tagged @@ -10,18 +11,48 @@ class TestUIPivot(common.HttpCase): @classmethod def setUpClass(cls): super().setUpClass() + + # res_users_model_id = ( + # cls.env["ir.model"].search([("model", "=", "res.users")]).id + # ) + # cls.env["ir.model.fields"].create( + # { + # "name": "x_user_year_born", + # "model": "res.users", + # "field_description": "Year Born", + # "ttype": "integer", + # "store": True, + # "model_id": res_users_model_id, + # } + # ) + # cls.env["ir.model.fields"].create( + # { + # "name": "x_user_year_now", + # "model": "res.users", + # "field_description": "Year Now", + # "ttype": "integer", + # "store": True, + # "model_id": res_users_model_id, + # } + # ) + cls.loader = FakeModelLoader(cls.env, cls.__module__) cls.loader.backup_registry() from .res_users_fake import ResUsersFake cls.loader.update_registry((ResUsersFake,)) + + cls.env.registry.setup_models(cls.env.cr) + cls.env.registry.init_models( + cls.env.cr, ["res.users"], {"update_custom_fields": True} + ) cls.env["res.users"].create( { "name": "User 1", "login": "us_1", # Fake fields - "user_year_born": 1998, - "user_year_now": 2022, + "x_user_year_born": 1998, + "x_user_year_now": 2022, } ) # Set pivot view to company action