|
| 1 | +from odoo.tests.common import TransactionCase |
| 2 | +from odoo.tests import tagged |
| 3 | +from odoo import Command |
| 4 | + |
| 5 | +@tagged("post_install", "-at_install") |
| 6 | +class TestRequiredAttributes(TransactionCase): |
| 7 | + |
| 8 | + @classmethod |
| 9 | + def setUpClass(cls): |
| 10 | + super(TestRequiredAttributes, cls).setUpClass() |
| 11 | + |
| 12 | + cls.test_required_attribute = cls.env["product.attribute"].create({ |
| 13 | + "name": "Required Attribute" |
| 14 | + }) |
| 15 | + cls.test_required_values = cls.env["product.attribute.value"].create([ |
| 16 | + { |
| 17 | + "name": "Required Attribute 1", |
| 18 | + "attribute_id": cls.test_required_attribute.id |
| 19 | + }, |
| 20 | + { |
| 21 | + "name": "Required Attribute 2", |
| 22 | + "attribute_id": cls.test_required_attribute.id |
| 23 | + }, |
| 24 | + { |
| 25 | + "name": "Required Attribute 3", |
| 26 | + "attribute_id": cls.test_required_attribute.id |
| 27 | + } |
| 28 | + ]) |
| 29 | + |
| 30 | + cls.test_attribute = cls.env["product.attribute"].create({ |
| 31 | + "name": "Normal Attribute" |
| 32 | + }) |
| 33 | + cls.test_attribute_values = cls.env["product.attribute.value"].create([ |
| 34 | + { |
| 35 | + "name": "Attribute 1", |
| 36 | + "attribute_id": cls.test_attribute.id |
| 37 | + }, |
| 38 | + { |
| 39 | + "name": "Attribute 2", |
| 40 | + "attribute_id": cls.test_attribute.id |
| 41 | + }, |
| 42 | + { |
| 43 | + "name": "Attribute 3", |
| 44 | + "attribute_id": cls.test_attribute.id |
| 45 | + } |
| 46 | + ]) |
| 47 | + |
| 48 | + cls.category = cls.env['product.category'].create([{ |
| 49 | + 'name': 'Test Category', |
| 50 | + 'show_on_global_info': True, |
| 51 | + 'required_attribute_ids': [Command.set([cls.test_required_attribute.id])] |
| 52 | + }]) |
| 53 | + |
| 54 | + cls.product = cls.env["product.product"].create({ |
| 55 | + "name": "Test Product", |
| 56 | + "categ_id": cls.category.id, |
| 57 | + "attribute_line_ids": [Command.create({ |
| 58 | + "attribute_id": cls.test_required_attribute.id, |
| 59 | + "value_ids": [Command.set(cls.test_required_values.ids)] |
| 60 | + }), Command.create({ |
| 61 | + "attribute_id": cls.test_attribute.id, |
| 62 | + "value_ids": [Command.set(cls.test_attribute_values.ids)] |
| 63 | + })] |
| 64 | + }) |
| 65 | + |
| 66 | + |
| 67 | + cls.sale_order = cls.env["sale.order"].create({"partner_id": cls.env.ref("base.res_partner_1").id}) |
| 68 | + cls.sale_order_line = cls.env["sale.order.line"].create({ |
| 69 | + "order_id": cls.sale_order.id, |
| 70 | + "product_id": cls.product.id |
| 71 | + }) |
| 72 | + |
| 73 | + def test_category_fields(self): |
| 74 | + self.assertTrue(self.category.show_on_global_info, "Category should have 'show_on_global_info' set to True") |
| 75 | + self.assertEqual(len(self.category.required_attribute_ids), 1, "Category should have one required attributes (test_required_attribute)") |
| 76 | + |
| 77 | + def test_product_attributes(self): |
| 78 | + product_attributes = self.product.attribute_line_ids.mapped("attribute_id.name") |
| 79 | + self.assertIn("Required Attribute", product_attributes, "Product should have 'Required Attribute' in attribute") |
| 80 | + self.assertIn("Normal Attribute", product_attributes, "Product should have 'Normal Attribute' in attribute") |
| 81 | + |
| 82 | + def test_sale_order_line_global_info(self): |
| 83 | + if self.sale_order_line.product_id.categ_id.show_on_global_info: |
| 84 | + displayed_attributes = self.sale_order_line.product_id.categ_id.required_attribute_ids.mapped("name") |
| 85 | + self.assertIn("Required Attribute", displayed_attributes, "Global info should show 'Required Attribute'") |
| 86 | + self.assertNotIn("Normal Attribute", displayed_attributes, "Global info should NOT show 'Normal Attribute'") |
0 commit comments