|
| 1 | +# Copyright 2025 Kencove (http://kencove.com). |
| 2 | +# @author Mohamed Alkobrosli <[email protected]> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +import lxml.etree as ET |
| 6 | + |
| 7 | +from odoo.tests.common import Form, TransactionCase |
| 8 | + |
| 9 | + |
| 10 | +class TestProductAttributeSet(TransactionCase): |
| 11 | + def setUp(self): |
| 12 | + self.ProductTemplate = self.env["product.template"] |
| 13 | + self.view_id = self.env.ref("product.product_template_only_form_view").id |
| 14 | + self.computer = self.env.ref("product_attribute_set.computer_attribute_set") |
| 15 | + self.x_technical_description = self.env.ref( |
| 16 | + "product_attribute_set.computer_tech_description_attribute" |
| 17 | + ) |
| 18 | + super().setUp() |
| 19 | + |
| 20 | + def test_onchange_attribute_set_id(self): |
| 21 | + """Test that onchange on attribute_set_id is triggered and view fields are updated""" |
| 22 | + |
| 23 | + product = None |
| 24 | + |
| 25 | + with self.assertRaises(AssertionError): |
| 26 | + # AssertionError: can't write on invisible field x_technical_description |
| 27 | + with Form(self.ProductTemplate) as f: |
| 28 | + f.name = "Test Product" |
| 29 | + f.x_technical_description = "high quality product" |
| 30 | + f.save() |
| 31 | + with Form(self.ProductTemplate) as f: |
| 32 | + f.name = "Test Product" |
| 33 | + f.attribute_set_id = self.computer |
| 34 | + f.x_technical_description = "high quality product" |
| 35 | + product = f.save() |
| 36 | + # Asserting the field is only writeable when it is visible |
| 37 | + self.assertEqual( |
| 38 | + product.x_technical_description, |
| 39 | + "high quality product", |
| 40 | + ) |
| 41 | + |
| 42 | + views = product.get_views([(self.view_id, "form")], {}) |
| 43 | + arch = views["views"]["form"]["arch"] |
| 44 | + xml_arch = ET.fromstring(arch) |
| 45 | + field_node = xml_arch.xpath("//field[@name='x_technical_description']") |
| 46 | + self.assertTrue(field_node) |
| 47 | + field_node = field_node[0] |
| 48 | + attrs = field_node.get("attrs") |
| 49 | + # This domain as it is rendered with attributes, |
| 50 | + # it will make them visible only if the attribute set id equals 1 |
| 51 | + self.assertEqual(attrs, "{'invisible': [('attribute_set_id', 'not in', [1])]}") |
| 52 | + self.assertEqual(self.computer.id, 1) |
0 commit comments