11from odoo .tests .common import TransactionCase , tagged
2- from odoo import fields , Command
2+
33
44@tagged ("post_install" , "-at_install" )
55class TestProductTemplateAttributeValueXMLID (TransactionCase ):
6-
76 def setUp (self ):
87 super ().setUp ()
98 # Create common product template and attribute (dynamic variant creation)
10- self .product_template = self .env ['product.template' ].create ({
11- 'name' : 'Test Template' ,
12- })
13- self .attribute = self .env ['product.attribute' ].create ({
14- 'name' : 'Test Attribute' ,
15- 'create_variant' : 'dynamic' ,
16- })
17- self .attribute_value = self .env ['product.attribute.value' ].create ({
18- 'name' : 'Test Value' ,
19- 'attribute_id' : self .attribute .id ,
20- })
9+ self .product_template = self .env ["product.template" ].create (
10+ {
11+ "name" : "Test Template" ,
12+ }
13+ )
14+ self .attribute = self .env ["product.attribute" ].create (
15+ {
16+ "name" : "Test Attribute" ,
17+ "create_variant" : "dynamic" ,
18+ }
19+ )
20+ self .attribute_value = self .env ["product.attribute.value" ].create (
21+ {
22+ "name" : "Test Value" ,
23+ "attribute_id" : self .attribute .id ,
24+ }
25+ )
2126
2227 def test_xmlid_generation_on_ptav_creation (self ):
2328 """
@@ -26,39 +31,61 @@ def test_xmlid_generation_on_ptav_creation(self):
2631 automatically during variant creation.
2732 """
2833 # 1. Create a product.template.attribute.line
29- attribute_line = self .env ['product.template.attribute.line' ].create ({
30- 'product_tmpl_id' : self .product_template .id ,
31- 'attribute_id' : self .attribute .id ,
32- 'value_ids' : [(6 , 0 , [self .attribute_value .id ])],
33- })
34+ attribute_line = self .env ["product.template.attribute.line" ].create (
35+ {
36+ "product_tmpl_id" : self .product_template .id ,
37+ "attribute_id" : self .attribute .id ,
38+ "value_ids" : [(6 , 0 , [self .attribute_value .id ])],
39+ }
40+ )
3441
3542 # 2. Trigger Variant Creation (Simulate Selection)
3643 # This forces Odoo to create the product.template.attribute.value record
37- context = {'default_product_tmpl_id' : self .product_template .id }
38- self .product_template .with_context (context ).write ({
39- 'attribute_line_ids' : [(1 , attribute_line .id , {'value_ids' : [(6 , 0 , [self .attribute_value .id ])]})]
40- })
44+ context = {"default_product_tmpl_id" : self .product_template .id }
45+ self .product_template .with_context (** context ).write (
46+ {
47+ "attribute_line_ids" : [
48+ (
49+ 1 ,
50+ attribute_line .id ,
51+ {"value_ids" : [(6 , 0 , [self .attribute_value .id ])]},
52+ )
53+ ]
54+ }
55+ )
4156
4257 # 3. Find the Automatically Created product.template.attribute.value
43- ptav = self .env ['product.template.attribute.value' ].search ([
44- ('attribute_line_id' , '=' , attribute_line .id ),
45- ('product_attribute_value_id' , '=' , self .attribute_value .id )
46- ], limit = 1 )
58+ ptav = self .env ["product.template.attribute.value" ].search (
59+ [
60+ ("attribute_line_id" , "=" , attribute_line .id ),
61+ ("product_attribute_value_id" , "=" , self .attribute_value .id ),
62+ ],
63+ limit = 1 ,
64+ )
4765 self .assertTrue (ptav , "product.template.attribute.value was not created" )
4866
4967 # 4. Check for XML ID on the product.template.attribute.value
50- xml_id = self .env ['ir.model.data' ].search ([
51- ('model' , '=' , 'product.template.attribute.value' ),
52- ('res_id' , '=' , ptav .id ),
53- ], limit = 1 )
54- self .assertTrue (xml_id , f"No XML ID generated for product.template.attribute.value: { ptav .id } " )
68+ xml_id = self .env ["ir.model.data" ].search (
69+ [
70+ ("model" , "=" , "product.template.attribute.value" ),
71+ ("res_id" , "=" , ptav .id ),
72+ ],
73+ limit = 1 ,
74+ )
75+ self .assertTrue (
76+ xml_id ,
77+ f"No XML ID generated for product.template.attribute.value: { ptav .id } " ,
78+ )
5579
5680 def _get_xml_id (self , record ):
5781 """Helper function to get the XML ID of a record."""
5882 if not record :
59- return ''
60- xml_ids = self .env ['ir.model.data' ].search ([
61- ('model' , '=' , record ._name ),
62- ('res_id' , '=' , record .id ),
63- ], limit = 1 )
64- return xml_ids .name if xml_ids else ''
83+ return ""
84+ xml_ids = self .env ["ir.model.data" ].search (
85+ [
86+ ("model" , "=" , record ._name ),
87+ ("res_id" , "=" , record .id ),
88+ ],
89+ limit = 1 ,
90+ )
91+ return xml_ids .name if xml_ids else ""
0 commit comments