From 8ae6c079bea47ec8176a8d092fba0661ac5ccf7b Mon Sep 17 00:00:00 2001 From: Francois Poizat Date: Thu, 22 Feb 2024 16:25:29 +0100 Subject: [PATCH] fix tests --- .../tests/test_product_mass_addition.py | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/base_product_mass_addition/tests/test_product_mass_addition.py b/base_product_mass_addition/tests/test_product_mass_addition.py index d19337800663..163245b0eadf 100644 --- a/base_product_mass_addition/tests/test_product_mass_addition.py +++ b/base_product_mass_addition/tests/test_product_mass_addition.py @@ -2,11 +2,17 @@ # @author Iván Todorovich # 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): @@ -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({}) @@ -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 @@ -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)