Skip to content

Commit

Permalink
[18.0][FIX] web_pivot_computed_measure: fix fake fields in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kobros-tech committed Dec 30, 2024
1 parent 48dc591 commit 6512a83
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 15 deletions.
4 changes: 4 additions & 0 deletions web_pivot_computed_measure/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ Contributors
- Ernesto Tejeda
- Carlos Roca

- `Kencove <https://www.kencove.com/>`__:

- Mohamed Alkobrosli

Maintainers
-----------

Expand Down
4 changes: 4 additions & 0 deletions web_pivot_computed_measure/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
- Pedro M. Baeza
- Ernesto Tejeda
- Carlos Roca

- [Kencove](https://www.kencove.com/):
- Mohamed Alkobrosli

4 changes: 4 additions & 0 deletions web_pivot_computed_measure/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Carlos Roca</li>
</ul>
</li>
<li><a class="reference external" href="https://www.kencove.com/">Kencove</a>:<ul>
<li>Mohamed Alkobrosli</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
36 changes: 26 additions & 10 deletions web_pivot_computed_measure/static/src/pivot/pivot_model.esm.js
Original file line number Diff line number Diff line change
@@ -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) */
Expand Down Expand Up @@ -216,7 +218,7 @@ 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) {
if (fieldDef?.__computed_id) {
const cm = this._computed_measures.find((item) => {
return item.id === fieldDef.__computed_id;
});
Expand All @@ -230,6 +232,10 @@ patch(PivotModel.prototype, {
}
toEnableFields.push(afield);
toEnable.push(toEnableFields);
} else {
console.error(
`Field "${fieldDef}" is undefined in metaData.fields`
);
}
}
if (toEnable.length) {
Expand Down Expand Up @@ -275,17 +281,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);
Expand Down
5 changes: 2 additions & 3 deletions web_pivot_computed_measure/tests/res_users_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

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()
35 changes: 33 additions & 2 deletions web_pivot_computed_measure/tests/test_ui_pivot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6512a83

Please sign in to comment.