@@ -605,6 +605,106 @@ def extract_weights(schema: SchemaBranch):
605605 assert len (in_second ) == 1 and in_second [0 ].startswith (new_attr2_partial_id )
606606
607607
608+ def test_schema_branch_processes_generic_template_schema_weight (register_core_models_schema ):
609+ schema = {
610+ "generics" : [
611+ {
612+ "name" : "GenericDevice" ,
613+ "namespace" : "Dcim" ,
614+ "description" : "Generic Device object." ,
615+ "label" : "Device" ,
616+ "icon" : "mdi:server" ,
617+ "human_friendly_id" : ["name__value" ],
618+ "order_by" : ["name__value" ],
619+ "display_labels" : ["name__value" ],
620+ "generate_template" : True ,
621+ "attributes" : [
622+ {"name" : "name" , "kind" : "Text" , "unique" : True , "order_weight" : 7000 },
623+ {"name" : "description" , "kind" : "Text" , "optional" : True , "order_weight" : 8000 },
624+ {"name" : "os_version" , "kind" : "Text" , "optional" : True , "order_weight" : 5200 },
625+ ],
626+ "relationships" : [
627+ {
628+ "name" : "tags" ,
629+ "peer" : "BuiltinTag" ,
630+ "optional" : True ,
631+ "cardinality" : "many" ,
632+ "kind" : "Attribute" ,
633+ "order_weight" : 3000 ,
634+ },
635+ ],
636+ },
637+ core_object_template ,
638+ core_object_component_template ,
639+ ],
640+ "nodes" : [
641+ {
642+ "name" : "Tag" ,
643+ "namespace" : "Builtin" ,
644+ "label" : "Tag" ,
645+ "default_filter" : "name__value" ,
646+ "attributes" : [
647+ {"name" : "name" , "kind" : "Text" , "label" : "Name" , "unique" : True },
648+ {
649+ "name" : "description" ,
650+ "kind" : "Text" ,
651+ "label" : "Description" ,
652+ "optional" : True ,
653+ "branch" : BranchSupportType .AGNOSTIC .value ,
654+ },
655+ ],
656+ },
657+ ],
658+ }
659+ schema_branch = SchemaBranch (cache = {}, name = "test" )
660+ schema_branch .load_schema (schema = SchemaRoot (** schema ))
661+ schema_branch .process ()
662+
663+ template = schema_branch .get (name = "TemplateDcimGenericDevice" , duplicate = False )
664+ dcim_generic_device = schema_branch .get (name = "DcimGenericDevice" , duplicate = False )
665+
666+ assert template .get_attribute (name = "template_name" ).order_weight == 1000
667+ assert (
668+ template .get_attribute (name = "description" ).order_weight
669+ == dcim_generic_device .get_attribute (name = "description" ).order_weight
670+ == 8000
671+ )
672+ assert (
673+ template .get_attribute (name = "os_version" ).order_weight
674+ == dcim_generic_device .get_attribute (name = "os_version" ).order_weight
675+ == 5200
676+ )
677+ assert (
678+ template .get_relationship (name = "tags" ).order_weight
679+ == dcim_generic_device .get_relationship (name = "tags" ).order_weight
680+ == 3000
681+ )
682+
683+ schema_2 = copy .deepcopy (schema )
684+ schema_2 ["generics" ][0 ]["attributes" ] = [
685+ {"name" : "name" , "kind" : "Text" , "unique" : False },
686+ {"name" : "description" , "kind" : "Text" , "optional" : True },
687+ {"name" : "os_version" , "kind" : "Text" , "optional" : True },
688+ ]
689+ schema_branch .load_schema (schema = SchemaRoot (** schema_2 ))
690+ schema_branch .process ()
691+
692+ template = schema_branch .get (name = "TemplateDcimGenericDevice" , duplicate = False )
693+ dcim_generic_device = schema_branch .get (name = "DcimGenericDevice" , duplicate = False )
694+
695+ assert (
696+ template .get_attribute (name = "name" ).order_weight == dcim_generic_device .get_attribute (name = "name" ).order_weight
697+ )
698+ assert (
699+ template .get_attribute (name = "description" ).order_weight
700+ == dcim_generic_device .get_attribute (name = "description" ).order_weight
701+ )
702+ assert (
703+ template .get_attribute (name = "os_version" ).order_weight
704+ == dcim_generic_device .get_attribute (name = "os_version" ).order_weight
705+ )
706+
707+
608708async def test_schema_branch_add_profile_schema (schema_all_in_one ):
609709 core_profile_schema = _get_schema_by_kind (core_models , kind = InfrahubKind .PROFILE )
610710 schema_all_in_one ["generics" ].append (core_profile_schema )
0 commit comments