Files
odoo_source/addons/web/tests/test_read_progress_bar.py
T
Yolann Sabaux 3a177c448d [FIX] models: use localized first day of the week when grouping by week
Steps to reproduce:
- Make sure language preference is 'en_US'
- In accounting, in the dashboard click on bills
- Filter 'due_date' by week

Issue: The start day is Monday and should be, for 'en_US', Sunday as it
is the case in the dashboard view in accounting (see appendix).

Cause: The query uses the `date_trunc('week', date)` which in Postgres
retrieves the first day of the week as Monday (ISO week).

Solution: Create an offset in the query depending on the first day of
the locale variable.

Note: the `web/tests/test_read_progress_bar.py` has been modified: since
the default language is 'en_US' there will be an offset of one day.  To
make it less confusing, I used only two anglo-saxons countries so the
day offset is not the variable tested.  (for this matter, pleaser refer
to `test_read_group/tests/test_read_group_process_groupby.py`)

Appendix:
Language (english-US)

		VIEW (per week)			|		DASHBOARD
	___________________________________________________________________
	W23		->	06/05		|	05/29	->	06/04
	W24	06/06	->	06/12		|	06/05 	->	06/11
	W25	06/13	->			|	06/12	->	06/18

		(Monday - Sunday)			(Sunday - Saturday)

Language (french-BE)

		VIEW (per week)			|		DASHBOARD
	___________________________________________________________________
	W22		->	06/05		|	05/30	->	06/05
	W23	06/06	->	06/12		|	06/06 	->	06/12
	W24	06/13	->			|	06/13	->	06/19

		(Monday - Sunday)			(Monday - Sunday)

opw-2747066

closes odoo/odoo#93053

Related: odoo/enterprise#29539
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-02-24 10:05:58 +01:00

160 lines
6.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import common
@common.tagged('post_install', '-at_install')
class TestReadProgressBar(common.TransactionCase):
"""Test for read_progress_bar"""
def setUp(self):
super(TestReadProgressBar, self).setUp()
self.Model = self.env['res.partner']
def test_read_progress_bar_m2m(self):
""" Test that read_progress_bar works with m2m field grouping """
progressbar = {
'field': 'type',
'colors': {
'contact': 'success', 'private': 'danger', 'other': '200',
}
}
result = self.env['res.partner'].read_progress_bar([], 'category_id', progressbar)
# check that it works when grouping by m2m field
self.assertTrue(result)
# check the null group
self.assertIn('False', result)
def test_week_grouping(self):
"""The labels associated to each record in read_progress_bar should match
the ones from read_group, even in edge cases like en_US locale on sundays
"""
context = {"lang": "en_US"}
groupby = "date:week"
self.Model.create({'date': '2021-05-02', 'name': "testWeekGrouping_first"}) # Sunday
self.Model.create({'date': '2021-05-09', 'name': "testWeekGrouping_second"}) # Sunday
progress_bar = {
'field': 'name',
'colors': {
"testWeekGrouping_first": 'success',
"testWeekGrouping_second": 'danger',
}
}
groups = self.Model.with_context(context).read_group(
[('name', "like", "testWeekGrouping%")], fields=['date', 'name'], groupby=[groupby])
progressbars = self.Model.with_context(context).read_progress_bar(
[('name', "like", "testWeekGrouping%")], group_by=groupby, progress_bar=progress_bar)
self.assertEqual(len(groups), 2)
self.assertEqual(len(progressbars), 2)
# format the read_progress_bar result to get a dictionary under this format : {record_name: group_name}
# original format (after read_progress_bar) is : {group_name: {record_name: count}}
pg_groups = {
next(record_name for record_name, count in data.items() if count): group_name
for group_name, data in progressbars.items()
}
self.assertEqual(groups[0][groupby], pg_groups["testWeekGrouping_first"])
self.assertEqual(groups[1][groupby], pg_groups["testWeekGrouping_second"])
def test_simple(self):
model = self.env['ir.model'].create({
'model': 'x_progressbar',
'name': 'progress_bar',
'field_id': [
(0, 0, {
'field_description': 'Country',
'name': 'x_country_id',
'ttype': 'many2one',
'relation': 'res.country',
}),
(0, 0, {
'field_description': 'Date',
'name': 'x_date',
'ttype': 'date',
}),
(0, 0, {
'field_description': 'State',
'name': 'x_state',
'ttype': 'selection',
'selection': "[('foo', 'Foo'), ('bar', 'Bar'), ('baz', 'Baz')]",
}),
],
})
c1, c2, c3 = self.env['res.country'].search([], limit=3)
self.env['x_progressbar'].create([
# week 53 2018 / week 1 2019
{'x_country_id': c1.id, 'x_date': '2019-01-01', 'x_state': 'foo'},
{'x_country_id': c1.id, 'x_date': '2019-01-02', 'x_state': 'foo'},
{'x_country_id': c1.id, 'x_date': '2019-01-03', 'x_state': 'foo'},
{'x_country_id': c1.id, 'x_date': '2019-01-04', 'x_state': 'bar'},
{'x_country_id': c1.id, 'x_date': '2019-01-05', 'x_state': 'baz'},
# week 2 2019
{'x_country_id': c2.id, 'x_date': '2019-01-06', 'x_state': 'foo'},
{'x_country_id': c2.id, 'x_date': '2019-01-07', 'x_state': 'bar'},
{'x_country_id': c2.id, 'x_date': '2019-01-08', 'x_state': 'bar'},
{'x_country_id': c2.id, 'x_date': '2019-01-09', 'x_state': 'baz'},
{'x_country_id': c3.id, 'x_date': '2019-01-10', 'x_state': 'baz'},
{'x_country_id': c3.id, 'x_date': '2019-01-11', 'x_state': 'foo'},
{'x_country_id': c3.id, 'x_date': '2019-01-12', 'x_state': 'foo'},
# week 3 2019
{'x_country_id': c3.id, 'x_date': '2019-01-13', 'x_state': 'baz'},
{'x_country_id': c3.id, 'x_date': '2019-01-14', 'x_state': 'baz'},
{'x_country_id': c3.id, 'x_date': '2019-01-15', 'x_state': 'baz'},
])
progress_bar = {
'field': 'x_state',
'colors': {'foo': 'success', 'bar': 'warning', 'baz': 'danger'},
}
result = self.env['x_progressbar'].read_progress_bar([], 'x_country_id', progress_bar)
self.assertEqual(result, {
c1.display_name: {'foo': 3, 'bar': 1, 'baz': 1},
c2.display_name: {'foo': 1, 'bar': 2, 'baz': 1},
c3.display_name: {'foo': 2, 'bar': 0, 'baz': 4},
})
# check date aggregation and format
result = self.env['x_progressbar'].read_progress_bar([], 'x_date:week', progress_bar)
self.assertEqual(result, {
'W53 2018': {'foo': 3, 'bar': 1, 'baz': 1},
'W2 2019': {'foo': 3, 'bar': 2, 'baz': 2},
'W3 2019': {'foo': 0, 'bar': 0, 'baz': 3},
})
# add a computed field on model
model.write({'field_id': [
(0, 0, {
'field_description': 'Related State',
'name': 'x_state_computed',
'ttype': 'selection',
'selection': "[('foo', 'Foo'), ('bar', 'Bar'), ('baz', 'Baz')]",
'compute': "for rec in self: rec['x_state_computed'] = rec.x_state",
'depends': 'x_state',
'readonly': True,
'store': False,
}),
]})
progress_bar = {
'field': 'x_state_computed',
'colors': {'foo': 'success', 'bar': 'warning', 'baz': 'danger'},
}
result = self.env['x_progressbar'].read_progress_bar([], 'x_country_id', progress_bar)
self.assertEqual(result, {
c1.display_name: {'foo': 3, 'bar': 1, 'baz': 1},
c2.display_name: {'foo': 1, 'bar': 2, 'baz': 1},
c3.display_name: {'foo': 2, 'bar': 0, 'baz': 4},
})
result = self.env['x_progressbar'].read_progress_bar([], 'x_date:week', progress_bar)
self.assertEqual(result, {
# first week is not the same as above, but that seems acceptable...
'W1 2019': {'foo': 3, 'bar': 1, 'baz': 1},
'W2 2019': {'foo': 3, 'bar': 2, 'baz': 2},
'W3 2019': {'foo': 0, 'bar': 0, 'baz': 3},
})