Skip to content

Commit

Permalink
[IMP] Adapt modified function to new ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Feb 28, 2024
1 parent 007ebe1 commit 36c70d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
25 changes: 6 additions & 19 deletions base_product_mass_addition/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ def modified(self, fnames, create=False, before=False):
# Moreover, from a functional perspective, these magic fields aren't really
# modifying the product's data so it doesn't make sense to update its metadata.
#
# We achieve it by reverting the changes made by ``write`` [^1], before [^2]
# reaching any explicit flush [^3] or inverse computation [^4].
#
# [^1]:
# https://github.com/odoo/odoo/blob/3991737a53e75398fcf70b1924525783b54d256b/odoo/models.py#L3778-L3787 # noqa: B950
# [^2]:
# https://github.com/odoo/odoo/blob/3991737a53e75398fcf70b1924525783b54d256b/odoo/models.py#L3882 # noqa: B950
# [^3]:
# https://github.com/odoo/odoo/blob/3991737a53e75398fcf70b1924525783b54d256b/odoo/models.py#L3885 # noqa: B950
# [^4]:
# https://github.com/odoo/odoo/blob/f74434c6f4303650e886d99fb950c763f2d4cc6e/odoo/models.py#L3703 # noqa: B950
#
# Basically, if all we're modifying are quick magic fields, and we don't have
# any other column to flush besides the LOG_ACCESS_COLUMNS, clear it.
quick_fnames = ("qty_to_process", "quick_uom_id")
Expand All @@ -65,13 +53,12 @@ def modified(self, fnames, create=False, before=False):
and fnames
and any(quick_fname in fnames for quick_fname in quick_fnames)
):
for record in self.filtered("id"):
towrite = self.env.all.towrite[self._name]
vals = towrite[record.id]
if not vals: # pragma: no cover
continue
if all(fname in LOG_ACCESS_COLUMNS for fname in vals.keys()):
towrite.pop(record.id)
if all(
field.name in LOG_ACCESS_COLUMNS
for field in self.env.cache.get_dirty_fields()
):
for fname in LOG_ACCESS_COLUMNS:
self.env.cache.clear_dirty_field(self._fields[fname])
return super().modified(fnames, create=create, before=before)

@property
Expand Down
55 changes: 33 additions & 22 deletions base_product_mass_addition/tests/test_product_mass_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
# @author Iván Todorovich <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import datetime

from odoo_test_helper import FakeModelLoader

from odoo.tests.common import TransactionCase


def now():
return datetime.now()


class TestProductMassAddition(TransactionCase):
@classmethod
def setUpClass(cls):
Expand All @@ -16,7 +22,12 @@ def setUpClass(cls):
cls.loader.backup_registry()
from .models.order import ModelOrder, ModelOrderLine

cls.loader.update_registry((ModelOrder, ModelOrderLine))
cls.loader.update_registry(
(
ModelOrder,
ModelOrderLine,
)
)

# Setup data
cls.order = cls.env["model.order"].create({})
Expand All @@ -25,11 +36,6 @@ def setUpClass(cls):
**cls.quick_ctx
)

@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()

def test_quick_line_add(self):
"""Test quick lines are added, updated and removed"""
# Case 1: Create new line
Expand All @@ -47,34 +53,39 @@ def test_quick_line_add(self):

def test_quick_should_not_write_on_product(self):
"""Using quick magic fields shouldn't write on product's metadata"""
user_demo = self.env.ref("base.user_demo")
self.product.write_uid = user_demo
self.assertEqual(self.product.write_uid, user_demo)
# Monkey patch the now method for our testing purpose
# other the now method of cr would return the date of the Transaction
# which is unique for the whole test
self.env.cr.now = now
base_date = self.product.write_date
# Case 1: Updating qty_to_process shouldn't write on products
self.product.qty_to_process = 1.0
self.assertEqual(self.product.write_uid, user_demo)
self.product.qty_to_process = 4.0
self.env["product.product"].flush_model()
self.assertEqual(base_date, self.product.write_date)
after_update_date = self.product.write_date
# Case 2: Updating quick_uom_id shouldn't write on products
self.product.quick_uom_id = self.env.ref("uom.product_uom_categ_unit").uom_ids[
1
]
self.assertEqual(self.product.write_uid, user_demo)
self.env["product.product"].flush_model()
self.assertEqual(after_update_date, self.product.write_date)

def test_quick_should_write_on_product(self):
"""Updating fields that are not magic fields should update
product metadata"""
# Change the product write_uid for testing
user_demo = self.env.ref("base.user_demo")
self.product.write_uid = user_demo
self.assertEqual(self.product.write_uid, user_demo)
# Monkey patch the now method for our testing purpose
# other the now method of cr would return the date of the Transaction
# which is unique for the whole test
self.env.cr.now = now
base_date = self.product.write_date
# Case 1: Updating name field should write on product's metadata
self.product.name = "Testing"
self.assertEqual(self.product.write_uid, self.env.user)
# Change the product write_uid for testing
user_demo = self.env.ref("base.user_demo")
self.product.write_uid = user_demo
self.assertEqual(self.product.write_uid, user_demo)
self.env["product.product"].flush_model()
self.assertNotEqual(base_date, self.product.write_date)
after_update_date = self.product.write_date
# Case 2: Updating qty_to_process and name before flush should
# write on product's metadata
self.product.qty_to_process = 2.0
self.product.name = "Testing 2"
self.assertEqual(self.product.write_uid, self.env.user)
self.env["product.product"].flush_model()
self.assertNotEqual(after_update_date, self.product.write_date)

0 comments on commit 36c70d9

Please sign in to comment.