Files
odoo_source/addons/resource/tests/test_utils.py
T
vevi-odoo 1d1dcefc06 [FIX] resource: prevent traceback with false start date
This traceback arises when  the user tries to remove the start date

Steps to produce

1. Install 'resource'
2. Open 'Settings/Technical/resource/Resource Time Off'
3. Create a new record and remove start date

AttributeError
'bool' object has no attribute 'tzinfo'

when the user tries to remove the start date, an error will be
produced because _compute_date_to seems to be computing the date_to
based on the date_from field. when removing the date_from  from calculations
on empty or none

which leads to traceback from here
https://github.com/odoo/odoo/blob/322e7ea19b7c069fdb92d3b86e5615c55489ca21/addons/resource/models/resource_calendar_leaves.py#L54-L59

This commit solves the above issue by computing `date_to` for records that have
`date_from`. Apart from that, this commit also removes `# -*- coding: utf-8 -*-`
from the first line of the modified file.

sentry-4983497879

closes odoo/odoo#163311

X-original-commit: 5032a8ffed10bfbd619c85a266092f129716ceca
Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
2024-04-25 17:06:57 +00:00

85 lines
4.2 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from dateutil.relativedelta import relativedelta
from odoo.fields import Datetime
from odoo.tests.common import TransactionCase
from odoo.osv.expression import normalize_domain
from odoo.addons.resource.models import utils
from odoo.tests import Form
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])
# Testing field mapping 1
self.assertEqual(
[('field4', '!=', 'test')],
normalize_domain(utils.filter_domain_leaf(
['|', ('field1', 'in', [1, 2]), '!', ('field2', '=', False), ('field3', '!=', 'test')],
lambda field: field == 'field3',
field_name_mapping={'field3': 'field4'},
))
)
def test_resource_creation_with_date_from(self):
"""
Test resource creation with a date_from.
AssertError is raised when date_from is not provided.
"""
with self.assertRaises(AssertionError):
with Form(self.env['resource.calendar.leaves']) as res:
res.date_from = False
res.date_to = Datetime.now()
with Form(self.env['resource.calendar.leaves']) as res:
date_from = Datetime.now()
date_to = Datetime.now() + relativedelta(hours=24)
res.date_from = date_from
res.date_to = date_to
self.assertFalse(res.id, 'The resource does not have an id before saving')
res.save()
self.assertTrue(res.id, 'The resource was successfully created')
self.assertEqual(res.date_from, Datetime.to_string(date_from))
self.assertEqual(res.date_to, Datetime.to_string(date_to))