@@ -722,6 +722,35 @@ def test_OrderedNamespace(self) -> None:
722722 f"{ self ._namespace_class .__name__ } [{ self .namespace .id } ]'" , # type: ignore[has-type]
723723 str (cm2 .exception ))
724724
725+ def test_ordered_namespaceset_slice_setitem_preserves_order (self ) -> None :
726+ # Replace a slice of items; the new items must appear in the correct positions after replacement
727+ ns = ExampleOrderedNamespace ()
728+ sid1 = model .ExternalReference ((model .Key (model .KeyTypes .GLOBAL_REFERENCE , "http://example.org/sid1" ),))
729+ sid2 = model .ExternalReference ((model .Key (model .KeyTypes .GLOBAL_REFERENCE , "http://example.org/sid2" ),))
730+ sid3 = model .ExternalReference ((model .Key (model .KeyTypes .GLOBAL_REFERENCE , "http://example.org/sid3" ),))
731+ sid4 = model .ExternalReference ((model .Key (model .KeyTypes .GLOBAL_REFERENCE , "http://example.org/sid4" ),))
732+ sid5 = model .ExternalReference ((model .Key (model .KeyTypes .GLOBAL_REFERENCE , "http://example.org/sid5" ),))
733+ p1 = model .Property ("PA" , model .datatypes .Int , semantic_id = sid1 )
734+ p2 = model .Property ("PB" , model .datatypes .Int , semantic_id = sid2 )
735+ p3 = model .Property ("PC" , model .datatypes .Int , semantic_id = sid3 )
736+ ns .set1 .add (p1 )
737+ ns .set1 .add (p2 )
738+ ns .set1 .add (p3 )
739+ self .assertEqual ([p1 , p2 , p3 ], list (ns .set1 ))
740+
741+ # Replace slice [0:2] (p1, p2) with two new items
742+ new1 = model .Property ("PX" , model .datatypes .Int , semantic_id = sid4 )
743+ new2 = model .Property ("PY" , model .datatypes .Int , semantic_id = sid5 )
744+ ns .set1 [0 :2 ] = [new1 , new2 ]
745+
746+ # After replacement: [new1, new2, p3]
747+ result = list (ns .set1 )
748+ self .assertEqual ([new1 , new2 , p3 ], result )
749+ self .assertIsNone (p1 .parent )
750+ self .assertIsNone (p2 .parent )
751+ self .assertIs (ns , new1 .parent )
752+ self .assertIs (ns , new2 .parent )
753+
725754
726755class ExternalReferenceTest (unittest .TestCase ):
727756 def test_constraints (self ):
0 commit comments