From 3c7db87ade7e99eafb44228bd038fa39429fdabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Fri, 22 Mar 2024 10:23:40 +0100 Subject: [PATCH] [IMP] core: add warning for malformed aggregate specification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _read_group doesn't raise an error when we have an aggregate specification like `order_id.create_date:min`, instead it silently ignores the `.create_date` part. We only add a warning in the stable version to avoid breaking any change. closes odoo/odoo#158777 Signed-off-by: Rémy Voet (ryv) --- .../tests/test_private_read_group.py | 16 +++++++++++----- odoo/models.py | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/odoo/addons/test_read_group/tests/test_private_read_group.py b/odoo/addons/test_read_group/tests/test_private_read_group.py index 5750bdba9ab..82d085d3ae7 100644 --- a/odoo/addons/test_read_group/tests/test_private_read_group.py +++ b/odoo/addons/test_read_group/tests/test_private_read_group.py @@ -312,19 +312,22 @@ class TestPrivateReadGroup(common.TransactionCase): ) def test_malformed_params(self): - Model = self.env['test_read_group.fill_temporal'] + Model = self.env['test_read_group.order.line'] # Test malformed groupby clause with self.assertRaises(ValueError): - Model._read_group([], ['date:bad_granularity']) + Model._read_group([], ['create_date:bad_granularity']) with self.assertRaises(ValueError): - Model._read_group([], ['Other stuff date:week']) + Model._read_group([], ['Other stuff create_date:week']) with self.assertRaises(ValueError): - Model._read_group([], ['date']) # No granularity + Model._read_group([], ['create_date']) # No granularity with self.assertRaises(ValueError): - Model._read_group([], ['"date:week']) + Model._read_group([], ['"create_date:week']) + + with self.assertRaises(ValueError): + Model._read_group([], ['order_id.id']) # Test malformed aggregate clause with self.assertRaises(ValueError): @@ -348,6 +351,9 @@ class TestPrivateReadGroup(common.TransactionCase): with self.assertRaises(ValueError): Model._read_group([], aggregates=['label:sum(value)']) + with self.assertWarns(Warning): + Model._read_group([], aggregates=['order_id.create_date:min']) + # Test malformed having clause with self.assertRaises(ValueError): Model._read_group([], ['value'], having=[('__count', '>')]) diff --git a/odoo/models.py b/odoo/models.py index 5ce03fa100c..d56fc811ca0 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -1954,6 +1954,8 @@ class BaseModel(metaclass=MetaModel): field = self._fields[fname] if func == 'recordset' and not (field.relational or fname == 'id'): raise ValueError(f"Aggregate method {func!r} can be only used on relational field (or id) (for {aggregate_spec!r}).") + if property_name and field.type != 'property': + warnings.warn(f"Ignore the {property_name!r} part of {aggregate_spec!r}, this notation is reserved for the Property field") sql_field = self._field_to_sql(self._table, access_fname, query) sql_expr = READ_GROUP_AGGREGATE[func](self._table, sql_field)