Files
odoo_source/addons/resource/tests/test_utils.py
T
Hugo Carlier (Huca) 20b8635737 [REF] resource, project: replace the function remove_domain_leaf
The function remove_domain_leaf is currently used for project burdown
chart. Due to an increasing need of such a function in multiple modules
for various functional cases, it has been decided to:
- Move this function in 'resource' that is imported in all the modules
where this function is needed.
- Implement extensive unit test to ensure that it address corner cases.
- Refactor the function to address the corner cases it was not
addressing until now.

The refactored function 'filter_domain_leaf' is used to transform a
given domain to a new domain using only the leaves that verify a given
check (more precisely, the leaves whose first element verify this check). To perform this transformation, the leaves that do not verify
this check are considered as undetermined. All the logical operators
dealing with undetermined leaves are ignored, which means:
- AND(leaf, ?) = leaf
- OR(leaf, ?) = leaf
- AND(? , ?) = ?
- OR(?, ?) = ?
- NOT(?) = ?
If the result of the operation is undetermined, it is returned as an
empty domain ([]).

closes odoo/odoo#105470

Related: odoo/enterprise#33657
Related: odoo/upgrade#4221
Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
2023-01-16 15:43:01 +01:00

50 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests.common import TransactionCase
from odoo.osv.expression import normalize_domain
from odoo.addons.resource.models import utils
class TestExpression(TransactionCase):
def test_filter_domain_leaf(self):
domains = [
['|', ('skills', '=', 1), ('admin', '=', True)],
['|', ('skills', '=', 1), ('admin', '=', True), '|', ('skills', '=', 2), ('admin', '=', True)],
['|', ('skills', '=', 1), ('skills', '=', 2), '|', ('skills', '=', 2), ('admin', '=', True)],
['|', '|', ('skills', '=', 1), ('skills', '=', True), '|', ('skills', '=', 2), ('admin', '=', True)],
['|', '|', ('admin', '=', 1), ('admin', '=', True), '&', ('skills', '=', 2), ('admin', '=', True)],
['|', '|', '!', ('admin', '=', 1), ('admin', '=', True), '!', '&', '!', ('skills', '=', 2), ('admin', '=', True)],
['&', '!', ('skills', '=', 2), ('admin', '=', True)],
[['start_datetime', '<=', '2022-12-17 22:59:59'], ['end_datetime', '>=', '2022-12-10 23:00:00']],
[('admin', '=', 1), ('admin', '=', 1), '|', ('admin', '=', 1), ('admin', '=', 1), ('skills', '=', 2)]
]
fields_to_remove = [['skills'], ['admin', 'skills']]
expected_results = []
expected_results.append([
normalize_domain([('admin', '=', True)]),
normalize_domain([('admin', '=', True), ('admin', '=', True)]),
normalize_domain([('admin', '=', True)]),
normalize_domain([('admin', '=', True)]),
normalize_domain(['|', '|', ('admin', '=', 1), ('admin', '=', True), ('admin', '=', True)]),
normalize_domain(['|', '|', '!', ('admin', '=', 1), ('admin', '=', True), '!', ('admin', '=', True)]),
normalize_domain([('admin', '=', True)]),
normalize_domain([['start_datetime', '<=', '2022-12-17 22:59:59'], ['end_datetime', '>=', '2022-12-10 23:00:00']]),
normalize_domain([('admin', '=', 1), ('admin', '=', 1), '|', ('admin', '=', 1), ('admin', '=', 1)])
])
expected_results.append([
normalize_domain([]),
normalize_domain([]),
normalize_domain([]),
normalize_domain([]),
normalize_domain([]),
normalize_domain([]),
normalize_domain([]),
normalize_domain([['start_datetime', '<=', '2022-12-17 22:59:59'], ['end_datetime', '>=', '2022-12-10 23:00:00']]),
normalize_domain([])
])
for idx, fields in enumerate(fields_to_remove):
results = [normalize_domain(utils.filter_domain_leaf(dom, lambda field: field not in fields)) for dom in domains]
self.assertEqual(results, expected_results[idx])