# Copyright 2025 Criptomart # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) from datetime import datetime, timedelta from odoo.tests.common import TransactionCase class TestProductExtension(TransactionCase): '''Test suite para las extensiones de product.template.''' def setUp(self): super(TestProductExtension, self).setUp() self.product = self.env['product.product'].create({ 'name': 'Test Product', }) self.order = self.env['group.order'].create({ 'name': 'Test Order', 'product_ids': [(4, self.product.id)] }) def test_product_template_group_order_ids_field_exists(self): '''Test que el campo group_order_ids existe en product.template.''' product_template = self.product.product_tmpl_id # El campo debe existir y ser readonly self.assertTrue(hasattr(product_template, 'group_order_ids')) def test_product_group_order_ids_readonly(self): """ Test that group_order_ids is a readonly field """ field = self.env['product.template']._fields['group_order_ids'] self.assertTrue(field.readonly) def test_product_group_order_ids_reverse_lookup(self): """ Test that adding a product to an order reflects in group_order_ids """ related_orders = self.product.product_tmpl_id.group_order_ids self.assertIn(self.order, related_orders) def test_product_group_order_ids_empty_by_default(self): """ Test that a new product has no group orders """ new_product = self.env['product.product'].create({'name': 'New Product'}) self.assertFalse(new_product.product_tmpl_id.group_order_ids) def test_product_group_order_ids_multiple_orders(self): """ Test that group_order_ids can contain multiple orders """ order2 = self.env['group.order'].create({ 'name': 'Test Order 2', 'product_ids': [(4, self.product.id)] }) self.assertIn(self.order, self.product.product_tmpl_id.group_order_ids) self.assertIn(order2, self.product.product_tmpl_id.group_order_ids) def test_product_group_order_ids_empty_after_remove_from_order(self): """ Test that group_order_ids is empty after removing the product from all orders """ self.order.write({'product_ids': [(3, self.product.id)]}) self.assertFalse(self.product.product_tmpl_id.group_order_ids) def test_product_group_order_ids_with_multiple_products(self): """ Test group_order_ids with multiple products in one order """ product2 = self.env['product.product'].create({'name': 'Test Product 2'}) self.order.write({'product_ids': [ (4, self.product.id), (4, product2.id) ]}) self.assertIn(self.order, self.product.product_tmpl_id.group_order_ids) self.assertIn(self.order, product2.product_tmpl_id.group_order_ids) def test_product_with_variants_group_order_ids(self): """ Test that group_order_ids works correctly with product variants """ # Create a product template with two variants product_template = self.env['product.template'].create({ 'name': 'Product with Variants', 'attribute_line_ids': [(0, 0, { 'attribute_id': self.env.ref('product.product_attribute_1').id, 'value_ids': [ (4, self.env.ref('product.product_attribute_value_1').id), (4, self.env.ref('product.product_attribute_value_2').id) ] })] }) variant1 = product_template.product_variant_ids[0] variant2 = product_template.product_variant_ids[1] # Add one variant to an order (store variant id, not template id) order_with_variant = self.env['group.order'].create({ 'name': 'Order with Variant', 'product_ids': [(4, variant1.id)] }) # Check that the order appears in the group_order_ids of the template self.assertIn(order_with_variant, product_template.group_order_ids) # Check that the order also appears for both variants (as it's a related field on template) related_orders_v1 = variant1.product_tmpl_id.group_order_ids related_orders_v2 = variant2.product_tmpl_id.group_order_ids self.assertIn(order_with_variant, related_orders_v1) self.assertIn(order_with_variant, related_orders_v2)