Skip to Content
Menu
This question has been flagged
1 Reply
6998 Views


def test_change_forecasting_config_of_products(self):
"Change forecasting configuration of products"
forecast_group = self.Forecast_Group.search([('name', '=', 'C')])
product_product = self.Product_Product.search([('name', '=', 'Large Cabinet')])
product_product.forecast_group_id = forecast_group # enter the computing method 1st time
self.assertEqual(
[product_product.no_periods, product_product.period_type, product_product.first_date_of_period],
[forecast_group.periods_to_forecast, forecast_group.period_type, forecast_group.first_date])

product_product.auto_update = False # enter the computing method 2nd time

In the above code i am trying to edit the computed filed mannally in a test method

And this is the method using for computing



@api.depends('auto_update',
'forecast_group_id',
'forecast_group_id.period_type',
'forecast_group_id.periods_to_forecast',
'forecast_group_id.first_date')
def _compute_forecasting(self):
# custom prefetch
cache = {
record['id']: {'first_date_of_period': record['first_date_of_period'],'period_type': record['period_type'], 'no_periods': record['no_periods']}
for record in self.read(['first_date_of_period', 'period_type', 'no_periods'])
}# removing this line will make the product variable below return to empty value
for product in self:
if product.auto_update:
product.first_date_of_period = product.forecast_group_id.first_date
product.period_type = product.forecast_group_id.period_type
product.no_periods = product.forecast_group_id.periods_to_forecast

My computed fields:


period_type = fields.Selection(PeriodType.LIST_PERIODS, compute='_compute_forecasting', store=True)
no_periods = fields.Integer(string="Number of Periods", compute='_compute_forecasting', store=True)
first_date_of_period = fields.Datetime(compute='_compute_forecasting', store=True)

From what I know i should not be allowed to change the computed field manually without using inverse method, but when i tested this Odoo did not throw any error. Can anyone explain for me   ?

And there is another question, if i remove the "cache" variable, the second time the computing function triggered the new value of product that is changed in the first time the function is triggered is not return. Why does this happen ?


Avatar
Discard
Best Answer

Hello Dang,

Compute field values are updated when the values of the fields which are in depends of the compute method are changed.

In your test method you can directly call the compute method: record._compute_forecasting()
And then you can test the compute result based on assert condition.

Regards,




Email: odoo@aktivsoftware.com

Skype: kalpeshmaheshwari

   

Avatar
Discard
Related Posts Replies Views Activity
1
May 21
3592
1
Jun 25
15114
3
Apr 25
5198
2
Jul 24
2074