-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] compute_field_after_install: Group computes by compute function
- Loading branch information
1 parent
c49a4b5
commit b3f5bf3
Showing
2 changed files
with
86 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
# @author Sébastien BEAU <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from unittest.mock import Mock, patch | ||
from contextlib import contextmanager | ||
from unittest.mock import Mock, patch | ||
|
||
from odoo.tests import TransactionCase | ||
from odoo.tools import config | ||
|
@@ -56,6 +56,7 @@ def test_add_fields(self): | |
# Purge field commercial_company_name to simulate | ||
# the installation of a new field | ||
self.env.cr.execute("UPDATE res_partner SET commercial_company_name=null") | ||
self.env.invalidate_all() | ||
|
||
partner = self.env.ref("base.res_partner_address_7") | ||
self.assertFalse(partner.commercial_company_name) | ||
|
@@ -98,6 +99,7 @@ def test_add_fields_batch(self): | |
# Purge field commercial_company_name to simulate | ||
# the installation of a new field | ||
self.env.cr.execute("UPDATE res_partner SET commercial_company_name=null") | ||
self.env.invalidate_all() | ||
|
||
partner = self.env.ref("base.res_partner_address_7") | ||
self.assertFalse(partner.commercial_company_name) | ||
|
@@ -187,3 +189,58 @@ def test_default_step(self): | |
) | ||
self.assertEqual(recompute_field.step, 4000) | ||
|
||
def test_multifields(self): | ||
if "sale.order" not in self.env: | ||
self.skipTest("This test requires the sale module to be installed") | ||
|
||
records = self.env["sale.order"].search([]) | ||
original_id_amounts = { | ||
record.id: (record.amount_untaxed, record.amount_tax, record.amount_total) | ||
for record in records | ||
} | ||
|
||
with patch.dict( | ||
config.options, {"computed_fields_defer_threshold": 1}, clear=True | ||
): | ||
for field in ("amount_untaxed", "amount_tax", "amount_total"): | ||
self.env(context={"module": "fake_module"}).add_to_compute( | ||
records._fields[field], records | ||
) | ||
|
||
# Check that jobs have been created | ||
recompute_fields = self.env["recompute.field"].search( | ||
[ | ||
("model", "=", "sale.order"), | ||
] | ||
) | ||
self.assertEqual(len(recompute_fields), 3) | ||
self.assertEqual(recompute_fields.mapped("state"), ["todo", "todo", "todo"]) | ||
|
||
# Purge field commercial_company_name to simulate | ||
# the installation of a new field | ||
self.env.cr.execute( | ||
"UPDATE sale_order SET amount_untaxed=null, amount_tax=null, amount_total=null" | ||
) | ||
self.env.invalidate_all() | ||
|
||
for record in records: | ||
self.assertFalse(record.amount_untaxed) | ||
self.assertFalse(record.amount_tax) | ||
self.assertFalse(record.amount_total) | ||
|
||
# Run the cron to process computed field | ||
|
||
with self._count_computes( | ||
self.env["sale.order"], | ||
"_compute_amounts", | ||
) as computed: | ||
self.env["recompute.field"]._run_all() | ||
self.assertEqual(computed["records"], len(records)) | ||
self.assertEqual(computed["calls"], 1) | ||
|
||
self.assertEqual(recompute_fields.mapped("state"), ["done", "done", "done"]) | ||
|
||
for record in records: | ||
self.assertEqual(record.amount_untaxed, original_id_amounts[record.id][0]) | ||
self.assertEqual(record.amount_tax, original_id_amounts[record.id][1]) | ||
self.assertEqual(record.amount_total, original_id_amounts[record.id][2]) |